debitoor/dot-prop-immutable

Exception thrown when adding an item to array with $end, and the array is empty

Closed this issue · 0 comments

Example:
let obj={a:[]};
dotProp.set(Obj, 'a.$end', 5);
index.js:147 Uncaught (in promise) Error: Array index '-1' has to be an integer
The behavior when the array is empty should be consistent with the case when the array is not empty.

function getArrayIndex(head, obj) { if (head === '$end') { head = obj.length - 1; } if (!/^\+?\d+$/.test(head)) { throw new Error('Array index \'' + head + '\' has to be an integer'); } return parseInt(head); }