-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: fix esm build, remove lodash dependency * fix: fix esm build, remove lodash dependency
- Loading branch information
Showing
5 changed files
with
50 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Fromhttps://dev.to/salyadav/deep-clone-of-js-objects-with-circular-dependency-4if7 | ||
const isArray = (val) => { | ||
return Array.isArray(val); | ||
}; | ||
|
||
const isObject = (val) => { | ||
return {}.toString.call(val) === "[object Object]" && !isArray(val); | ||
}; | ||
|
||
export const cloneDeep = (val, history = new Set()) => { | ||
const stack = history || new Set(); | ||
|
||
if (stack.has(val)) { | ||
return val; | ||
} | ||
|
||
stack.add(val); | ||
|
||
const copyObject = (o) => { | ||
const oo = Object.create({}); | ||
for (const k in o) { | ||
oo[k] = cloneDeep(o[k], stack); | ||
} | ||
return oo; | ||
}; | ||
|
||
const copyArray = (a) => { | ||
return [...a].map((e) => { | ||
if (isArray(e)) { | ||
return copyArray(e); | ||
} else if (isObject(e)) { | ||
return copyObject(e); | ||
} | ||
return cloneDeep(e, stack); | ||
}); | ||
}; | ||
|
||
if (isArray(val)) { | ||
return copyArray(val); | ||
} | ||
|
||
if (isObject(val)) { | ||
return copyObject(val); | ||
} | ||
|
||
return val; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters