-
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
Apply clone option to target object #209
base: master
Are you sure you want to change the base?
Conversation
This looks good. One request – could you add a test that asserts that when |
Great call, I hadn't used that entrypoint so didn't think to change it. I made the change, but it required a few more lines to change to make it elegant and aligned with the code style in place. The Let me know if you'd like any add'l changes! |
I like it but I am a little worried about backwards compatibility. As is, this would definitely need to be released as a new major version. |
You're probably right. This is an awkward one because it sounds like it is technically a bug fix because the API "lies" to the consumer about how the clone option is applied, but fixing it potentially breaks consumers whose code relies on the bug being there. I'll let you guys duke it out 😉 |
Yup, this would be a breaking change. I'm thinking to pair it with removing the deprecation warning from the |
I like the idea behind it but this is definitely a breaking change that I can see a lot of push back happening on if it goes live. The current implementation of the These two behaviors are very different and I don't think it would be a good idea to switch from one to the other using the same flag. The issue with having this behavior on the "clone" flag is that it that it is valid to want to both clone and not mutate the any of the parameters. I would be best to put this behavior behind a new flag called "mergeWithTarget". I just made a new issue #212 with my proposal for the clone behavior. |
@RebeccaStevens I lied about how long it would take. My wife started watching The Bachelorette and that was my cue to zone out. Let me know if this addresses the concerns re: |
Looks good to me 👍 |
@creativeux do you want to mark this as closes #186? |
index.js
Outdated
@@ -51,8 +55,17 @@ function propertyIsUnsafe(target, key) { | |||
&& Object.propertyIsEnumerable.call(target, key)) // and also unsafe if they're nonenumerable. | |||
} | |||
|
|||
// Retrieves either a new object or the appropriate target object to mutate. | |||
function getDestinationObject(target, options) { | |||
if (options && options.mergeWithTarget) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RebeccaStevens does this need to account for options.clone
scenarios for objects that are not the root target? I've read through it a few times and with deepmerge
being self-referenced, I wonder if there's a case where this function would be called in reference to an object nested deeper than the entrypoint where we might want to return the target here in the case where options.clone
is false
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No I don't think so. This function is only called at most once per call of the main deepmerge function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per my last commit, this is called repeatedly during recursion down an object tree. Not sure if this changes your thought on the topic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specifically, deepmerge
is returned from cloneUnlessOtherwiseSpecified
which can call mergeObject
which calls getDestinationObject
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'm seeing the issue you are talking about here.
I think with options.mergeWithTarget: true
& options.clone: false
, merging with each target going down should happen.
I think with options.mergeWithTarget: true
& options.clone: true
, merging should only happen with the top most target.
Actually no, scrap that. Merging should only ever happen with the top most target.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the codebase, it doesn't really look set up to be able to add this functionality easily in a non-hacky way.
A bunch of refactoring may be needed (which I'm in the process of doing in #215).
If you want to just do it in a hacky way, you could just remove mergeWithTarget
from the options object before it is passed to the recursive call.
@TehShrike and @RebeccaStevens - I caught a bug with my in-use fork of this where the merge fails in the case that a target is |
return Array.isArray(target) ? firstArrayEntry(target) : target | ||
} | ||
|
||
return isArray ? [] : {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The destination object should never be an array.
This is how I would define this function:
function getDestinationObject(target, options) {
const targetDefined = typeof target !== 'undefined'
const doMerge = options && (options.mergeWithTarget || options.clone === false)
if (targetDefined && doMerge) {
return target
}
return {};
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me, the array check was required because during recursion it's possible that there is a property being handled whose value is undefined
, but we know that the source value is an array, so we need to instantiate an empty object that can be merged into. If I remove this, nested properties with array types will fail to merge because an array source will try to be merged into an object target.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah sorry, I was thrown off by the function mergeObject
's name. I assumed that it's parameters were always non-array objects but in actual fact it merges any 2 values (or returns the latter one if they can't be merged).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait no, that's not right. mergeObject
does only ever receive non-array objects. target
and source
should never be arrays in mergeObject
- arrays should be handled by options.arrayMerge
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@creativeux I suspect you'll need to update the logic in deepmerge
itself to handle the case of target
being undefined
.
https://github.com/TehShrike/deepmerge/blob/master/index.js#L87-L93
@@ -100,7 +117,7 @@ deepmerge.all = function deepmergeAll(array, options) { | |||
|
|||
return array.reduce(function(prev, next) { | |||
return deepmerge(prev, next, options) | |||
}, {}) | |||
}, getDestinationObject(array, undefined, options)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following on from my other comment, this is how getDestinationObject
would be called from deepmergeAll
:
const target = array[0];
const destination = getDestinationObject(target, options)
const source = destination === target ? array.slice(1) : array
return source.reduce(function(prev, next) {
return deepmerge(prev, next, options)
}, destination)
2ae7430
to
3b66e4c
Compare
closes #208
closes #186
destination
object based on theclone
option.clone
is set tofalse
.