From 0704dd7d4cdf50fdc73ad82a46a8b750577e3c29 Mon Sep 17 00:00:00 2001 From: Jimb Esser Date: Sat, 1 Jan 2022 11:17:56 -0600 Subject: [PATCH] Fix crashing modifying array length Also avoids unnecessary allocation and set. Fixes cases like the following: ```javascript let obj = { a: [1,2] }; dotProp.delete(obj, 'a.1'); dotProp.set(obj, 'a.length', 1); ``` Previously would throw `Uncaught RangeError: Invalid array length` when it tries to set `obj.a.length = {}` --- index.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 3fe9b42..cd66e82 100644 --- a/index.js +++ b/index.js @@ -73,12 +73,10 @@ module.exports = { for (let i = 0; i < pathArray.length; i++) { const p = pathArray[i]; - if (!isObject(object[p])) { - object[p] = {}; - } - if (i === pathArray.length - 1) { object[p] = value; + } else if (!isObject(object[p])) { + object[p] = {}; } object = object[p];