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
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ function emptyTarget(val) {
return Array.isArray(val) ? [] : {}
}

function firstArrayEntry(arr) {
return arr && arr.length ? arr[0] : []
}

function cloneUnlessOtherwiseSpecified(value, options) {
return (options.clone !== false && options.isMergeableObject(value))
? deepmerge(emptyTarget(value), value, options)
Expand Down Expand Up @@ -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.

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

return {}
}

function mergeObject(target, source, options) {
var destination = {}
var destination = getDestinationObject(target, options)
if (options.isMergeableObject(target)) {
getKeys(target).forEach(function(key) {
destination[key] = cloneUnlessOtherwiseSpecified(target[key], options)
Expand Down Expand Up @@ -100,7 +113,7 @@ deepmerge.all = function deepmergeAll(array, options) {

return array.reduce(function(prev, next) {
return deepmerge(prev, next, options)
}, {})
}, getDestinationObject(array, options))
}

module.exports = deepmerge
36 changes: 36 additions & 0 deletions test/merge.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
var merge = require('../')
var test = require('tape')

test('result should retain target type information when mergeWithTarget set to true', function(t) {
var src = { key1: 'value1', key2: 'value2' }
class CustomType {}
var target = new CustomType()

var res = merge(target, src, {mergeWithTarget: true})
t.not(src instanceof CustomType)
t.assert(target instanceof CustomType)
t.assert(res instanceof CustomType)
t.end()
})

test('modify target object if mergeWithTarget set to true', function(t) {
var src = { key1: 'value1', key2: 'value2' }
var target = { key3: 'value3'}

var clonedRes = merge(target, src)
var notClonedRes = merge(target, src, {mergeWithTarget: true})

t.assert(clonedRes !== target, 'result should be cloned')
t.assert(notClonedRes === target, 'result should maintain target reference')
t.end()
})

test('merge.all mutates target object when mergeWithTarget set to true', function(t) {
var src = { key1: 'value1', key2: 'value2' }
var target = { key3: 'value3'}

var clonedRes = merge.all([target, src])
var notClonedRes = merge.all([target, src], {mergeWithTarget: true})

t.assert(clonedRes !== target, 'result should be cloned')
t.assert(notClonedRes === target, 'result should maintain first array entry reference')
t.end()
})

test('add keys in target that do not exist at the root', function(t) {
var src = { key1: 'value1', key2: 'value2' }
var target = {}
Expand Down