-
Notifications
You must be signed in to change notification settings - Fork 216
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
Variable number of arguments #33
Comments
That would make sense to me, too. It might be a bit trickier to add though, since so many people seem to want the ability to configure array merging via a third argument (#14). It's probably still possible, though. I wouldn't be against a pull request. |
If anyone wants to suggest a backwards-compatible API and/or open a pull request, fire away. |
While this option may be convenient, it's not too difficult to just write a wrapper around const deepMerge = require("deepmerge")
const deepMergeAll = (...xs) => {
const initial = xs.shift()
return xs
.filter((x) => typeof x !== 'undefined' && x !== null)
.reduce((x, y) => deepMerge(x, y), initial || {})
}
deepMergeAll({ a: 1 }, { b: 2 }, null, { c: 3 })
// { a: 1, b: 2, c: 3 } I don't think there are very many projects that use more than a maximum of 5 different array merge strategies (that just sounds super rare & unrealistic, imo) so as far as boilerplate goes, this isn't too bad. |
Well, it is possible just to expose another function, as @declandewet suggested. |
How do y'all feel about an API like const mergeAll = deepmerge.options({ arrayMerge: someFunction })
const mergedA = mergeAll(a, b, c)
const mergedB = mergeAll(b, c, a) |
My suggestion: merge([x, y, z], { /* options */ }); |
I like that, though I don't think we could distinguish between that and somebody trying to merge a regular object into a normal array. We'd have to add a new method, |
You can't merge a Boolean, so you could inspect if the first argument is boolean and use that as a hint that the 2nd param is an options object and return a function that merges all arguments const deepMergeAll = deepmerge(true, [options])
const merged = deepMergeAll(a, b, c, d) This would be just as convenient and mitigate any need for a breaking change or perhaps a string might be more descriptive const deepMergeAll = deepmerge('all', [options]) |
Adding a new function like Relying on special values in ordered arguments makes me nervous. |
I feel the same as @TehShrike – there is no particular need to break things, and passing Though I am not sure about instantiating new function with different merge strategies – it looks kind of strange too (at least for me). I can't really suggest better solution – only the first argument, and then collect other values through rest spread, but then we make first argument mandatory. |
Vote for your preference via reactions (assume that the names are changeable, so vote just based on api/functionality): Option ❤️ const merge = require('deepmerge')
const mergeAll = merge.options({ clone: true })
const mergedObject = mergeAll(a, b, c) Option 🎉 const merge = require('deepmerge')
const mergedObject = merge.all([a, b, c], { clone: true }) |
Sounds good to me! We'll go with Unless anyone thinks of an objection, I'll say the specs are something like:
Pull requests with tests for these checks, or tests for the rest of the functionality, would be much appreciated. I think they should go into a new file |
@TehShrike I'd like to do this! I can do this on weekend. |
This sounds Like a very good way to go about this! |
This is implemented by #50 and published as 1.3.0! |
It would be nice if deepmerge will support a viariable number of arguments
The text was updated successfully, but these errors were encountered: