Skip to content

Commit 483762b

Browse files
authored
fix: replace deepCopy with a simpler function (#365)
* fix: replace `deepCopy` with a simpler function
1 parent 3fac08f commit 483762b

File tree

2 files changed

+12
-30
lines changed

2 files changed

+12
-30
lines changed

packages/multiple-select-vanilla/src/multiple-select.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function _multipleSelect(
5050
// then after it's destroyed, we'll also have to nullify the instance
5151
const msOptions = node._multipleSelect.getOptions(false);
5252
msOptions.onHardDestroy = () => delete node._multipleSelect;
53-
msOptions.onAfterHardDestroyed = () => (instances[i] = null as any);
53+
msOptions.onAfterHardDestroy = () => (instances[i] = null as any);
5454

5555
instances.push(node._multipleSelect);
5656
} catch (e) {

packages/multiple-select-vanilla/src/utils/utils.ts

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,20 @@ export function compareObjects(objectA: any, objectB: any, compareLength = false
1616
return true;
1717
}
1818

19-
/**
20-
* Create an immutable clone of an array or object
21-
* (c) 2019 Chris Ferdinandi, MIT License, https://gomakethings.com
22-
* @param {Array|Object} objectOrArray - the array or object to copy
23-
* @return {Array|Object} - the clone of the array or object
24-
*/
25-
export function deepCopy(objectOrArray: any | any[]): any | any[] {
26-
const cloneObj = () => {
27-
const clone = {}; // create new object
28-
29-
// Loop through each item in the original, recursively copy it's value and add to the clone
30-
// eslint-disable-next-line no-restricted-syntax
31-
for (const key in objectOrArray) {
32-
if (Object.prototype.hasOwnProperty.call(objectOrArray, key)) {
33-
(clone as any)[key] = deepCopy(objectOrArray[key]);
34-
}
35-
}
36-
return clone;
37-
};
38-
39-
// Create an immutable copy of an array
40-
const cloneArr = () => objectOrArray.map((item: any) => deepCopy(item));
19+
export function deepCopy(obj: any): any {
20+
if (typeof obj !== 'object' || obj === null) {
21+
return obj;
22+
}
4123

42-
// Get object type
43-
const type = Object.prototype.toString.call(objectOrArray).slice(8, -1).toLowerCase();
44-
if (type === 'object') {
45-
return cloneObj(); // if it's an object
24+
if (Array.isArray(obj)) {
25+
return obj.map(deepCopy);
4626
}
47-
if (type === 'array') {
48-
return cloneArr(); // if it's an array
27+
28+
if (typeof obj === 'function') {
29+
return obj;
4930
}
50-
return objectOrArray; // otherwise, return it as-is, could be primitive or else
31+
32+
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, deepCopy(value)]));
5133
}
5234

5335
export function isDefined(val: any) {

0 commit comments

Comments
 (0)