Skip to content

Commit

Permalink
merge with target, not clone with target...
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Stone committed Nov 6, 2020
1 parent cf8ef25 commit 971f5e6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function propertyIsUnsafe(target, key) {

// Retrieves either a new object or the appropriate target object to mutate.
function getDestinationObject(target, options) {
if (options && !!options.cloneWithTarget) {
if (options && !!options.mergeWithTarget) {
return Array.isArray(target) ? firstArrayEntry(target) : target
}

Expand Down
12 changes: 6 additions & 6 deletions test/merge.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
var merge = require('../')
var test = require('tape')

test('result should retain target type information when cloneWithTarget set to true', function(t) {
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, {cloneWithTarget: true})
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 cloneWithTarget set to true', function(t) {
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, {cloneWithTarget: true})
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 cloneWithTarget set to true', function(t) {
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], {cloneWithTarget: true})
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')
Expand Down

0 comments on commit 971f5e6

Please sign in to comment.