lukeed / dset

A tiny (197B) utility for safely writing deep Object values~!
MIT License
759 stars 25 forks source link

Should throw if falsey key value exists #1

Closed donavon closed 6 years ago

donavon commented 6 years ago

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]];
lukeed commented 6 years ago

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.

donavon commented 6 years ago

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]] : {};
lukeed commented 6 years ago

That'd throw immediately for anything that isn't already an object, including undefineds 😆 That's probably what you want, except for the latter.