Should throw if falsey key value exists
donavon opened this issue · comments
In your example, if foo.d
is already set to say 5
, it throws. I would expect that. However, if it is 0
(or anything falsey), it paves over the falsey value.
{a: 1, b: 2, d: 0}
//=> { a:1, b:2, d:{ e:{ f:'hello' } } };
A possible fix is:
x = o[keys[i]] === undefined ? {} : o[keys[i]];
Ah, right, good catch~! I'll fix this, but we should also be paving on null
value too.
Slightly unrelated, but thoughts about not throwing if a value exists? Instead, would silently break & exit.
I wouldn't pave over a null
as it is an actual value.
I'm a big fan of throwing when there is an exception. Hiding/masking the problem will make it harder to track down the root cause.
Actually, you might want to use this instead:
x = keys[i] in o ? o[keys[i]] : {};
That'd throw immediately for anything that isn't already an object, including undefined
s 😆 That's probably what you want, except for the latter.