mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-05 01:17:50 +00:00
Bump to v3.0.0.
This commit is contained in:
64
internal/baseMergeDeep.js
Normal file
64
internal/baseMergeDeep.js
Normal file
@@ -0,0 +1,64 @@
|
||||
import arrayCopy from './arrayCopy';
|
||||
import isArguments from '../lang/isArguments';
|
||||
import isArray from '../lang/isArray';
|
||||
import isLength from './isLength';
|
||||
import isPlainObject from '../lang/isPlainObject';
|
||||
import isTypedArray from '../lang/isTypedArray';
|
||||
import toPlainObject from '../lang/toPlainObject';
|
||||
|
||||
/**
|
||||
* A specialized version of `baseMerge` for arrays and objects which performs
|
||||
* deep merges and tracks traversed objects enabling objects with circular
|
||||
* references to be merged.
|
||||
*
|
||||
* @private
|
||||
* @param {Object} object The destination object.
|
||||
* @param {Object} source The source object.
|
||||
* @param {string} key The key of the value to merge.
|
||||
* @param {Function} mergeFunc The function to merge values.
|
||||
* @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 {boolean} Returns `true` if the objects are equivalent, else `false`.
|
||||
*/
|
||||
function baseMergeDeep(object, source, key, mergeFunc, customizer, stackA, stackB) {
|
||||
var length = stackA.length,
|
||||
srcValue = source[key];
|
||||
|
||||
while (length--) {
|
||||
if (stackA[length] == srcValue) {
|
||||
object[key] = stackB[length];
|
||||
return;
|
||||
}
|
||||
}
|
||||
var value = object[key],
|
||||
result = customizer ? customizer(value, srcValue, key, object, source) : undefined,
|
||||
isCommon = typeof result == 'undefined';
|
||||
|
||||
if (isCommon) {
|
||||
result = srcValue;
|
||||
if (isLength(srcValue.length) && (isArray(srcValue) || isTypedArray(srcValue))) {
|
||||
result = isArray(value)
|
||||
? value
|
||||
: (value ? arrayCopy(value) : []);
|
||||
}
|
||||
else if (isPlainObject(srcValue) || isArguments(srcValue)) {
|
||||
result = isArguments(value)
|
||||
? toPlainObject(value)
|
||||
: (isPlainObject(value) ? value : {});
|
||||
}
|
||||
}
|
||||
// Add the source value to the stack of traversed objects and associate
|
||||
// it with its merged value.
|
||||
stackA.push(srcValue);
|
||||
stackB.push(result);
|
||||
|
||||
if (isCommon) {
|
||||
// Recursively merge objects and arrays (susceptible to call stack limits).
|
||||
object[key] = mergeFunc(result, srcValue, customizer, stackA, stackB);
|
||||
} else if (result === result ? result !== value : value === value) {
|
||||
object[key] = result;
|
||||
}
|
||||
}
|
||||
|
||||
export default baseMergeDeep;
|
||||
Reference in New Issue
Block a user