From 396e0854acf9bdd84154d60bc7a4d2a30449dcfe Mon Sep 17 00:00:00 2001 From: Hafsteinn Date: Tue, 6 Oct 2020 12:34:30 +0200 Subject: [PATCH 1/3] #185 change customMerge signature to allow for ignoring null or empty string overwrites --- index.js | 22 +++++++++++----------- test/merge.js | 12 ++++++++++++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 0ac71be..f8d6af2 100644 --- a/index.js +++ b/index.js @@ -16,11 +16,11 @@ function defaultArrayMerge(target, source, options) { }) } -function getMergeFunction(key, options) { +function getMergeFunction(key, options, target, source) { if (!options.customMerge) { return deepmerge } - var customMerge = options.customMerge(key) + var customMerge = options.customMerge(key, target, source); return typeof customMerge === 'function' ? customMerge : deepmerge } @@ -59,16 +59,16 @@ function mergeObject(target, source, options) { }) } getKeys(source).forEach(function(key) { - if (propertyIsUnsafe(target, key)) { - return + if (propertyIsUnsafe(target, key)) { + return + } + + if (propertyIsOnObject(target, key) && options.isMergeableObject(source[key])) { + destination[key] = getMergeFunction(key, options, target[key], source[key])(target[key], source[key], options); + } else + destination[key] = cloneUnlessOtherwiseSpecified(source[key], options); } - - if (propertyIsOnObject(target, key) && options.isMergeableObject(source[key])) { - destination[key] = getMergeFunction(key, options)(target[key], source[key], options) - } else { - destination[key] = cloneUnlessOtherwiseSpecified(source[key], options) - } - }) + ); return destination } diff --git a/test/merge.js b/test/merge.js index fa2ac5b..662408e 100644 --- a/test/merge.js +++ b/test/merge.js @@ -667,3 +667,15 @@ test('Falsey properties should be mergeable', function(t) { t.ok(customMergeWasCalled, 'custom merge function was called') t.end() }) + +test('customMerge without overwriting with null or empty string', function(t) { + var src = { someNewVariable: 'herp',very: { nested: { thing: '' }} } + var target = { very: { nested: { thing: 'derp' } } }; + + var res = merge(target, src, { + customMerge: (key, target, source) => !target || target === '' ? () => source : () => target, + }); + + t.deepEqual(res, { someNewVariable: 'herp',very: {nested: { thing: 'derp'}} }) + t.end() +}) From 0b23f28e1eb2bb3af78ca24d5fe3f15da688af44 Mon Sep 17 00:00:00 2001 From: Hafsteinn Date: Tue, 6 Oct 2020 12:48:22 +0200 Subject: [PATCH 2/3] #185 add a util function customMergeIgnoreEmptyValues for handyness --- index.js | 4 ++++ test/merge.js | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index f8d6af2..0d8d622 100644 --- a/index.js +++ b/index.js @@ -103,4 +103,8 @@ deepmerge.all = function deepmergeAll(array, options) { }, {}) } +deepmerge.customMergeIgnoreEmptyValues = (key, target, source) => !target || target === '' + ? () => source + : () => target; + module.exports = deepmerge diff --git a/test/merge.js b/test/merge.js index 662408e..955d21d 100644 --- a/test/merge.js +++ b/test/merge.js @@ -673,7 +673,7 @@ test('customMerge without overwriting with null or empty string', function(t) { var target = { very: { nested: { thing: 'derp' } } }; var res = merge(target, src, { - customMerge: (key, target, source) => !target || target === '' ? () => source : () => target, + customMerge: merge.customMergeIgnoreEmptyValues, }); t.deepEqual(res, { someNewVariable: 'herp',very: {nested: { thing: 'derp'}} }) From 3d84fb1b403ee0d221d928ba6ede09fe9ed9065c Mon Sep 17 00:00:00 2001 From: Hafsteinn Date: Tue, 6 Oct 2020 12:52:07 +0200 Subject: [PATCH 3/3] #185 remove accidental indentation --- index.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 0d8d622..ed0ec53 100644 --- a/index.js +++ b/index.js @@ -59,16 +59,15 @@ function mergeObject(target, source, options) { }) } getKeys(source).forEach(function(key) { - if (propertyIsUnsafe(target, key)) { - return - } - - if (propertyIsOnObject(target, key) && options.isMergeableObject(source[key])) { - destination[key] = getMergeFunction(key, options, target[key], source[key])(target[key], source[key], options); - } else - destination[key] = cloneUnlessOtherwiseSpecified(source[key], options); + if (propertyIsUnsafe(target, key)) { + return } - ); + + if (propertyIsOnObject(target, key) && options.isMergeableObject(source[key])) { + destination[key] = getMergeFunction(key, options, target[key], source[key])(target[key], source[key], options); + } else + destination[key] = cloneUnlessOtherwiseSpecified(source[key], options); + }); return destination }