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 1 commit
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
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)) {
TheHaff marked this conversation as resolved.
Show resolved Hide resolved
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()
})