Skip to content

Commit

Permalink
TehShrike#185 change customMerge signature to allow for ignoring null…
Browse files Browse the repository at this point in the history
… or empty string overwrites
  • Loading branch information
Hafsteinn-Ingason-Tidal committed Oct 6, 2020
1 parent 7640d50 commit 396e085
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
22 changes: 11 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
12 changes: 12 additions & 0 deletions test/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

0 comments on commit 396e085

Please sign in to comment.