diff options
| author | tamaina <tamaina@hotmail.co.jp> | 2022-11-17 23:35:55 +0900 |
|---|---|---|
| committer | tamaina <tamaina@hotmail.co.jp> | 2022-11-17 23:35:55 +0900 |
| commit | 764da890b6ad3d53808ec592099a93d9d39d7b08 (patch) | |
| tree | b3e9b08bfafa2bbbb5f657af3adb60bcc9510b67 /packages/client/src/scripts/clone.ts | |
| parent | fix (diff) | |
| parent | Merge branch 'develop' of https://github.com/misskey-dev/misskey into develop (diff) | |
| download | misskey-764da890b6ad3d53808ec592099a93d9d39d7b08.tar.gz misskey-764da890b6ad3d53808ec592099a93d9d39d7b08.tar.bz2 misskey-764da890b6ad3d53808ec592099a93d9d39d7b08.zip | |
Merge branch 'develop' into pizzax-indexeddb
Diffstat (limited to 'packages/client/src/scripts/clone.ts')
| -rw-r--r-- | packages/client/src/scripts/clone.ts | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/packages/client/src/scripts/clone.ts b/packages/client/src/scripts/clone.ts new file mode 100644 index 0000000000..16fad24129 --- /dev/null +++ b/packages/client/src/scripts/clone.ts @@ -0,0 +1,18 @@ +// 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; + } +} |