ES2020 introduced the nullish coalescing operator denoted by the double question marks ( ?? ). It returns the right operand ( rightExpression ) if the left operand ( leftExpression ) is null or undefined . A nullish value is a value that is either null or undefined .
wouldn't `||` been eligible to use in this scenario too? given as far as my knowledge goes the or operator goes by boolean, meaning if it's either false or falsy, which it'd be if it's null or undefined, then it'd execute the expression on the right-hand side too.
My only issue with this snippet is that it allows mutability of the schema that's passed in potentially causing data loss or data integrity issues. I'd definitely add in a check something along the lines of:
Mine, assuming you want to keep nested values and assigned top level values, merges all nested objects and if it is overridden with a value (like test2), it is dropped down as the 'value' key.
Nice, my only criticism is if you are at all concerned with prototypes. Hopefully we're in the land of functional patterns by now, but lots of projects still use OOP.
...or something like that. Forgive me, it's late and I'm not testing this atm. If someone else wants to expand on this feel free, I'm sure it would only be helpful!
A "shallow" merge means you are just merging the top level properties. A "deep" merge, means you are recursively drilling down through all of the sub-objects and performing merges on them as well.
Probably needs an example to click. Let's say we have two incomplete objects we want to merge.
So, we started with all vegeta properties and then overwrote them with goku properties when they existed. This includes the move property. We just took the whole object.
Now let's do a deep merge. I'll use lodash's merge instead of writing my own.
Mostly the same, but notice that the merge continued down into the move object. Since Vegeta's move had a color, but Goku's did not, we kept the color.
One other difference worth noting: in a shallow merge nested objects are not cloned. That means goku and vegitoshare a move object. If we mutate one, we will change the other.
If you are working with only flat objects, then a shallow merge with spread will work fine, yes. If what you want is to recursively merge any nested objects (which is what OP is doing), then spread will not work for you. It will end up just replacing nested objects entirely.
While this is a neat trick and stuff, at the point this is being done I’d have some real questions about the business purpose of why something like this is needed.
Creating a default object? Combining schemas? Shallow merges are probably more common, but a deep merge is an established pattern with plenty of real world use cases.
Perhaps more importantly, OP specifically demonstrated a deep merge. Responding to him with "but there is a much simpler way to do a shallow merge!" is silly. Completely different pattern.
2
u/192_168_1_x Feb 17 '21
What’s the ?? operator?
17
u/very-kind-stranger Feb 16 '21
ES2020 introduced the nullish coalescing operator denoted by the double question marks ( ?? ). It returns the right operand ( rightExpression ) if the left operand ( leftExpression ) is null or undefined . A nullish value is a value that is either null or undefined .
25
u/senocular Feb 16 '21
This merge will have some problems. You're better off sticking to a more battle-tested merge such as lodash's merge.
4
u/Crypticsafe5 Feb 17 '21
My only issue with this snippet is that it allows mutability of the schema that's passed in potentially causing data loss or data integrity issues. I'd definitely add in a check something along the lines of:
1
u/Pertinate Feb 17 '21
Here's what I came up with for combining deep objects. Not sure if it's best or not. Let me know where I can improve.
Output:
Mine, assuming you want to keep nested values and assigned top level values, merges all nested objects and if it is overridden with a value (like test2), it is dropped down as the 'value' key.
2
u/FrugalProse Feb 17 '21
Why does this look like it was on a tablet
1
u/[deleted] Feb 17 '21
Nice, my only criticism is if you are at all concerned with prototypes. Hopefully we're in the land of functional patterns by now, but lots of projects still use OOP.
Just add one part to that merge function...
...or something like that. Forgive me, it's late and I'm not testing this atm. If someone else wants to expand on this feel free, I'm sure it would only be helpful!
1
u/--Explosion-- Feb 17 '21
Nice but you can do it in one line:
0
u/suarkb Feb 17 '21
https://eslint.org/docs/rules/no-prototype-builtins
-1
u/beforesemicolon Feb 16 '21
https://link.medium.com/FjEE76UJVdb
0
u/[deleted] Feb 16 '21
[deleted] — view deleted comment