From 6d102b65c8075e3f10bbebf5c850cb84e4256cc6 Mon Sep 17 00:00:00 2001 From: Gabriel Amram Date: Wed, 7 Mar 2018 10:21:56 +0200 Subject: [PATCH] fixed, array indices can be more than one digit long --- src/util.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/util.js b/src/util.js index 0a014b0..49dd478 100644 --- a/src/util.js +++ b/src/util.js @@ -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 }