Skip to content

Commit

Permalink
respect & preserve falsey values (closes #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Feb 21, 2018
1 parent e56ae65 commit f1b36b8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default function (obj, keys, val) {
while (i < len) {
o = obj;
for (j=0; j < i; j++) o=o[keys[j]];
x = o[keys[i]] || {};
x = (o[keys[i]] == null) ? {} : o[keys[i]];
o[keys[i]] = (++i === len) ? val : x;
}
}
3 changes: 3 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ test('deepset', t => {
t.is(foo.d.a.x.y, 456, 'refuses to convert existing non-object value into object');
fn(foo, 'd.a.x.z', [1,2,3,4]); // preserve object tree, with array value
t.same(foo.d.a, { b:123, x:{y:456,z:[1,2,3,4]} }, 'mutates; writes into existing object w/ array value');
// Change?
t.throws(_ => fn(foo, 'b.c.d.e', 123), /TypeError/, 'throws if trying to nest within non-object value');
t.throws(_ => fn({ a:1, b:0, c:2 }, 'b.a.s.d', 123), /TypeError/, 'throws while respecting `0` as a value');
t.end();
});

0 comments on commit f1b36b8

Please sign in to comment.