Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#185 change customMerge signature for merging empty values #205

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 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 @@ -64,11 +64,10 @@ function mergeObject(target, source, 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)
}
})
destination[key] = getMergeFunction(key, options, target[key], source[key])(target[key], source[key], options);
} else
destination[key] = cloneUnlessOtherwiseSpecified(source[key], options);
});
return destination
}

Expand Down Expand Up @@ -103,4 +102,8 @@ deepmerge.all = function deepmergeAll(array, options) {
}, {})
}

deepmerge.customMergeIgnoreEmptyValues = (key, target, source) => !target || target === ''
? () => source
: () => target;

module.exports = deepmerge
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: merge.customMergeIgnoreEmptyValues,
});

t.deepEqual(res, { someNewVariable: 'herp',very: {nested: { thing: 'derp'}} })
t.end()
})