Skip to content

Commit

Permalink
Merge pull request #1162 from samchon/features/clone
Browse files Browse the repository at this point in the history
Fix #1161: change `$clone()` proper.
  • Loading branch information
samchon authored Jul 14, 2024
2 parents 54596c7 + c5ac612 commit 577ab21
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 14 deletions.
2 changes: 1 addition & 1 deletion benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@
"suppress-warnings": "^1.0.2",
"tstl": "^3.0.0",
"uuid": "^9.0.1",
"typia": "../typia-6.5.0.tgz"
"typia": "../typia-6.5.1.tgz"
}
}
38 changes: 38 additions & 0 deletions debug/features/clone.ts
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));
});
2 changes: 1 addition & 1 deletion debug/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"typescript": "^5.4.2"
},
"dependencies": {
"typia": "../typia-6.4.0.tgz"
"typia": "../typia-6.5.0.tgz"
}
}
2 changes: 1 addition & 1 deletion errors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"typescript": "^5.3.2"
},
"dependencies": {
"typia": "../typia-6.5.0.tgz"
"typia": "../typia-6.5.1.tgz"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typia",
"version": "6.5.0",
"version": "6.5.1",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-json/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-json",
"version": "6.5.0",
"version": "6.5.1",
"description": "Superfast runtime validators with only one line",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -63,7 +63,7 @@
},
"homepage": "https://typia.io",
"dependencies": {
"typia": "6.5.0"
"typia": "6.5.1"
},
"peerDependencies": {
"typescript": ">=4.8.0 <5.6.0"
Expand Down
3 changes: 1 addition & 2 deletions src/functional/$any.ts
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);
50 changes: 47 additions & 3 deletions src/functional/$clone.ts
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;
};
2 changes: 1 addition & 1 deletion test-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
"typescript": "^5.4.5"
},
"dependencies": {
"typia": "../typia-6.5.0.tgz"
"typia": "../typia-6.5.1.tgz"
}
}
2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@
"suppress-warnings": "^1.0.2",
"tstl": "^3.0.0",
"uuid": "^9.0.1",
"typia": "../typia-6.5.0.tgz"
"typia": "../typia-6.5.1.tgz"
}
}
38 changes: 38 additions & 0 deletions test/src/features/issues/test_issue_1161_clone_was_wrong.ts
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;
}
2 changes: 1 addition & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"tgrid": "^1.0.2",
"tstl": "^3.0.0",
"typescript": "^5.5.3",
"typia": "^6.5.0"
"typia": "^6.5.1"
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
Expand Down

0 comments on commit 577ab21

Please sign in to comment.