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 6 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) {
creativeux marked this conversation as resolved.
Show resolved Hide resolved
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