Skip to content

Commit

Permalink
fix: special case merging when merging keys of length zero
Browse files Browse the repository at this point in the history
  • Loading branch information
Ozzie Kirkby committed Mar 14, 2022
1 parent 56923fe commit ffc84ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export function merge(a, b, k) {
export function dset(obj, keys, val) {
keys.split && (keys=keys.split('.'));
var i=0, l=keys.length, t=obj, x, k;
if (l === 0) {
merge(t, val)
return
}

while (i < l) {
k = keys[i++];
if (k === '__proto__' || k === 'constructor' || k === 'prototype') break;
Expand Down
17 changes: 17 additions & 0 deletions test/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,23 @@ dsets('should merge array at path notation', () => {
]);
});

dsets('should be able to insert at root', () => {
let input = {};
dset(input, [], { hero: { id: "A" }});
assert.equal(input, {
hero: {
id: "A"
}
});
});

dsets('should ignore root merge of value without key', () => {
let input = {};
dset(input, [], "A");
assert.instance(input, Object);
assert.equal(input, {});
});

dsets.run();

// ---
Expand Down

0 comments on commit ffc84ff

Please sign in to comment.