Skip to content

Commit

Permalink
Fix crashing modifying array length
Browse files Browse the repository at this point in the history
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 = {}`
  • Loading branch information
Jimbly committed Jan 1, 2022
1 parent d400c8d commit 0704dd7
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions index.js
Expand Up @@ -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];
Expand Down

0 comments on commit 0704dd7

Please sign in to comment.