-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1162 from samchon/features/clone
Fix #1161: change `$clone()` proper.
- Loading branch information
Showing
12 changed files
with
133 additions
and
14 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
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import typia, { tags } from "typia"; | ||
|
||
import { $clone } from "typia/lib/functional/$clone"; | ||
|
||
interface IBbsGroup { | ||
id: string & tags.Format<"uuid">; | ||
code: string; | ||
name: string; | ||
articles: IBbsArticle[]; | ||
} | ||
interface IBbsArticle { | ||
id: string & tags.Format<"uuid">; | ||
title: string; | ||
content: string; | ||
files: IAttachmentFile[]; | ||
created_at: Date; | ||
updated_at: Date; | ||
deleted_at: Date | null; | ||
} | ||
interface IAttachmentFile { | ||
id: string & tags.Format<"uri">; | ||
name: string; | ||
extension: string | null; | ||
data: Uint8Array | DataView | Blob | File; | ||
} | ||
|
||
new Array(1_000).fill(null).forEach(() => { | ||
const group: IBbsGroup = { | ||
...typia.random<IBbsGroup>(), | ||
articles: new Array(10).fill(null).map(() => ({ | ||
...typia.random<IBbsArticle>(), | ||
files: new Array(10) | ||
.fill(null) | ||
.map(() => typia.random<IAttachmentFile>()), | ||
})), | ||
}; | ||
typia.assert<IBbsGroup>($clone(group)); | ||
}); |
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 |
---|---|---|
|
@@ -15,6 +15,6 @@ | |
"typescript": "^5.4.2" | ||
}, | ||
"dependencies": { | ||
"typia": "../typia-6.4.0.tgz" | ||
"typia": "../typia-6.5.0.tgz" | ||
} | ||
} |
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 |
---|---|---|
|
@@ -32,6 +32,6 @@ | |
"typescript": "^5.3.2" | ||
}, | ||
"dependencies": { | ||
"typia": "../typia-6.5.0.tgz" | ||
"typia": "../typia-6.5.1.tgz" | ||
} | ||
} |
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
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,4 +1,3 @@ | ||
import { $clone } from "./$clone"; | ||
|
||
export const $any = (val: any): any => | ||
val !== undefined ? $clone(val) : undefined; | ||
export const $any = (val: any): any => $clone(val); |
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,4 +1,48 @@ | ||
import { Primitive } from "../Primitive"; | ||
import { Resolved } from "../Resolved"; | ||
|
||
export const $clone = <T>(value: T): Primitive<T> => | ||
JSON.parse(JSON.stringify(value)); | ||
export const $clone = <T>(value: T): Resolved<T> => | ||
$cloneMain(value) as Resolved<T>; | ||
|
||
const $cloneMain = (value: any): any => { | ||
if (value === undefined) return undefined; | ||
else if (typeof value === "object") | ||
if (value === null) return null; | ||
else if (Array.isArray(value)) return value.map($cloneMain); | ||
else if (value instanceof Date) return new Date(value); | ||
else if (value instanceof Uint8Array) return new Uint8Array(value); | ||
else if (value instanceof Uint8ClampedArray) | ||
return new Uint8ClampedArray(value); | ||
else if (value instanceof Uint16Array) return new Uint16Array(value); | ||
else if (value instanceof Uint32Array) return new Uint32Array(value); | ||
else if (value instanceof BigUint64Array) return new BigUint64Array(value); | ||
else if (value instanceof Int8Array) return new Int8Array(value); | ||
else if (value instanceof Int16Array) return new Int16Array(value); | ||
else if (value instanceof Int32Array) return new Int32Array(value); | ||
else if (value instanceof BigInt64Array) return new BigInt64Array(value); | ||
else if (value instanceof Float32Array) return new Float32Array(value); | ||
else if (value instanceof Float64Array) return new Float64Array(value); | ||
else if (value instanceof ArrayBuffer) return value.slice(0); | ||
else if (value instanceof SharedArrayBuffer) return value.slice(0); | ||
else if (value instanceof DataView) | ||
return new DataView(value.buffer.slice(0)); | ||
else if (value instanceof Blob) | ||
return new Blob([value], { type: value.type }); | ||
else if (value instanceof File) | ||
return new File([value], value.name, { type: value.type }); | ||
else if (value instanceof Set) return new Set([...value].map($cloneMain)); | ||
else if (value instanceof Map) | ||
return new Map( | ||
[...value].map(([k, v]) => [$cloneMain(k), $cloneMain(v)]), | ||
); | ||
else if (value instanceof WeakSet || value instanceof WeakMap) | ||
throw new Error("WeakSet and WeakMap are not supported"); | ||
else if (value.valueOf() !== value) return $cloneMain(value.valueOf()); | ||
else | ||
return Object.fromEntries( | ||
Object.entries(value) | ||
.map(([k, v]) => [k, $cloneMain(v)]) | ||
.filter(([, v]) => v !== undefined), | ||
); | ||
else if (typeof value === "function") return undefined; | ||
return value; | ||
}; |
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 |
---|---|---|
|
@@ -36,6 +36,6 @@ | |
"typescript": "^5.4.5" | ||
}, | ||
"dependencies": { | ||
"typia": "../typia-6.5.0.tgz" | ||
"typia": "../typia-6.5.1.tgz" | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
test/src/features/issues/test_issue_1161_clone_was_wrong.ts
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import typia, { tags } from "typia"; | ||
|
||
export const test_issue_1161_clone_was_wrong = (): void => { | ||
new Array(1_000).fill(null).forEach(() => { | ||
const group: IBbsGroup = { | ||
...typia.random<IBbsGroup>(), | ||
articles: new Array(10).fill(null).map(() => ({ | ||
...typia.random<IBbsArticle>(), | ||
files: new Array(10) | ||
.fill(null) | ||
.map(() => typia.random<IAttachmentFile>()), | ||
})), | ||
}; | ||
typia.assert<IBbsGroup>(typia.misc.clone<any>(group)); | ||
}); | ||
}; | ||
|
||
interface IBbsGroup { | ||
id: string & tags.Format<"uuid">; | ||
code: string; | ||
name: string; | ||
articles: IBbsArticle[]; | ||
} | ||
interface IBbsArticle { | ||
id: string & tags.Format<"uuid">; | ||
title: string; | ||
content: string; | ||
files: IAttachmentFile[]; | ||
created_at: Date; | ||
updated_at: Date; | ||
deleted_at: Date | null; | ||
} | ||
interface IAttachmentFile { | ||
id: string & tags.Format<"uri">; | ||
name: string; | ||
extension: string | null; | ||
data: Uint8Array | DataView | Blob | File; | ||
} |
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