8000 Fix cloneDeep with circularly dependent Sets/Maps. [closes #3122] · lodash/lodash@e6bd8b5 · GitHub
[go: up one dir, main page]

Ski 8000 p to content

Commit e6bd8b5

Browse files
committed
Fix cloneDeep with circularly dependent Sets/Maps. [closes #3122]
1 parent d8e069c commit e6bd8b5

File tree

1 file changed

+21
-34
lines changed

1 file changed

+21
-34
lines changed

lodash.js

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,7 +2691,7 @@
26912691
if (!cloneableTags[tag]) {
26922692
return object ? value : {};
26932693
}
2694-
result = initCloneByTag(value, tag, baseClone, isDeep);
2694+
result = initCloneByTag(value, tag, isDeep);
26952695
}
26962696
}
26972697
// Check for circular references and return its corresponding clone.
@@ -2702,6 +2702,22 @@
27022702
}
27032703
stack.set(value, result);
27042704

2705+
if (isSet(value)) {
2706+
value.forEach(function(subValue) {
2707+
result.add(baseClone(subValue, bitmask, customizer, subValue, value, stack));
2708+
});
2709+
2710+
return result;
2711+
}
2712+
2713+
if (isMap(value)) {
2714+
value.forEach(function(subValue, key) {
2715+
result.set(key, baseClone(subValue, bitmask, customizer, key, value, stack));
2716+
});
2717+
2718+
return result;
2719+
}
2720+
27052721
var keysFunc = isFull
27062722
? (isFlat ? getAllKeysIn : getAllKeys)
27072723
: (isFlat ? keysIn : keys);
@@ -4565,20 +4581,6 @@
45654581
return new dataView.constructor(buffer, dataView.byteOffset, dataView.byteLength);
45664582
}
45674583

4568-
/**
4569-
* Creates a clone of `map`.
4570-
*
4571-
* @private
4572-
* @param {Object} map The map to clone.
4573-
* @param {Function} cloneFunc The function to clone values.
4574-
* @param {boolean} [isDeep] Specify a deep clone.
4575-
* @returns {Object} Returns the cloned map.
4576-
*/
4577-
function cloneMap(map, isDeep, cloneFunc) {
4578-
var array = isDeep ? cloneFunc(mapToArray(map), CLONE_DEEP_FLAG) : mapToArray(map);
4579-
return arrayReduce(array, addMapEntry, new map.constructor);
4580-
}
4581-
45824584
/**
45834585
* Creates a clone of `regexp`.
45844586
*
@@ -4592,20 +4594,6 @@
45924594
return result;
45934595
}
45944596

4595-
/**
4596-
* Creates a clone of `set`.
4597-
*
4598-
* @private
4599-
* @param {Object} set The set to clone.
4600-
* @param {Function} cloneFunc The function to clone values.
4601-
* @param {boolean} [isDeep] Specify a deep clone.
4602-
* @returns {Object} Returns the cloned set.
4603-
*/
4604-
function cloneSet(set, isDeep, cloneFunc) {
4605-
var array = isDeep ? cloneFunc(setToArray(set), CLONE_DEEP_FLAG) : setToArray(set);
4606-
return arrayReduce(array, addSetEntry, new set.constructor);
4607-
}
4608-
46094597
/**
46104598
* Creates a clone of the `symbol` object.
46114599
*
@@ -6227,16 +6215,15 @@
62276215
* Initializes an object clone based on its `toStringTag`.
62286216
*
62296217
* **Note:** This function only supports cloning values with tags of
6230-
* `Boolean`, `Date`, `Error`, `Number`, `RegExp`, or `String`.
6218+
* `Boolean`, `Date`, `Error`, `Map`, `Number`, `RegExp`, `Set`, or `String`.
62316219
*
62326220
* @private
62336221
* @param {Object} object The object to clone.
62346222
* @param {string} tag The `toStringTag` of the object to clone.
6235-
* @param {Function} cloneFunc The function to clone values.
62366223
* @param {boolean} [isDeep] Specify a deep clone.
62376224
* @returns {Object} Returns the initialized clone.
62386225
*/
6239-
function initCloneByTag(object, tag, cloneFunc, isDeep) {
6226+
function initCloneByTag(object, tag, isDeep) {
62406227
var Ctor = object.constructor;
62416228
switch (tag) {
62426229
case arrayBufferTag:
@@ -6255,7 +6242,7 @@
62556242
return cloneTypedArray(object, isDeep);
62566243

62576244
case mapTag:
6258-
return cloneMap(object, isDeep, cloneFunc);
6245+
return new Ctor;
62596246

62606247
case numberTag:
62616248
case stringTag:
@@ -6265,7 +6252,7 @@
62656252
return cloneRegExp(object);
62666253

62676254
case setTag:
6268-
return cloneSet(object, isDeep, cloneFunc);
6255+
return new Ctor;
62696256

62706257
case symbolTag:
62716258
return cloneSymbol(object);

0 commit comments

Comments
 (0)
0