2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:50:11 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-11-17 01:31:07 +01:00
|
|
|
// structredCloneが遅いため
|
|
|
|
// SEE: http://var.blog.jp/archives/86038606.html
|
2023-07-04 13:21:44 +02:00
|
|
|
// あと、Vue RefをIndexedDBに保存しようとしてstructredCloneを使ったらエラーになった
|
|
|
|
// https://github.com/misskey-dev/misskey/pull/8098#issuecomment-1114144045
|
2022-11-17 01:31:07 +01:00
|
|
|
|
2024-02-06 07:03:07 +01:00
|
|
|
export type Cloneable = string | number | boolean | null | undefined | { [key: string]: Cloneable } | { [key: number]: Cloneable } | { [key: symbol]: Cloneable } | Cloneable[];
|
2022-11-17 01:31:07 +01:00
|
|
|
|
|
|
|
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;
|
2024-02-06 07:03:07 +01:00
|
|
|
const obj = {} as Record<string | number | symbol, Cloneable>;
|
2022-11-17 01:31:07 +01:00
|
|
|
for (const [k, v] of Object.entries(x)) {
|
2024-01-30 11:53:53 +01:00
|
|
|
obj[k] = v === undefined ? undefined : deepClone(v);
|
2022-11-17 01:31:07 +01:00
|
|
|
}
|
|
|
|
return obj as T;
|
|
|
|
} else {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
}
|