Skip to content

Commit

Permalink
fixed, array indices can be more than one digit long
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Amram authored and flexdinesh committed Mar 7, 2018
1 parent 6588e1b commit 6d102b6
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ export const getNestedObject = (obj, dotSeparatedKeys) => {
pathArr.forEach((key, idx, arr) => {
if (typeof key === 'string' && key.includes('[')) {
try {
arr.splice(idx + 1, 0, Number(/\[([^)]+)\]/.exec(key)[1]));
arr[idx] = key.slice(0, -3); // eslint-disable-line no-param-reassign
// extract the array index as string
const pos = /\[([^)]+)\]/.exec(key)[1];
// get the index string length (i.e. '21'.length === 2)
const posLen = pos.length;
arr.splice(idx + 1, 0, Number(pos));

// keep the key (array name) without the index comprehension:
// (i.e. key without [] (string of length 2)
// and the length of the index (posLen))
arr[idx] = key.slice(0, (-2 - posLen)); // eslint-disable-line no-param-reassign
} catch (e) {
// do nothing
}
Expand Down

0 comments on commit 6d102b6

Please sign in to comment.