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

Add "clone" option #28

Closed
gagan-bansal opened this issue Feb 22, 2016 · 7 comments
Closed

Add "clone" option #28

gagan-bansal opened this issue Feb 22, 2016 · 7 comments

Comments

@gagan-bansal
Copy link
Contributor

here is the test

var src = {
  "b": {
    "c": 'foo'
  }
}

var target = {
  "a": {}
}

var expected = {
  "a": {},
  "b": {
    "c": 'foo'
  }
}
var combined = merge(target, src)
src.b.c = 'bar'

console.log(combined.b.c)
// output as
// 'bar'
// though it should be 'foo'

Is this intentional behavior? I think in most of the cases source object should be cloned.

@TehShrike
Copy link
Owner

Interesting. So the issue is that you (quite reasonably) expected that all values would be recursively copied, and it definitely doesn't do that.

The docs don't claim that it recursively copies, but it sure does look like it would. I'm tempted to add this behavior because it's what I'm more likely to want, but I'm averse to adding it because it would be a breaking change.

In any case, if the change doesn't get added and #29 isn't merged, the documentation should at least be made clearer on this behavior.

Any other opinions? Is recursive copying worth the breaking change?

@gagan-bansal
Copy link
Contributor Author

Yes it will break the present behavior and sure its major change.

But as the document states that a new merged object would be created then expectations are that the changes in new object would not affect the original objects. If someone wants the current behavior (without cloning) then generally extend is used.

As the change is major I think more suggestions are required.

@gagan-bansal
Copy link
Contributor Author

Passing clone (boolean) as an argument would be better. Like

merge(x, y, clone)

and default value for clone should be false.

@TehShrike
Copy link
Owner

Yeah, that would probably be the way to go. #37 adds an options object - a clone property on that would be a good way to enable this behavior.

@gagan-bansal
Copy link
Contributor Author

gagan-bansal commented Sep 28, 2016

OK once you merge #37 I'll make changes to #29 so that clone as option is passed in third argument options.

@TehShrike
Copy link
Owner

👍

@TehShrike TehShrike changed the title merged object is not cloned deeply Add "clone" option Sep 29, 2016
gagan-bansal added a commit to gagan-bansal/deepmerge that referenced this issue Oct 2, 2016
@TehShrike
Copy link
Owner

Published as 1.2.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