-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Returned package-based cloning, improved cloning, removed unnecessary…
… steps
- Loading branch information
1 parent
7e7dc50
commit c9ffbe1
Showing
4 changed files
with
28 additions
and
51 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,45 +1,3 @@ | ||
export function isInt(value: number) { | ||
return value === Math.floor(value); | ||
} | ||
|
||
/** | ||
* Warning: do not use on classes - breaks instanceof, this | ||
*/ | ||
export const clone = <T>(data: T, parent?: any): T => { | ||
switch (typeof data) { | ||
case 'object': { | ||
if (data === null) return data; | ||
if (Array.isArray(data)) { | ||
return data.map((d) => clone(d)) as T; | ||
} | ||
|
||
const origData = Object.entries(data); | ||
|
||
let hasParent = false; | ||
const res: Record<string, any> = {}; | ||
for (const [key, value] of origData) { | ||
if (key === 'parent') { | ||
hasParent = true; | ||
continue; | ||
} | ||
|
||
res[key] = clone(value, res); | ||
} | ||
|
||
if (hasParent) res['parent'] = parent; | ||
|
||
return res as T; | ||
} | ||
|
||
case 'function': { | ||
return data; | ||
} | ||
|
||
case 'undefined': { | ||
return data; | ||
} | ||
|
||
default: | ||
return JSON.parse(JSON.stringify(data)); | ||
} | ||
}; |