Skip to content

Commit

Permalink
fix: remove mutating of target with clone=false
Browse files Browse the repository at this point in the history
this is wanted with a new option "mergeWithTarget"
  • Loading branch information
RebeccaStevens committed Jan 23, 2021
1 parent 7c537a1 commit 0a5a3c3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
5 changes: 3 additions & 2 deletions src/deepmerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ function mergeObject<
T2 extends Record<Property, unknown>,
O extends Options
>(target: T1, source: T2, options: FullOptions<O>): DeepMergeObjects<T1, T2, O> {
const destination: any = options.clone ? emptyTarget(target) : target
// const destination: any = options.mergeWithTarget ? target : {}
const destination: any = {}

if (options.isMergeable(target)) {
getKeys(target).forEach(
Expand Down Expand Up @@ -117,5 +118,5 @@ export function deepmergeAll(objects: ReadonlyArray<object>, options?: Options):
: value
}

return objects.reduce((prev, next) => deepmergeImpl(prev, next, fullOptions))
return objects.reduce((prev, next) => deepmergeImpl(prev, next, fullOptions), {})
}
23 changes: 12 additions & 11 deletions test/merge-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,15 @@ test(`invoke merge on every item in array without clone should clone all element
t.end()
})

test(`With clone: false, mergeAll should not clone the target root`, (t) => {
const destination = {}
const output = mergeAll([
destination, {
sup: true,
},
], { clone: false })

t.equal(destination, output)
t.end()
})
// test(`With mergeWithTarget=true, mergeAll should mutate the root target`, (t) => {
// const destination = {}
// const output = mergeAll([
// destination, {
// sup: true,
// },
// ], { mergeWithTarget: true })

// t.notEqual(destination, {})
// t.equal(destination, output)
// t.end()
// })
19 changes: 10 additions & 9 deletions test/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,12 +668,13 @@ test(`Falsey properties should be mergeable`, (t) => {
t.end()
})

test(`With clone: false, merge should not clone the target root`, (t) => {
const destination = {}
const output = merge(destination, {
sup: true,
}, { clone: false })

t.equal(destination, output)
t.end()
})
// test(`With mergeWithTarget=true, merge should mutate the root target`, (t) => {
// const destination = {}
// const output = merge(destination, {
// sup: true,
// }, { mergeWithTarget: true })

// t.notEqual(destination, {})
// t.equal(destination, output)
// t.end()
// })

0 comments on commit 0a5a3c3

Please sign in to comment.