From 0a5a3c3bd3034d65fbe43a33d300e1262051195b Mon Sep 17 00:00:00 2001 From: Rebecca Stevens Date: Sun, 24 Jan 2021 01:32:37 +1300 Subject: [PATCH] fix: remove mutating of target with clone=false this is wanted with a new option "mergeWithTarget" --- src/deepmerge.ts | 5 +++-- test/merge-all.ts | 23 ++++++++++++----------- test/merge.ts | 19 ++++++++++--------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/deepmerge.ts b/src/deepmerge.ts index 7f744db..0aa7203 100644 --- a/src/deepmerge.ts +++ b/src/deepmerge.ts @@ -15,7 +15,8 @@ function mergeObject< T2 extends Record, O extends Options >(target: T1, source: T2, options: FullOptions): DeepMergeObjects { - const destination: any = options.clone ? emptyTarget(target) : target + // const destination: any = options.mergeWithTarget ? target : {} + const destination: any = {} if (options.isMergeable(target)) { getKeys(target).forEach( @@ -117,5 +118,5 @@ export function deepmergeAll(objects: ReadonlyArray, options?: Options): : value } - return objects.reduce((prev, next) => deepmergeImpl(prev, next, fullOptions)) + return objects.reduce((prev, next) => deepmergeImpl(prev, next, fullOptions), {}) } diff --git a/test/merge-all.ts b/test/merge-all.ts index aed5419..4d0405b 100644 --- a/test/merge-all.ts +++ b/test/merge-all.ts @@ -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() +// }) diff --git a/test/merge.ts b/test/merge.ts index 159bbc2..50b1d5e 100644 --- a/test/merge.ts +++ b/test/merge.ts @@ -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() +// })