mirror of
https://iceshrimp.dev/crimekillz/iceshrimp-161sh.git
synced 2024-11-24 21:19:07 +01:00
25 lines
526 B
TypeScript
25 lines
526 B
TypeScript
// structredCloneが遅いため
|
|
// SEE: http://var.blog.jp/archives/86038606.html
|
|
|
|
type Cloneable =
|
|
| string
|
|
| number
|
|
| boolean
|
|
| null
|
|
| { [key: string]: Cloneable }
|
|
| Cloneable[];
|
|
|
|
export function deepClone<T extends Cloneable>(x: T): T {
|
|
if (typeof x === "object") {
|
|
if (x === null) return x;
|
|
if (Array.isArray(x)) return x.map(deepClone) as T;
|
|
const obj = {} as Record<string, Cloneable>;
|
|
for (const [k, v] of Object.entries(x)) {
|
|
obj[k] = deepClone(v);
|
|
}
|
|
return obj as T;
|
|
} else {
|
|
return x;
|
|
}
|
|
}
|