-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Copy pathbaseMergeDeep.js
63 lines (58 loc) · 2.2 KB
/
baseMergeDeep.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import assignMergeValue from './assignMergeValue';
import baseClone from './baseClone';
import copyArray from './copyArray';
import isArguments from '../isArguments';
import isArray from '../isArray';
import isArrayLikeObject from '../isArrayLikeObject';
import isFunction from '../isFunction';
import isObject from '../isObject';
import isPlainObject from '../isPlainObject';
import isTypedArray from '../isTypedArray';
import toPlainObject from '../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 assigned values.
* @param {Object} [stack] Tracks traversed source values and their merged counterparts.
*/
function baseMergeDeep(object, source, key, mergeFunc, customizer, stack) {
var objValue = object[key],
srcValue = source[key],
stacked = stack.get(srcValue) || stack.get(objValue);
if (stacked) {
assignMergeValue(object, key, stacked);
return;
}
var newValue = customizer ? customizer(objValue, srcValue, (key + ''), object, source, stack) : undefined,
isCommon = newValue === undefined;
if (isCommon) {
newValue = srcValue;
if (isArray(srcValue) || isTypedArray(srcValue)) {
newValue = isArray(objValue)
? objValue
: ((isArrayLikeObject(objValue)) ? copyArray(objValue) : baseClone(srcValue));
}
else if (isPlainObject(srcValue) || isArguments(srcValue)) {
newValue = isArguments(objValue)
? toPlainObject(objValue)
: (isObject(objValue) ? objValue : baseClone(srcValue));
}
else {
isCommon = isFunction(srcValue);
}
}
stack.set(srcValue, newValue);
if (isCommon) {
// Recursively merge objects and arrays (susceptible to call stack limits).
mergeFunc(newValue, srcValue, customizer, stack);
}
assignMergeValue(object, key, newValue);
}
export default baseMergeDeep;