From 1ecb85e7a9dc260d0ea2d37e14255e49b0061f0d Mon Sep 17 00:00:00 2001 From: Joseph Dykstra Date: Wed, 23 Oct 2019 21:58:57 -0500 Subject: [PATCH 1/4] Avoid mutating options --- index.js | 17 ++++++++++------- test/merge.js | 9 +++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index b774ba1..23635bd 100644 --- a/index.js +++ b/index.js @@ -72,13 +72,16 @@ function mergeObject(target, source, options) { return destination } -function deepmerge(target, source, options) { - options = options || {} - options.arrayMerge = options.arrayMerge || defaultArrayMerge - options.isMergeableObject = options.isMergeableObject || defaultIsMergeableObject - // cloneUnlessOtherwiseSpecified is added to `options` so that custom arrayMerge() - // implementations can use it. The caller may not replace it. - options.cloneUnlessOtherwiseSpecified = cloneUnlessOtherwiseSpecified +function deepmerge(target, source, opts) { + var options = { + clone: opts && opts.clone, + customMerge: opts && opts.customMerge, + arrayMerge: (opts && opts.arrayMerge) || defaultArrayMerge, + isMergeableObject: (opts && opts.isMergeableObject) || defaultIsMergeableObject, + // cloneUnlessOtherwiseSpecified is added to `options` so that custom arrayMerge() + // implementations can use it. The caller may not replace it. + cloneUnlessOtherwiseSpecified: cloneUnlessOtherwiseSpecified + } var sourceIsArray = Array.isArray(source) var targetIsArray = Array.isArray(target) diff --git a/test/merge.js b/test/merge.js index fa2ac5b..84d28ae 100644 --- a/test/merge.js +++ b/test/merge.js @@ -667,3 +667,12 @@ test('Falsey properties should be mergeable', function(t) { t.ok(customMergeWasCalled, 'custom merge function was called') t.end() }) + +test('should not mutate options', function(t) { + var options = {} + + merge({}, {}, options) + + t.deepEqual(options, {}) + t.end() +}) From 948c3b39c76b5dca7ea15c72523d5df8dd46e03e Mon Sep 17 00:00:00 2001 From: TehShrike Date: Tue, 19 Jan 2021 19:51:04 -0600 Subject: [PATCH 2/4] Use object spread syntax instead of enumerating properties by hand --- index.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index aed9015..953b00d 100644 --- a/index.js +++ b/index.js @@ -70,12 +70,11 @@ const mergeObject = (target, source, options) => { return destination } -function deepmerge(target, source, opts) { +function deepmerge(target, source, inputOptions) { const options = { - clone: opts && opts.clone, - customMerge: opts && opts.customMerge, - arrayMerge: (opts && opts.arrayMerge) || defaultArrayMerge, - isMergeable: (opts && opts.isMergeable) || defaultIsMergeable, + arrayMerge: defaultArrayMerge, + isMergeable: defaultIsMergeable, + ...inputOptions, // cloneUnlessOtherwiseSpecified is added to `options` so that custom arrayMerge() // implementations can use it. The caller may not replace it. cloneUnlessOtherwiseSpecified: cloneUnlessOtherwiseSpecified From 497f7c5ac4992b9f059ed7632a18a458bc7b70ab Mon Sep 17 00:00:00 2001 From: TehShrike Date: Tue, 19 Jan 2021 19:55:29 -0600 Subject: [PATCH 3/4] Changelog for the options argument mutation --- changelog.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 29b038d..96dff46 100644 --- a/changelog.md +++ b/changelog.md @@ -1,8 +1,9 @@ # [5.0.0](https://github.com/TehShrike/deepmerge/releases/tag/v5.0.0) -- Breaking: Dropped ES5 support. [#161](https://github.com/TehShrike/deepmerge/issues/161) +- Breaking: The shipped bundle now targets ES2015 instead of ES5. If you target IE11, you'll need to change your build process to compile dependencies. [#161](https://github.com/TehShrike/deepmerge/issues/161) - Breaking: by default, only [plain objects](https://github.com/sindresorhus/is-plain-obj/#is-plain-obj-) will have their properties merged, with all other values being copied to the target. [#152](https://github.com/TehShrike/deepmerge/issues/152) - Breaking: the `isMergeableObject` option is renamed to `isMergeable` [#168](https://github.com/TehShrike/deepmerge/pull/168) +- Fixed: the options argument is no longer mutated (again) [#173](https://github.com/TehShrike/deepmerge/pull/173) # [4.2.2](https://github.com/TehShrike/deepmerge/releases/tag/v4.2.2) From 59402c135f7ece580daeaa2a86e18c9f49325bd7 Mon Sep 17 00:00:00 2001 From: TehShrike Date: Tue, 19 Jan 2021 19:56:26 -0600 Subject: [PATCH 4/4] Put the ES2015 back in --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 953b00d..2600ec5 100644 --- a/index.js +++ b/index.js @@ -70,7 +70,7 @@ const mergeObject = (target, source, options) => { return destination } -function deepmerge(target, source, inputOptions) { +const deepmerge = (target, source, inputOptions) => { const options = { arrayMerge: defaultArrayMerge, isMergeable: defaultIsMergeable,