Skip to content
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

merge.all with single element array #71

Closed
shawn98ag opened this issue Aug 25, 2017 · 3 comments
Closed

merge.all with single element array #71

shawn98ag opened this issue Aug 25, 2017 · 3 comments

Comments

@shawn98ag
Copy link

I'm curious why when one calls merge.all() with a single element array, [{'a':1}], this function doesnt just return the value of that first element in the array {'a':1}? The function is instead throwing an error.

In my code, I'm dynamically creating an array of objects that I need to merge together. Occasionally, that array only has one element. To protect against this error I have to add .concat({}) to the array argument increase its length to 2.

@TehShrike
Copy link
Owner

deepmerge doesn't mutate either argument that is passed in to it, so it's difficult to imagine what would happen if you passed just a single object to it. Would it just be a shallow copy to a new object, like Object.assign?

It would be a breaking change, but I think it would be a bit easier to fix this if we made clone default to true so that there would always be some default behavior you could expect ("copy everything").

As an aside, I'd personally prefer this to the concat I think:

const output = array.length > 1 ? merge.all(array) : array[0]

Or if you want the copy:

const output = array.length > 1 ? merge.all(array) : merge({}, array[0])

@TehShrike
Copy link
Owner

My current stance: if we do publish a breaking change to make cloning the default (#72), we can feel free to make this change so that merge.all can take any number of inputs.

@TehShrike
Copy link
Owner

As of version 2.0.0, merge.all takes an array of any size, including 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants