UNPKG

3.07 kBJavaScriptView Raw
1import assignMergeValue from './_assignMergeValue.js';
2import cloneBuffer from './_cloneBuffer.js';
3import cloneTypedArray from './_cloneTypedArray.js';
4import copyArray from './_copyArray.js';
5import initCloneObject from './_initCloneObject.js';
6import isArguments from './isArguments.js';
7import isArray from './isArray.js';
8import isArrayLikeObject from './isArrayLikeObject.js';
9import isBuffer from './isBuffer.js';
10import isFunction from './isFunction.js';
11import isObject from './isObject.js';
12import isPlainObject from './isPlainObject.js';
13import isTypedArray from './isTypedArray.js';
14import safeGet from './_safeGet.js';
15import toPlainObject from './toPlainObject.js';
16
17/**
18 * A specialized version of `baseMerge` for arrays and objects which performs
19 * deep merges and tracks traversed objects enabling objects with circular
20 * references to be merged.
21 *
22 * @private
23 * @param {Object} object The destination object.
24 * @param {Object} source The source object.
25 * @param {string} key The key of the value to merge.
26 * @param {number} srcIndex The index of `source`.
27 * @param {Function} mergeFunc The function to merge values.
28 * @param {Function} [customizer] The function to customize assigned values.
29 * @param {Object} [stack] Tracks traversed source values and their merged
30 * counterparts.
31 */
32function baseMergeDeep(object, source, key, srcIndex, mergeFunc, customizer, stack) {
33 var objValue = safeGet(object, key),
34 srcValue = safeGet(source, key),
35 stacked = stack.get(srcValue);
36
37 if (stacked) {
38 assignMergeValue(object, key, stacked);
39 return;
40 }
41 var newValue = customizer
42 ? customizer(objValue, srcValue, (key + ''), object, source, stack)
43 : undefined;
44
45 var isCommon = newValue === undefined;
46
47 if (isCommon) {
48 var isArr = isArray(srcValue),
49 isBuff = !isArr && isBuffer(srcValue),
50 isTyped = !isArr && !isBuff && isTypedArray(srcValue);
51
52 newValue = srcValue;
53 if (isArr || isBuff || isTyped) {
54 if (isArray(objValue)) {
55 newValue = objValue;
56 }
57 else if (isArrayLikeObject(objValue)) {
58 newValue = copyArray(objValue);
59 }
60 else if (isBuff) {
61 isCommon = false;
62 newValue = cloneBuffer(srcValue, true);
63 }
64 else if (isTyped) {
65 isCommon = false;
66 newValue = cloneTypedArray(srcValue, true);
67 }
68 else {
69 newValue = [];
70 }
71 }
72 else if (isPlainObject(srcValue) || isArguments(srcValue)) {
73 newValue = objValue;
74 if (isArguments(objValue)) {
75 newValue = toPlainObject(objValue);
76 }
77 else if (!isObject(objValue) || isFunction(objValue)) {
78 newValue = initCloneObject(srcValue);
79 }
80 }
81 else {
82 isCommon = false;
83 }
84 }
85 if (isCommon) {
86 // Recursively merge objects and arrays (susceptible to call stack limits).
87 stack.set(srcValue, newValue);
88 mergeFunc(newValue, srcValue, srcIndex, customizer, stack);
89 stack['delete'](srcValue);
90 }
91 assignMergeValue(object, key, newValue);
92}
93
94export default baseMergeDeep;