Skip to content

Commit

Permalink
fix(frontend/backend): deepCloneの戻り値にundefinedを含まないように
Browse files Browse the repository at this point in the history
resolve #98
  • Loading branch information
taiyme committed Nov 5, 2023
1 parent 05507cd commit 4f7fbb6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/backend/src/misc/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
// structredCloneが遅いため
// SEE: http://var.blog.jp/archives/86038606.html

type Cloneable = string | number | boolean | null | { [key: string]: Cloneable } | Cloneable[];
type Cloneable = string | number | boolean | null | { [key: string]: Cloneable | undefined } | 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)) {
if (v === undefined) continue;
obj[k] = deepClone(v);
}
return obj as T;
Expand Down
3 changes: 2 additions & 1 deletion packages/frontend/src/scripts/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
// あと、Vue RefをIndexedDBに保存しようとしてstructredCloneを使ったらエラーになった
// https://github.com/misskey-dev/misskey/pull/8098#issuecomment-1114144045

type Cloneable = string | number | boolean | null | { [key: string]: Cloneable } | Cloneable[];
type Cloneable = string | number | boolean | null | { [key: string]: Cloneable | undefined } | 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)) {
if (v === undefined) continue;
obj[k] = deepClone(v);
}
return obj as T;
Expand Down

0 comments on commit 4f7fbb6

Please sign in to comment.