typia 4.2.0 → 4.2.1-dev.20230808

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,124 +1,124 @@
1
- import ts from "typescript";
2
-
3
- export namespace TypeFactory {
4
- export const resolve =
5
- (checker: ts.TypeChecker) =>
6
- (type: ts.Type): ts.Type | null =>
7
- getReturnType(checker)(type)("toJSON");
8
-
9
- export const isFunction = (type: ts.Type): boolean =>
10
- getFunction(type) !== null;
11
-
12
- const getFunction = (type: ts.Type) => {
13
- const node = type.symbol?.declarations?.[0];
14
- if (node === undefined) return null;
15
-
16
- return ts.isFunctionLike(node)
17
- ? node
18
- : ts.isPropertyAssignment(node) || ts.isPropertyDeclaration(node)
19
- ? ts.isFunctionLike(node.initializer)
20
- ? node.initializer
21
- : null
22
- : null;
23
- };
24
-
25
- export const getReturnType =
26
- (checker: ts.TypeChecker) =>
27
- (type: ts.Type) =>
28
- (name: string): ts.Type | null => {
29
- // FIND TO-JSON METHOD
30
- const symbol: ts.Symbol | undefined = type.getProperty(name);
31
- if (!symbol) return null;
32
- else if (!symbol.valueDeclaration) return null;
33
-
34
- // GET FUNCTION DECLARATION
35
- const functor: ts.Type = checker.getTypeOfSymbolAtLocation(
36
- symbol,
37
- symbol.valueDeclaration,
38
- );
39
-
40
- // RETURNS THE RETURN-TYPE
41
- const signature: ts.Signature | undefined =
42
- checker.getSignaturesOfType(functor, ts.SignatureKind.Call)[0];
43
- return signature ? signature.getReturnType() : null;
44
- };
45
-
46
- export const getFullName =
47
- (checker: ts.TypeChecker) =>
48
- (type: ts.Type, symbol?: ts.Symbol): string => {
49
- // PRIMITIVE
50
- symbol ??= type.aliasSymbol ?? type.getSymbol();
51
- if (symbol === undefined) return checker.typeToString(type);
52
-
53
- // UNION OR INTERSECT
54
- if (
55
- type.aliasSymbol === undefined &&
56
- type.isUnionOrIntersection()
57
- ) {
58
- const joiner: string = type.isIntersection() ? " & " : " | ";
59
- return type.types
60
- .map((child) => getFullName(checker)(child))
61
- .join(joiner);
62
- }
63
-
64
- //----
65
- // SPECIALIZATION
66
- //----
67
- const name: string = get_name(symbol);
68
-
69
- // CHECK GENERIC
70
- const generic: readonly ts.Type[] = type.aliasSymbol
71
- ? type.aliasTypeArguments || []
72
- : checker.getTypeArguments(type as ts.TypeReference);
73
- return generic.length
74
- ? name === "Promise"
75
- ? getFullName(checker)(generic[0]!)
76
- : `${name}<${generic
77
- .map((child) => getFullName(checker)(child))
78
- .join(", ")}>`
79
- : name;
80
- };
81
-
82
- const explore_name =
83
- (decl: ts.Node) =>
84
- (name: string): string =>
85
- ts.isModuleBlock(decl)
86
- ? explore_name(decl.parent.parent)(
87
- `${decl.parent.name.getFullText().trim()}.${name}`,
88
- )
89
- : name;
90
-
91
- const get_name = (symbol: ts.Symbol): string => {
92
- const parent = symbol.getDeclarations()?.[0]?.parent;
93
- return parent
94
- ? explore_name(parent)(symbol.escapedName.toString())
95
- : "__type";
96
- };
97
-
98
- export const keyword = (
99
- type:
100
- | "void"
101
- | "any"
102
- | "unknown"
103
- | "boolean"
104
- | "number"
105
- | "bigint"
106
- | "string",
107
- ) => {
108
- return ts.factory.createKeywordTypeNode(
109
- type === "void"
110
- ? ts.SyntaxKind.VoidKeyword
111
- : type === "any"
112
- ? ts.SyntaxKind.AnyKeyword
113
- : type === "unknown"
114
- ? ts.SyntaxKind.UnknownKeyword
115
- : type === "boolean"
116
- ? ts.SyntaxKind.BooleanKeyword
117
- : type === "number"
118
- ? ts.SyntaxKind.NumberKeyword
119
- : type === "bigint"
120
- ? ts.SyntaxKind.BigIntKeyword
121
- : ts.SyntaxKind.StringKeyword,
122
- );
123
- };
124
- }
1
+ import ts from "typescript";
2
+
3
+ export namespace TypeFactory {
4
+ export const resolve =
5
+ (checker: ts.TypeChecker) =>
6
+ (type: ts.Type): ts.Type | null =>
7
+ getReturnType(checker)(type)("toJSON");
8
+
9
+ export const isFunction = (type: ts.Type): boolean =>
10
+ getFunction(type) !== null;
11
+
12
+ const getFunction = (type: ts.Type) => {
13
+ const node = type.symbol?.declarations?.[0];
14
+ if (node === undefined) return null;
15
+
16
+ return ts.isFunctionLike(node)
17
+ ? node
18
+ : ts.isPropertyAssignment(node) || ts.isPropertyDeclaration(node)
19
+ ? ts.isFunctionLike(node.initializer)
20
+ ? node.initializer
21
+ : null
22
+ : null;
23
+ };
24
+
25
+ export const getReturnType =
26
+ (checker: ts.TypeChecker) =>
27
+ (type: ts.Type) =>
28
+ (name: string): ts.Type | null => {
29
+ // FIND TO-JSON METHOD
30
+ const symbol: ts.Symbol | undefined = type.getProperty(name);
31
+ if (!symbol) return null;
32
+ else if (!symbol.valueDeclaration) return null;
33
+
34
+ // GET FUNCTION DECLARATION
35
+ const functor: ts.Type = checker.getTypeOfSymbolAtLocation(
36
+ symbol,
37
+ symbol.valueDeclaration,
38
+ );
39
+
40
+ // RETURNS THE RETURN-TYPE
41
+ const signature: ts.Signature | undefined =
42
+ checker.getSignaturesOfType(functor, ts.SignatureKind.Call)[0];
43
+ return signature ? signature.getReturnType() : null;
44
+ };
45
+
46
+ export const getFullName =
47
+ (checker: ts.TypeChecker) =>
48
+ (type: ts.Type, symbol?: ts.Symbol): string => {
49
+ // PRIMITIVE
50
+ symbol ??= type.aliasSymbol ?? type.getSymbol();
51
+ if (symbol === undefined) return checker.typeToString(type);
52
+
53
+ // UNION OR INTERSECT
54
+ if (
55
+ type.aliasSymbol === undefined &&
56
+ type.isUnionOrIntersection()
57
+ ) {
58
+ const joiner: string = type.isIntersection() ? " & " : " | ";
59
+ return type.types
60
+ .map((child) => getFullName(checker)(child))
61
+ .join(joiner);
62
+ }
63
+
64
+ //----
65
+ // SPECIALIZATION
66
+ //----
67
+ const name: string = get_name(symbol);
68
+
69
+ // CHECK GENERIC
70
+ const generic: readonly ts.Type[] = type.aliasSymbol
71
+ ? type.aliasTypeArguments || []
72
+ : checker.getTypeArguments(type as ts.TypeReference);
73
+ return generic.length
74
+ ? name === "Promise"
75
+ ? getFullName(checker)(generic[0]!)
76
+ : `${name}<${generic
77
+ .map((child) => getFullName(checker)(child))
78
+ .join(", ")}>`
79
+ : name;
80
+ };
81
+
82
+ const explore_name =
83
+ (decl: ts.Node) =>
84
+ (name: string): string =>
85
+ ts.isModuleBlock(decl)
86
+ ? explore_name(decl.parent.parent)(
87
+ `${decl.parent.name.getFullText().trim()}.${name}`,
88
+ )
89
+ : name;
90
+
91
+ const get_name = (symbol: ts.Symbol): string => {
92
+ const parent = symbol.getDeclarations()?.[0]?.parent;
93
+ return parent
94
+ ? explore_name(parent)(symbol.escapedName.toString())
95
+ : "__type";
96
+ };
97
+
98
+ export const keyword = (
99
+ type:
100
+ | "void"
101
+ | "any"
102
+ | "unknown"
103
+ | "boolean"
104
+ | "number"
105
+ | "bigint"
106
+ | "string",
107
+ ) => {
108
+ return ts.factory.createKeywordTypeNode(
109
+ type === "void"
110
+ ? ts.SyntaxKind.VoidKeyword
111
+ : type === "any"
112
+ ? ts.SyntaxKind.AnyKeyword
113
+ : type === "unknown"
114
+ ? ts.SyntaxKind.UnknownKeyword
115
+ : type === "boolean"
116
+ ? ts.SyntaxKind.BooleanKeyword
117
+ : type === "number"
118
+ ? ts.SyntaxKind.NumberKeyword
119
+ : type === "bigint"
120
+ ? ts.SyntaxKind.BigIntKeyword
121
+ : ts.SyntaxKind.StringKeyword,
122
+ );
123
+ };
124
+ }
@@ -1,50 +1,50 @@
1
- /**
2
- * In the past, name of `typia` was `typescript-json`, and supported
3
- * JSON serialization by wrapping `fast-json-stringify. `typescript-json` was
4
- * a helper library of `fast-json-stringify`, which can skip manual JSON schema
5
- * definition just by putting pure TypeScript type.
6
- *
7
- * This `$string` function is a part of `fast-json-stringify` at that time, and
8
- * still being used in `typia` for the string serialization.
9
- *
10
- * @internal
11
- * @reference https://github.com/fastify/fast-json-stringify/blob/master/lib/serializer.js
12
- * @blog https://dev.to/samchon/good-bye-typescript-is-ancestor-of-typia-20000x-faster-validator-49fi
13
- */
14
- export const $string = (str: string): string => {
15
- if (STR_ESCAPE.test(str) === false) return `"${str}"`;
16
-
17
- const length: number = str.length;
18
- if (length > 41) return JSON.stringify(str);
19
-
20
- let result = "";
21
- let last = -1;
22
- let point = 255;
23
-
24
- // eslint-disable-next-line
25
- for (let i = 0; i < length; ++i) {
26
- point = str.charCodeAt(i);
27
- if (point < 32) {
28
- return JSON.stringify(str);
29
- }
30
- if (point >= 0xd800 && point <= 0xdfff) {
31
- // The current character is a surrogate.
32
- return JSON.stringify(str);
33
- }
34
- if (
35
- point === 0x22 || // '"'
36
- point === 0x5c // '\'
37
- ) {
38
- last === -1 && (last = 0);
39
- result += str.slice(last, i) + "\\";
40
- last = i;
41
- }
42
- }
43
-
44
- return (
45
- (last === -1 && '"' + str + '"') || '"' + result + str.slice(last) + '"'
46
- );
47
- };
48
-
49
- const STR_ESCAPE =
50
- /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?:[^\ud800-\udbff]|^)[\udc00-\udfff]/;
1
+ /**
2
+ * In the past, name of `typia` was `typescript-json`, and supported
3
+ * JSON serialization by wrapping `fast-json-stringify. `typescript-json` was
4
+ * a helper library of `fast-json-stringify`, which can skip manual JSON schema
5
+ * definition just by putting pure TypeScript type.
6
+ *
7
+ * This `$string` function is a part of `fast-json-stringify` at that time, and
8
+ * still being used in `typia` for the string serialization.
9
+ *
10
+ * @internal
11
+ * @reference https://github.com/fastify/fast-json-stringify/blob/master/lib/serializer.js
12
+ * @blog https://dev.to/samchon/good-bye-typescript-is-ancestor-of-typia-20000x-faster-validator-49fi
13
+ */
14
+ export const $string = (str: string): string => {
15
+ if (STR_ESCAPE.test(str) === false) return `"${str}"`;
16
+
17
+ const length: number = str.length;
18
+ if (length > 41) return JSON.stringify(str);
19
+
20
+ let result = "";
21
+ let last = -1;
22
+ let point = 255;
23
+
24
+ // eslint-disable-next-line
25
+ for (let i = 0; i < length; ++i) {
26
+ point = str.charCodeAt(i);
27
+ if (point < 32) {
28
+ return JSON.stringify(str);
29
+ }
30
+ if (point >= 0xd800 && point <= 0xdfff) {
31
+ // The current character is a surrogate.
32
+ return JSON.stringify(str);
33
+ }
34
+ if (
35
+ point === 0x22 || // '"'
36
+ point === 0x5c // '\'
37
+ ) {
38
+ last === -1 && (last = 0);
39
+ result += str.slice(last, i) + "\\";
40
+ last = i;
41
+ }
42
+ }
43
+
44
+ return (
45
+ (last === -1 && '"' + str + '"') || '"' + result + str.slice(last) + '"'
46
+ );
47
+ };
48
+
49
+ const STR_ESCAPE =
50
+ /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?:[^\ud800-\udbff]|^)[\udc00-\udfff]/;