Skip to content

Commit

Permalink
fix: handle null in obj, handle empty string in path
Browse files Browse the repository at this point in the history
Closes #35
Closes #36
  • Loading branch information
bgoscinski committed Apr 24, 2022
1 parent 56923fe commit f4299cb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export function dset(obj, keys, val) {
while (i < l) {
k = keys[i++];
if (k === '__proto__' || k === 'constructor' || k === 'prototype') break;
t = t[k] = (i === l) ? val : (typeof(x=t[k])===typeof(keys)) ? x : (keys[i]*0 !== 0 || !!~(''+keys[i]).indexOf('.')) ? {} : [];
t = t[k] = (i === l) ? val : ((x=t[k]) && typeof(x)===typeof(keys)) ? x : (k='+'+keys[i])*0 !== 0 || /\./.test(k) ? {} : []
}
}
2 changes: 1 addition & 1 deletion src/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export function dset(obj, keys, val) {
while (i < l) {
k = keys[i++];
if (k === '__proto__' || k === 'constructor' || k === 'prototype') break;
t = t[k] = (i === l) ? merge(t[k],val) : (typeof(x=t[k])===typeof keys) ? x : (keys[i]*0 !== 0 || !!~(''+keys[i]).indexOf('.')) ? {} : [];
t = t[k] = (i === l) ? merge(t[k],val) : ((x = t[k]) && typeof x === typeof keys) ? x : (k = '+' + keys[i]) * 0 !== 0 || /\./.test(k) ? {} : [];
}
}
20 changes: 20 additions & 0 deletions test/suites/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,25 @@ export default function (dset, isMerge) {
}
});

objects(`should ${verb} empty string property`, () => {
let { input } = prepare({});

dset(input, ['hello', ''], 123);

assert.equal(input, {
hello: { '': 123 },
});
});

objects(`should ${verb} null values`, () => {
let { input } = prepare({ hello: null });

dset(input, ['hello', 'a'], 123);

assert.equal(input, {
hello: { a: 123 },
});
});

objects.run();
}

0 comments on commit f4299cb

Please sign in to comment.