-
Notifications
You must be signed in to change notification settings - Fork 935
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
237 additions
and
19 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
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,45 @@ | ||
// tweaked from https://github.com/Kelin2025/nanoclone/blob/0abeb7635bda9b68ef2277093f76dbe3bf3948e1/src/index.js | ||
// MIT licensed | ||
|
||
import isSchema from './isSchema'; | ||
|
||
function clone(src: unknown, seen: Map<any, any> = new Map()) { | ||
if (isSchema(src) || !src || typeof src !== 'object') return src; | ||
if (seen.has(src)) return seen.get(src); | ||
|
||
let copy: any; | ||
if (src instanceof Date) { | ||
// Date | ||
copy = new Date(src.getTime()); | ||
seen.set(src, copy); | ||
} else if (src instanceof RegExp) { | ||
// RegExp | ||
copy = new RegExp(src); | ||
seen.set(src, copy); | ||
} else if (Array.isArray(src)) { | ||
// Array | ||
copy = new Array(src.length); | ||
seen.set(src, copy); | ||
for (let i = 0; i < src.length; i++) copy[i] = clone(src[i], seen); | ||
} else if (src instanceof Map) { | ||
// Map | ||
copy = new Map(); | ||
seen.set(src, copy); | ||
for (const [k, v] of src.entries()) copy.set(k, clone(v, seen)); | ||
} else if (src instanceof Set) { | ||
// Set | ||
copy = new Set(); | ||
seen.set(src, copy); | ||
for (const v of src) copy.add(clone(v, seen)); | ||
} else if (src instanceof Object) { | ||
// Object | ||
copy = {}; | ||
seen.set(src, copy); | ||
for (const [k, v] of Object.entries(src)) copy[k] = clone(v, seen); | ||
} else { | ||
throw Error(`Unable to clone ${src}`); | ||
} | ||
return copy; | ||
} | ||
|
||
export default clone; |
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
Oops, something went wrong.