-
Notifications
You must be signed in to change notification settings - Fork 217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't merge if value is an empty string #178
Comments
That is the current behavior: https://codepen.io/TehShrike/pen/Jjjrwzo What version of deepmerge are you using? A bug with falsey values not being handled properly was recently fixed in #172 |
Ah ok. I'm using 4.0 but I see 4.2 is available now. I'll try upgrading and see if it fixes it. Thanks! |
In your codepen with 4.2.2 version neither
Why is this closed? Can we have an option to specify values to treat as non-existent? |
I see. You had the arguments backwards in your original post – it sounds like you want
Unfortunately, I don't believe there's a way to twist There have been some other edge case requests like this that have come up. I'm tempted to alter the const mergedObject = deepmerge(defaultObject, newObject, {
customMerge(key, a, b) {
if (a === '') return () => b
},
}) |
Nah, it' not my original post, I'm just fighting with the same issue now. For me API returns This can be fixed with replacing
with
Then the following code
gives you
PR Created: |
I think this requires deep understanding of an internal code of a library and I'd go with more human-readable options, but you're maintainer. But shouldn't your code be const mergedObject = deepmerge(defaultObject, newObject, {
customMerge(key, a, b) {
if (b === '' || b === null || b === undefined) return () => a
},
}) Or is it somewhere swapped the order of parameters? |
I'd favor the more generalized customMerge way to solve this because in my case I'd only want to ignore empty strings and undefined but keep null values. Others may have other requirements and customMerge(key, a, b) would solve all of them. If this is documented in the readme with examples like posted in this issue, this shouldn't be too hard to use. |
I need to merge some data that originates from a
.yaml
file and is converted to a JS object using GraphQL (I'm using Gatsby with Netlify CMS if you need a real world use case).As you may know, yaml does not allow
undefined
values. The closest alternative is to set the value tonull
or an empty string (''
).For example, let's say I have the following GraphQL query:
Each field (
one
,two
andthree
) are optional fields.For example, if only
two
field has a value then the yaml file will be:If I attempt to run the query on that data, GraphQL will throw an error as fields
one
andthree
don't exist.The best I can do is to set a default value for all fields as
''
(an empty string). I can't set it tonull
due to an issue where users who change the value from the defaultnull
value, then delete the value, will save the value as an empty string.For example, if only the
two
field has a value then the yaml file will be:And after running the GraphQL query, it will return the following object:
If I wanted to merge it with the following object:
Then run
deepmerge()
on the two objects, it will simply return the first object:Having an option in deepmerge to ignore empty strings would be a lifesaver for this, for example:
Would result in
mergedObject
being equal to:The text was updated successfully, but these errors were encountered: