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

Apply clone option to target object #209

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

creativeux
Copy link

@creativeux creativeux commented Oct 29, 2020

closes #208
closes #186

  • Added ternary for initializing the destination object based on the clone option.
  • Added 2 unit tests... 1 to test that target type info is retained, and another to ensure that target memory pointer is maintained when clone is set to false.

@TehShrike
Copy link
Owner

This looks good. One request – could you add a test that asserts that when merge.all is called with { clone: true }, the first element is mutated/returned?

@creativeux
Copy link
Author

could you add a test that asserts that when merge.all is called with { clone: true }, the first element is mutated/returned?

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 firstArrayEntry() method is there to avoid chained ternaries which can be confusing to read, and I moved the logic to determine if the destination should be a new object or a target object into a function.

Let me know if you'd like any add'l changes!

@RebeccaStevens
Copy link

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.

@creativeux
Copy link
Author

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 😉

@TehShrike
Copy link
Owner

Yup, this would be a breaking change. I'm thinking to pair it with removing the deprecation warning from the clone flag

@RebeccaStevens
Copy link

RebeccaStevens commented Nov 6, 2020

I've changed my mind on liking this. [I'm back to liking it now]

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 clone flag makes it so that if set to false, objects will be directly copied over to the resulting merged object.
The new behavior in this MR as is makes it so that when the clone flag is false, the target option will be mutated.

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.

@creativeux
Copy link
Author

@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: clone vs mergeWithTarget.

index.js Outdated Show resolved Hide resolved
@RebeccaStevens
Copy link

Looks good to me 👍

@RebeccaStevens
Copy link

RebeccaStevens commented Nov 6, 2020

@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) {
Copy link
Author

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?

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.

Copy link
Author

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.

Copy link
Author

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.

Copy link

@RebeccaStevens RebeccaStevens Nov 12, 2020

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.

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.

@creativeux
Copy link
Author

@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 undefined. I pushed a fix and a unit test to ensure that there is always an emtpy object or array to merge into if the target is undefined.

return Array.isArray(target) ? firstArrayEntry(target) : target
}

return isArray ? [] : {};
Copy link

@RebeccaStevens RebeccaStevens Nov 12, 2020

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 {};
}

Call from deepmergeAll

Copy link
Author

@creativeux creativeux Nov 12, 2020

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.

Copy link

@RebeccaStevens RebeccaStevens Nov 13, 2020

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).

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.

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))
Copy link

@RebeccaStevens RebeccaStevens Nov 12, 2020

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)

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

Successfully merging this pull request may close these issues.

Merging objects discards target class information Restore "clone" feature via new option : mergeWithTarget
3 participants