Echo JS 0.11.0

<~>
Balkoth 2778 days ago. link 1 point
What's the point of using this over Object.assign ?

Replies

jth0024 2778 days ago. link 1 point
Good question! Well, Object.Assign overwrites any previous properties without discretion. For example, Object.Assign({ name: 'Greg' }, { name: 'Fred'}) will result in { name: 'Fred' }. That behavior isn't ideal when you only want to apply default values for missing arguments, a common task in my projects. Essentially, this library is offering a customizable Object.Assign. You can use the default customizer, which only overwrites null or undefined values, or you can write your own customizer that implements any logical check that you choose.
xat 2778 days ago. link 2 points
The way Object.assign is usually used is like this:

const opts = Object.assign({}, defaults, options);

It will fallback to the properties defined in defaults if they don't exist in options.

Nowadays you can also use destructering to achieve the same:

const opts = { ...defaults, ...options };
jth0024 2778 days ago. link 1 point
Yep, that's a great way to use Object.assign, but that doesn't really cover the variety of cases that prompted me to write this helper. For example, your code only works if options does not have a key for the default value, but not when it has a key with a null, undefined, or otherwise unacceptable value. const defaults = { name: '' }; const opts = { name: null }; Name would be set to null, which is a common case I try to avoid.
xat 2778 days ago. link 1 point
If someone explicitly passes in "const opts = { name: null }" then I would argue that he wants name to be null and it should not be overwritten by what is defined in defaults. But I agree with you that there may be some (rare?) cases where some other behaviour is wanted.
jth0024 2778 days ago. link 1 point
Well, I can't speak for the needs of every other developer or application, but I have personally encountered plenty of cases where null and undefined checks are necessary to avoid runtime errors.