UNPKG

5.61 kBJavaScriptView Raw
1import Stack from './_Stack.js';
2import arrayEach from './_arrayEach.js';
3import assignValue from './_assignValue.js';
4import baseAssign from './_baseAssign.js';
5import baseAssignIn from './_baseAssignIn.js';
6import cloneBuffer from './_cloneBuffer.js';
7import copyArray from './_copyArray.js';
8import copySymbols from './_copySymbols.js';
9import copySymbolsIn from './_copySymbolsIn.js';
10import getAllKeys from './_getAllKeys.js';
11import getAllKeysIn from './_getAllKeysIn.js';
12import getTag from './_getTag.js';
13import initCloneArray from './_initCloneArray.js';
14import initCloneByTag from './_initCloneByTag.js';
15import initCloneObject from './_initCloneObject.js';
16import isArray from './isArray.js';
17import isBuffer from './isBuffer.js';
18import isMap from './isMap.js';
19import isObject from './isObject.js';
20import isSet from './isSet.js';
21import keys from './keys.js';
22import keysIn from './keysIn.js';
23
24/** Used to compose bitmasks for cloning. */
25var CLONE_DEEP_FLAG = 1,
26 CLONE_FLAT_FLAG = 2,
27 CLONE_SYMBOLS_FLAG = 4;
28
29/** `Object#toString` result references. */
30var argsTag = '[object Arguments]',
31 arrayTag = '[object Array]',
32 boolTag = '[object Boolean]',
33 dateTag = '[object Date]',
34 errorTag = '[object Error]',
35 funcTag = '[object Function]',
36 genTag = '[object GeneratorFunction]',
37 mapTag = '[object Map]',
38 numberTag = '[object Number]',
39 objectTag = '[object Object]',
40 regexpTag = '[object RegExp]',
41 setTag = '[object Set]',
42 stringTag = '[object String]',
43 symbolTag = '[object Symbol]',
44 weakMapTag = '[object WeakMap]';
45
46var arrayBufferTag = '[object ArrayBuffer]',
47 dataViewTag = '[object DataView]',
48 float32Tag = '[object Float32Array]',
49 float64Tag = '[object Float64Array]',
50 int8Tag = '[object Int8Array]',
51 int16Tag = '[object Int16Array]',
52 int32Tag = '[object Int32Array]',
53 uint8Tag = '[object Uint8Array]',
54 uint8ClampedTag = '[object Uint8ClampedArray]',
55 uint16Tag = '[object Uint16Array]',
56 uint32Tag = '[object Uint32Array]';
57
58/** Used to identify `toStringTag` values supported by `_.clone`. */
59var cloneableTags = {};
60cloneableTags[argsTag] = cloneableTags[arrayTag] =
61cloneableTags[arrayBufferTag] = cloneableTags[dataViewTag] =
62cloneableTags[boolTag] = cloneableTags[dateTag] =
63cloneableTags[float32Tag] = cloneableTags[float64Tag] =
64cloneableTags[int8Tag] = cloneableTags[int16Tag] =
65cloneableTags[int32Tag] = cloneableTags[mapTag] =
66cloneableTags[numberTag] = cloneableTags[objectTag] =
67cloneableTags[regexpTag] = cloneableTags[setTag] =
68cloneableTags[stringTag] = cloneableTags[symbolTag] =
69cloneableTags[uint8Tag] = cloneableTags[uint8ClampedTag] =
70cloneableTags[uint16Tag] = cloneableTags[uint32Tag] = true;
71cloneableTags[errorTag] = cloneableTags[funcTag] =
72cloneableTags[weakMapTag] = false;
73
74/**
75 * The base implementation of `_.clone` and `_.cloneDeep` which tracks
76 * traversed objects.
77 *
78 * @private
79 * @param {*} value The value to clone.
80 * @param {boolean} bitmask The bitmask flags.
81 * 1 - Deep clone
82 * 2 - Flatten inherited properties
83 * 4 - Clone symbols
84 * @param {Function} [customizer] The function to customize cloning.
85 * @param {string} [key] The key of `value`.
86 * @param {Object} [object] The parent object of `value`.
87 * @param {Object} [stack] Tracks traversed objects and their clone counterparts.
88 * @returns {*} Returns the cloned value.
89 */
90function baseClone(value, bitmask, customizer, key, object, stack) {
91 var result,
92 isDeep = bitmask & CLONE_DEEP_FLAG,
93 isFlat = bitmask & CLONE_FLAT_FLAG,
94 isFull = bitmask & CLONE_SYMBOLS_FLAG;
95
96 if (customizer) {
97 result = object ? customizer(value, key, object, stack) : customizer(value);
98 }
99 if (result !== undefined) {
100 return result;
101 }
102 if (!isObject(value)) {
103 return value;
104 }
105 var isArr = isArray(value);
106 if (isArr) {
107 result = initCloneArray(value);
108 if (!isDeep) {
109 return copyArray(value, result);
110 }
111 } else {
112 var tag = getTag(value),
113 isFunc = tag == funcTag || tag == genTag;
114
115 if (isBuffer(value)) {
116 return cloneBuffer(value, isDeep);
117 }
118 if (tag == objectTag || tag == argsTag || (isFunc && !object)) {
119 result = (isFlat || isFunc) ? {} : initCloneObject(value);
120 if (!isDeep) {
121 return isFlat
122 ? copySymbolsIn(value, baseAssignIn(result, value))
123 : copySymbols(value, baseAssign(result, value));
124 }
125 } else {
126 if (!cloneableTags[tag]) {
127 return object ? value : {};
128 }
129 result = initCloneByTag(value, tag, isDeep);
130 }
131 }
132 // Check for circular references and return its corresponding clone.
133 stack || (stack = new Stack);
134 var stacked = stack.get(value);
135 if (stacked) {
136 return stacked;
137 }
138 stack.set(value, result);
139
140 if (isSet(value)) {
141 value.forEach(function(subValue) {
142 result.add(baseClone(subValue, bitmask, customizer, subValue, value, stack));
143 });
144 } else if (isMap(value)) {
145 value.forEach(function(subValue, key) {
146 result.set(key, baseClone(subValue, bitmask, customizer, key, value, stack));
147 });
148 }
149
150 var keysFunc = isFull
151 ? (isFlat ? getAllKeysIn : getAllKeys)
152 : (isFlat ? keysIn : keys);
153
154 var props = isArr ? undefined : keysFunc(value);
155 arrayEach(props || value, function(subValue, key) {
156 if (props) {
157 key = subValue;
158 subValue = value[key];
159 }
160 // Recursively populate clone (susceptible to call stack limits).
161 assignValue(result, key, baseClone(subValue, bitmask, customizer, key, value, stack));
162 });
163 return result;
164}
165
166export default baseClone;