mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-04 08:57:49 +00:00
Ensure _.merge ignores undefined values of source object properties. [closes #573]
This commit is contained in:
23
lodash.js
23
lodash.js
@@ -1962,7 +1962,8 @@
|
||||
* @returns {Object} Returns the destination object.
|
||||
*/
|
||||
function baseMerge(object, source, callback, stackA, stackB) {
|
||||
(isArrayLike(source) ? arrayEach : baseForOwn)(source, function(srcValue, key, source) {
|
||||
var isSrcArr = isArrayLike(source);
|
||||
(isSrcArr ? arrayEach : baseForOwn)(source, function(srcValue, key, source) {
|
||||
var isArr = srcValue && isArrayLike(srcValue),
|
||||
isObj = srcValue && isPlainObject(srcValue),
|
||||
value = object[key];
|
||||
@@ -1972,10 +1973,9 @@
|
||||
if (typeof result == 'undefined') {
|
||||
result = srcValue;
|
||||
}
|
||||
if (typeof result != 'undefined') {
|
||||
value = result;
|
||||
if (isSrcArr || typeof result != 'undefined') {
|
||||
object[key] = result;
|
||||
}
|
||||
object[key] = value;
|
||||
return;
|
||||
}
|
||||
// avoid merging previously merged cyclic sources
|
||||
@@ -1992,22 +1992,21 @@
|
||||
var result = callback ? callback(value, srcValue, key, object, source) : undefined,
|
||||
isShallow = typeof result != 'undefined';
|
||||
|
||||
if (isShallow) {
|
||||
value = result;
|
||||
} else {
|
||||
value = isArr
|
||||
if (!isShallow) {
|
||||
result = isArr
|
||||
? (isArray(value) ? value : [])
|
||||
: (isPlainObject(value) ? value : {});
|
||||
}
|
||||
// add `source` and associated `value` to the stack of traversed objects
|
||||
// add the source value to the stack of traversed objects
|
||||
// and associate it with its merged value
|
||||
stackA.push(srcValue);
|
||||
stackB.push(value);
|
||||
stackB.push(result);
|
||||
|
||||
// recursively merge objects and arrays (susceptible to call stack limits)
|
||||
if (!isShallow) {
|
||||
baseMerge(value, srcValue, callback, stackA, stackB);
|
||||
baseMerge(result, srcValue, callback, stackA, stackB);
|
||||
}
|
||||
object[key] = value;
|
||||
object[key] = result;
|
||||
});
|
||||
|
||||
return object;
|
||||
|
||||
Reference in New Issue
Block a user