Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle null in obj, handle empty string in path #37

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}