Files
lodash/internal/baseMerge.js
John-David Dalton 5379c1996b Bump to v3.0.0.
2015-01-25 13:44:01 -08:00

46 lines
1.6 KiB
JavaScript

import arrayEach from './arrayEach';
import baseForOwn from './baseForOwn';
import baseMergeDeep from './baseMergeDeep';
import isArray from '../lang/isArray';
import isLength from './isLength';
import isObjectLike from './isObjectLike';
import isTypedArray from '../lang/isTypedArray';
/**
* The base implementation of `_.merge` without support for argument juggling,
* multiple sources, and `this` binding `customizer` functions.
*
* @private
* @param {Object} object The destination object.
* @param {Object} source The source object.
* @param {Function} [customizer] The function to customize merging properties.
* @param {Array} [stackA=[]] Tracks traversed source objects.
* @param {Array} [stackB=[]] Associates values with source counterparts.
* @returns {Object} Returns the destination object.
*/
function baseMerge(object, source, customizer, stackA, stackB) {
var isSrcArr = isLength(source.length) && (isArray(source) || isTypedArray(source));
(isSrcArr ? arrayEach : baseForOwn)(source, function(srcValue, key, source) {
if (isObjectLike(srcValue)) {
stackA || (stackA = []);
stackB || (stackB = []);
return baseMergeDeep(object, source, key, baseMerge, customizer, stackA, stackB);
}
var value = object[key],
result = customizer ? customizer(value, srcValue, key, object, source) : undefined,
isCommon = typeof result == 'undefined';
if (isCommon) {
result = srcValue;
}
if ((isSrcArr || typeof result != 'undefined') &&
(isCommon || (result === result ? result !== value : value === value))) {
object[key] = result;
}
});
return object;
}
export default baseMerge;