Modify deepSet() to allow appending to arrays

This commit is contained in:
D. Berge
2023-11-13 20:52:08 +01:00
parent 26a487aa47
commit 80de0c1bb0

View File

@@ -175,11 +175,19 @@ function deepEqual (a, b) {
* If a non-leaf property does not exist, this function * If a non-leaf property does not exist, this function
* creates it as an empty object ({}) and keeps traversing. * creates it as an empty object ({}) and keeps traversing.
* *
* The last member of `path` may be `null`, in which case,
* if the object pointed to by the next to last member is
* an array, an insert operation will take place.
*
*/ */
function deepSet (obj, path, value) { function deepSet (obj, path, value) {
const key = path.shift(); const key = path.shift();
if (!path.length) { if (!path.length) {
obj[key] = value; if (key === null && Array.isArray(obj)) {
obj.push(value);
} else {
obj[key] = value;
}
} else { } else {
if (!Object.hasOwn(obj, key)) { if (!Object.hasOwn(obj, key)) {
obj[key] = {}; obj[key] = {};