typia 7.0.0-dev.20241009-3 → 7.0.0-dev.20241010

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.
Files changed (140) hide show
  1. package/lib/factories/JsonMetadataFactory.d.ts +5 -1
  2. package/lib/factories/JsonMetadataFactory.js +4 -1
  3. package/lib/factories/JsonMetadataFactory.js.map +1 -1
  4. package/lib/internal/{$httpFormDataReadArray.js → $httpFormdataReadArray.js} +1 -1
  5. package/lib/internal/$httpFormdataReadArray.js.map +1 -0
  6. package/lib/programmers/CheckerProgrammer.js +4 -1
  7. package/lib/programmers/CheckerProgrammer.js.map +1 -1
  8. package/lib/programmers/FeatureProgrammer.d.ts +4 -1
  9. package/lib/programmers/FeatureProgrammer.js +2 -2
  10. package/lib/programmers/FeatureProgrammer.js.map +1 -1
  11. package/lib/programmers/TypiaProgrammer.js +4 -1
  12. package/lib/programmers/TypiaProgrammer.js.map +1 -1
  13. package/lib/programmers/misc/MiscCloneProgrammer.js +4 -1
  14. package/lib/programmers/misc/MiscCloneProgrammer.js.map +1 -1
  15. package/lib/programmers/misc/MiscPruneProgrammer.js +4 -1
  16. package/lib/programmers/misc/MiscPruneProgrammer.js.map +1 -1
  17. package/lib/programmers/notations/NotationGeneralProgrammer.js +4 -1
  18. package/lib/programmers/notations/NotationGeneralProgrammer.js.map +1 -1
  19. package/lib/transformers/FileTransformer.js +0 -6
  20. package/lib/transformers/FileTransformer.js.map +1 -1
  21. package/lib/transformers/ImportTransformer.d.ts +4 -1
  22. package/lib/transformers/ImportTransformer.js +34 -30
  23. package/lib/transformers/ImportTransformer.js.map +1 -1
  24. package/package.json +1 -1
  25. package/src/IRandomGenerator.ts +49 -49
  26. package/src/factories/ExpressionFactory.ts +164 -164
  27. package/src/factories/JsonMetadataFactory.ts +25 -15
  28. package/src/factories/LiteralFactory.ts +50 -50
  29. package/src/factories/MetadataCommentTagFactory.ts +650 -650
  30. package/src/functional.ts +750 -750
  31. package/src/http.ts +1042 -1042
  32. package/src/internal/$IProtobufWriter.ts +18 -18
  33. package/src/internal/$ProtobufReader.ts +194 -194
  34. package/src/internal/$ProtobufSizer.ts +151 -151
  35. package/src/internal/$ProtobufWriter.ts +145 -145
  36. package/src/internal/$accessExpressionAsString.ts +46 -46
  37. package/src/internal/$assertGuard.ts +13 -13
  38. package/src/internal/$functionalTypeGuardErrorFactory.ts +4 -4
  39. package/src/internal/$httpFormDataReadBigint.ts +18 -18
  40. package/src/internal/$httpFormDataReadBlob.ts +10 -10
  41. package/src/internal/$httpFormDataReadBoolean.ts +16 -16
  42. package/src/internal/$httpFormDataReadFile.ts +10 -10
  43. package/src/internal/$httpFormDataReadNumber.ts +15 -15
  44. package/src/internal/$httpFormDataReadString.ts +10 -10
  45. package/src/internal/{$httpFormDataReadArray.ts → $httpFormdataReadArray.ts} +4 -4
  46. package/src/internal/$httpHeaderReadBigint.ts +10 -10
  47. package/src/internal/$httpHeaderReadBoolean.ts +8 -8
  48. package/src/internal/$httpHeaderReadNumber.ts +7 -7
  49. package/src/internal/$httpParameterBigint.ts +10 -10
  50. package/src/internal/$httpParameterBoolean.ts +8 -8
  51. package/src/internal/$httpParameterNumber.ts +7 -7
  52. package/src/internal/$httpParameterString.ts +2 -2
  53. package/src/internal/$httpQueryParseURLSearchParams.ts +10 -10
  54. package/src/internal/$httpQueryReadArray.ts +4 -4
  55. package/src/internal/$httpQueryReadBigint.ts +12 -12
  56. package/src/internal/$httpQueryReadBoolean.ts +14 -14
  57. package/src/internal/$httpQueryReadNumber.ts +9 -9
  58. package/src/internal/$httpQueryReadString.ts +4 -4
  59. package/src/internal/$isBetween.ts +2 -2
  60. package/src/internal/$isBigintString.ts +8 -8
  61. package/src/internal/$jsonStringifyNumber.ts +12 -12
  62. package/src/internal/$jsonStringifyRest.ts +3 -3
  63. package/src/internal/$jsonStringifyString.ts +42 -42
  64. package/src/internal/$jsonStringifyTail.ts +2 -2
  65. package/src/internal/$llmApplicationFinalize.ts +18 -18
  66. package/src/internal/$miscCloneAny.ts +48 -48
  67. package/src/internal/$notationAny.ts +37 -37
  68. package/src/internal/$notationCamel.ts +13 -13
  69. package/src/internal/$notationPascal.ts +8 -8
  70. package/src/internal/$notationSnake.ts +43 -43
  71. package/src/internal/$randomArray.ts +21 -21
  72. package/src/internal/$randomBigint.ts +6 -6
  73. package/src/internal/$randomBoolean.ts +1 -1
  74. package/src/internal/$randomFormatByte.ts +3 -3
  75. package/src/internal/$randomFormatDate.ts +18 -18
  76. package/src/internal/$randomFormatDatetime.ts +16 -16
  77. package/src/internal/$randomFormatDuration.ts +27 -27
  78. package/src/internal/$randomFormatEmail.ts +11 -11
  79. package/src/internal/$randomFormatHostname.ts +6 -6
  80. package/src/internal/$randomFormatIdnEmail.ts +3 -3
  81. package/src/internal/$randomFormatIdnHostname.ts +3 -3
  82. package/src/internal/$randomFormatIpv4.ts +11 -11
  83. package/src/internal/$randomFormatIpv6.ts +11 -11
  84. package/src/internal/$randomFormatIri.ts +3 -3
  85. package/src/internal/$randomFormatIriReference.ts +3 -3
  86. package/src/internal/$randomFormatJsonPointer.ts +7 -7
  87. package/src/internal/$randomFormatPassword.ts +8 -8
  88. package/src/internal/$randomFormatRegex.ts +4 -4
  89. package/src/internal/$randomFormatRelativeJsonPointer.ts +8 -8
  90. package/src/internal/$randomFormatTime.ts +14 -14
  91. package/src/internal/$randomFormatUri.ts +3 -3
  92. package/src/internal/$randomFormatUriReference.ts +3 -3
  93. package/src/internal/$randomFormatUriTemplate.ts +3 -3
  94. package/src/internal/$randomFormatUrl.ts +11 -11
  95. package/src/internal/$randomFormatUuid.ts +6 -6
  96. package/src/internal/$randomInteger.ts +47 -47
  97. package/src/internal/$randomNumber.ts +74 -74
  98. package/src/internal/$randomPattern.ts +10 -10
  99. package/src/internal/$randomPick.ts +9 -9
  100. package/src/internal/$randomString.ts +24 -24
  101. package/src/internal/$throwTypeGuardError.ts +5 -5
  102. package/src/internal/$validateReport.ts +13 -13
  103. package/src/internal/private/$__notationCapitalize.ts +2 -2
  104. package/src/internal/private/$__notationUnsnake.ts +24 -24
  105. package/src/json.ts +651 -651
  106. package/src/llm.ts +178 -178
  107. package/src/misc.ts +658 -658
  108. package/src/module.ts +933 -933
  109. package/src/notations.ts +827 -827
  110. package/src/programmers/CheckerProgrammer.ts +4 -1
  111. package/src/programmers/FeatureProgrammer.ts +619 -616
  112. package/src/programmers/TypiaProgrammer.ts +4 -1
  113. package/src/programmers/functional/FunctionalAssertFunctionProgrammer.ts +153 -153
  114. package/src/programmers/functional/FunctionalAssertParametersProgrammer.ts +125 -125
  115. package/src/programmers/functional/FunctionalAssertReturnProgrammer.ts +115 -115
  116. package/src/programmers/functional/FunctionalValidateFunctionProgrammer.ts +119 -119
  117. package/src/programmers/functional/FunctionalValidateParametersProgrammer.ts +274 -274
  118. package/src/programmers/functional/FunctionalValidateReturnProgrammer.ts +135 -135
  119. package/src/programmers/helpers/FunctionProgrammer.ts +67 -67
  120. package/src/programmers/helpers/RandomJoiner.ts +168 -168
  121. package/src/programmers/helpers/StringifyJoinder.ts +115 -115
  122. package/src/programmers/helpers/disable_function_programmer_declare.ts +32 -32
  123. package/src/programmers/internal/check_dynamic_key.ts +201 -201
  124. package/src/programmers/internal/check_dynamic_properties.ts +208 -208
  125. package/src/programmers/internal/check_object.ts +75 -75
  126. package/src/programmers/internal/feature_object_entries.ts +61 -61
  127. package/src/programmers/internal/prune_object_properties.ts +71 -71
  128. package/src/programmers/llm/LlmApplicationProgrammer.ts +235 -235
  129. package/src/programmers/llm/LlmSchemaProgrammer.ts +51 -51
  130. package/src/programmers/misc/MiscCloneProgrammer.ts +4 -1
  131. package/src/programmers/misc/MiscPruneProgrammer.ts +4 -1
  132. package/src/programmers/notations/NotationGeneralProgrammer.ts +4 -1
  133. package/src/protobuf.ts +868 -868
  134. package/src/reflect.ts +57 -57
  135. package/src/transformers/FileTransformer.ts +0 -6
  136. package/src/transformers/ImportTransformer.ts +59 -44
  137. package/src/utils/NamingConvention.ts +94 -94
  138. package/src/utils/StringUtil.ts +16 -16
  139. package/lib/internal/$httpFormDataReadArray.js.map +0 -1
  140. /package/lib/internal/{$httpFormDataReadArray.d.ts → $httpFormdataReadArray.d.ts} +0 -0
@@ -1,164 +1,164 @@
1
- import ts from "typescript";
2
-
3
- import { $randomFormatUuid } from "../internal/$randomFormatUuid";
4
-
5
- export namespace ExpressionFactory {
6
- export const number = (value: number) =>
7
- value < 0
8
- ? ts.factory.createPrefixUnaryExpression(
9
- ts.SyntaxKind.MinusToken,
10
- ts.factory.createNumericLiteral(Math.abs(value)),
11
- )
12
- : ts.factory.createNumericLiteral(value);
13
-
14
- export const bigint = (value: number | bigint) =>
15
- ts.factory.createCallExpression(
16
- ts.factory.createIdentifier("BigInt"),
17
- undefined,
18
- [ts.factory.createIdentifier(value.toString())],
19
- );
20
-
21
- export const isRequired = (input: ts.Expression): ts.Expression =>
22
- ts.factory.createStrictInequality(
23
- ts.factory.createIdentifier("undefined"),
24
- input,
25
- );
26
-
27
- export const isArray = (input: ts.Expression): ts.Expression =>
28
- ts.factory.createCallExpression(
29
- ts.factory.createIdentifier("Array.isArray"),
30
- undefined,
31
- [input],
32
- );
33
-
34
- export const isObject = (props: {
35
- checkNull: boolean;
36
- checkArray: boolean;
37
- input: ts.Expression;
38
- }): ts.Expression => {
39
- const conditions: ts.Expression[] = [
40
- ts.factory.createStrictEquality(
41
- ts.factory.createStringLiteral("object"),
42
- ts.factory.createTypeOfExpression(props.input),
43
- ),
44
- ];
45
- if (props.checkNull === true)
46
- conditions.push(
47
- ts.factory.createStrictInequality(ts.factory.createNull(), props.input),
48
- );
49
- if (props.checkArray === true)
50
- conditions.push(
51
- ts.factory.createStrictEquality(
52
- ts.factory.createFalse(),
53
- ts.factory.createCallExpression(
54
- ts.factory.createIdentifier("Array.isArray"),
55
- undefined,
56
- [props.input],
57
- ),
58
- ),
59
- );
60
-
61
- return conditions.length === 1
62
- ? conditions[0]!
63
- : conditions.reduce((x, y) => ts.factory.createLogicalAnd(x, y));
64
- };
65
-
66
- export const isInstanceOf = (
67
- type: string,
68
- input: ts.Expression,
69
- ): ts.Expression => {
70
- return ts.factory.createBinaryExpression(
71
- input,
72
- ts.factory.createToken(ts.SyntaxKind.InstanceOfKeyword),
73
- ts.factory.createIdentifier(type),
74
- );
75
- };
76
-
77
- export const coalesce = (x: ts.Expression, y: ts.Expression): ts.Expression =>
78
- ts.factory.createBinaryExpression(
79
- x,
80
- ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
81
- y,
82
- );
83
-
84
- export const currying = (props: {
85
- function: ts.Expression;
86
- arguments: ts.Expression[];
87
- }): ts.CallExpression => {
88
- if (props.arguments.length === 0)
89
- return ts.factory.createCallExpression(
90
- props.function,
91
- undefined,
92
- undefined,
93
- );
94
- let prev: ts.CallExpression = ts.factory.createCallExpression(
95
- props.function,
96
- undefined,
97
- [props.arguments[0]!],
98
- );
99
- for (const param of props.arguments.slice(1))
100
- prev = ts.factory.createCallExpression(prev, undefined, [param]);
101
- return prev;
102
- };
103
-
104
- export const selfCall = (body: ts.ConciseBody) =>
105
- ts.isCallExpression(body)
106
- ? body
107
- : ts.factory.createCallExpression(
108
- ts.factory.createParenthesizedExpression(
109
- ts.factory.createArrowFunction(
110
- undefined,
111
- undefined,
112
- [],
113
- undefined,
114
- undefined,
115
- body,
116
- ),
117
- ),
118
- undefined,
119
- undefined,
120
- );
121
-
122
- export const getEscapedText = (props: {
123
- printer: ts.Printer;
124
- input: ts.Expression;
125
- }): string =>
126
- props.printer.printNode(
127
- ts.EmitHint.Expression,
128
- props.input,
129
- props.input.getSourceFile(),
130
- );
131
-
132
- export const transpile = (
133
- transformer: ts.TransformationContext,
134
- script: string,
135
- ) => {
136
- const file: ts.SourceFile = ts.createSourceFile(
137
- `${$randomFormatUuid()}.ts`,
138
- script,
139
- ts.ScriptTarget.ESNext,
140
- true,
141
- ts.ScriptKind.TS,
142
- );
143
- const statement: ts.Statement | undefined = file.statements[0];
144
- if (statement === undefined)
145
- throw new ReferenceError(
146
- "Error on ExpressionFactory.transpile(): no statement exists.",
147
- );
148
- else if (!ts.isExpressionStatement(statement))
149
- throw new TypeError(
150
- "Error on ExpressionFactory.transpile(): statement is not an expression statement.",
151
- );
152
- return (input: ts.Expression): ts.Expression => {
153
- const visitor = (node: ts.Node): ts.Node => {
154
- if (ts.isIdentifier(node) && node.text === "$input") return input;
155
- return ts.visitEachChild(
156
- (ts.factory as any).cloneNode(node),
157
- visitor,
158
- transformer,
159
- );
160
- };
161
- return visitor(statement.expression) as ts.Expression;
162
- };
163
- };
164
- }
1
+ import ts from "typescript";
2
+
3
+ import { $randomFormatUuid } from "../internal/$randomFormatUuid";
4
+
5
+ export namespace ExpressionFactory {
6
+ export const number = (value: number) =>
7
+ value < 0
8
+ ? ts.factory.createPrefixUnaryExpression(
9
+ ts.SyntaxKind.MinusToken,
10
+ ts.factory.createNumericLiteral(Math.abs(value)),
11
+ )
12
+ : ts.factory.createNumericLiteral(value);
13
+
14
+ export const bigint = (value: number | bigint) =>
15
+ ts.factory.createCallExpression(
16
+ ts.factory.createIdentifier("BigInt"),
17
+ undefined,
18
+ [ts.factory.createIdentifier(value.toString())],
19
+ );
20
+
21
+ export const isRequired = (input: ts.Expression): ts.Expression =>
22
+ ts.factory.createStrictInequality(
23
+ ts.factory.createIdentifier("undefined"),
24
+ input,
25
+ );
26
+
27
+ export const isArray = (input: ts.Expression): ts.Expression =>
28
+ ts.factory.createCallExpression(
29
+ ts.factory.createIdentifier("Array.isArray"),
30
+ undefined,
31
+ [input],
32
+ );
33
+
34
+ export const isObject = (props: {
35
+ checkNull: boolean;
36
+ checkArray: boolean;
37
+ input: ts.Expression;
38
+ }): ts.Expression => {
39
+ const conditions: ts.Expression[] = [
40
+ ts.factory.createStrictEquality(
41
+ ts.factory.createStringLiteral("object"),
42
+ ts.factory.createTypeOfExpression(props.input),
43
+ ),
44
+ ];
45
+ if (props.checkNull === true)
46
+ conditions.push(
47
+ ts.factory.createStrictInequality(ts.factory.createNull(), props.input),
48
+ );
49
+ if (props.checkArray === true)
50
+ conditions.push(
51
+ ts.factory.createStrictEquality(
52
+ ts.factory.createFalse(),
53
+ ts.factory.createCallExpression(
54
+ ts.factory.createIdentifier("Array.isArray"),
55
+ undefined,
56
+ [props.input],
57
+ ),
58
+ ),
59
+ );
60
+
61
+ return conditions.length === 1
62
+ ? conditions[0]!
63
+ : conditions.reduce((x, y) => ts.factory.createLogicalAnd(x, y));
64
+ };
65
+
66
+ export const isInstanceOf = (
67
+ type: string,
68
+ input: ts.Expression,
69
+ ): ts.Expression => {
70
+ return ts.factory.createBinaryExpression(
71
+ input,
72
+ ts.factory.createToken(ts.SyntaxKind.InstanceOfKeyword),
73
+ ts.factory.createIdentifier(type),
74
+ );
75
+ };
76
+
77
+ export const coalesce = (x: ts.Expression, y: ts.Expression): ts.Expression =>
78
+ ts.factory.createBinaryExpression(
79
+ x,
80
+ ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
81
+ y,
82
+ );
83
+
84
+ export const currying = (props: {
85
+ function: ts.Expression;
86
+ arguments: ts.Expression[];
87
+ }): ts.CallExpression => {
88
+ if (props.arguments.length === 0)
89
+ return ts.factory.createCallExpression(
90
+ props.function,
91
+ undefined,
92
+ undefined,
93
+ );
94
+ let prev: ts.CallExpression = ts.factory.createCallExpression(
95
+ props.function,
96
+ undefined,
97
+ [props.arguments[0]!],
98
+ );
99
+ for (const param of props.arguments.slice(1))
100
+ prev = ts.factory.createCallExpression(prev, undefined, [param]);
101
+ return prev;
102
+ };
103
+
104
+ export const selfCall = (body: ts.ConciseBody) =>
105
+ ts.isCallExpression(body)
106
+ ? body
107
+ : ts.factory.createCallExpression(
108
+ ts.factory.createParenthesizedExpression(
109
+ ts.factory.createArrowFunction(
110
+ undefined,
111
+ undefined,
112
+ [],
113
+ undefined,
114
+ undefined,
115
+ body,
116
+ ),
117
+ ),
118
+ undefined,
119
+ undefined,
120
+ );
121
+
122
+ export const getEscapedText = (props: {
123
+ printer: ts.Printer;
124
+ input: ts.Expression;
125
+ }): string =>
126
+ props.printer.printNode(
127
+ ts.EmitHint.Expression,
128
+ props.input,
129
+ props.input.getSourceFile(),
130
+ );
131
+
132
+ export const transpile = (
133
+ transformer: ts.TransformationContext,
134
+ script: string,
135
+ ) => {
136
+ const file: ts.SourceFile = ts.createSourceFile(
137
+ `${$randomFormatUuid()}.ts`,
138
+ script,
139
+ ts.ScriptTarget.ESNext,
140
+ true,
141
+ ts.ScriptKind.TS,
142
+ );
143
+ const statement: ts.Statement | undefined = file.statements[0];
144
+ if (statement === undefined)
145
+ throw new ReferenceError(
146
+ "Error on ExpressionFactory.transpile(): no statement exists.",
147
+ );
148
+ else if (!ts.isExpressionStatement(statement))
149
+ throw new TypeError(
150
+ "Error on ExpressionFactory.transpile(): statement is not an expression statement.",
151
+ );
152
+ return (input: ts.Expression): ts.Expression => {
153
+ const visitor = (node: ts.Node): ts.Node => {
154
+ if (ts.isIdentifier(node) && node.text === "$input") return input;
155
+ return ts.visitEachChild(
156
+ (ts.factory as any).cloneNode(node),
157
+ visitor,
158
+ transformer,
159
+ );
160
+ };
161
+ return visitor(statement.expression) as ts.Expression;
162
+ };
163
+ };
164
+ }
@@ -6,6 +6,8 @@ import { AtomicPredicator } from "../programmers/helpers/AtomicPredicator";
6
6
 
7
7
  import { TransformerError } from "../transformers/TransformerError";
8
8
 
9
+ import { ValidationPipe } from "../typings/ValidationPipe";
10
+
9
11
  import { MetadataCollection } from "./MetadataCollection";
10
12
  import { MetadataFactory } from "./MetadataFactory";
11
13
 
@@ -16,27 +18,35 @@ export namespace JsonMetadataFactory {
16
18
  transformer?: ts.TransformationContext;
17
19
  type: ts.Type;
18
20
  }
21
+ export interface IOutput {
22
+ collection: MetadataCollection;
23
+ metadata: Metadata;
24
+ }
19
25
 
20
- export const analyze = (props: IProps): [MetadataCollection, Metadata] => {
21
- const collection = new MetadataCollection();
22
- const result = MetadataFactory.analyze({
23
- checker: props.checker,
24
- transformer: props.transformer,
25
- options: {
26
- escape: true,
27
- constant: true,
28
- absorb: true,
29
- validate,
30
- },
31
- collection,
32
- type: props.type,
33
- });
26
+ export const analyze = (props: IProps): IOutput => {
27
+ const collection: MetadataCollection = new MetadataCollection();
28
+ const result: ValidationPipe<Metadata, MetadataFactory.IError> =
29
+ MetadataFactory.analyze({
30
+ checker: props.checker,
31
+ transformer: props.transformer,
32
+ options: {
33
+ escape: true,
34
+ constant: true,
35
+ absorb: true,
36
+ validate,
37
+ },
38
+ collection,
39
+ type: props.type,
40
+ });
34
41
  if (result.success === false)
35
42
  throw TransformerError.from({
36
43
  code: props.method,
37
44
  errors: result.errors,
38
45
  });
39
- return [collection, result.data];
46
+ return {
47
+ collection,
48
+ metadata: result.data,
49
+ };
40
50
  };
41
51
 
42
52
  export const validate = (meta: Metadata) => {
@@ -1,50 +1,50 @@
1
- import ts from "typescript";
2
-
3
- import { ExpressionFactory } from "./ExpressionFactory";
4
- import { IdentifierFactory } from "./IdentifierFactory";
5
-
6
- export namespace LiteralFactory {
7
- export const write = (input: any): ts.Expression => {
8
- if (input === null) return ts.factory.createNull();
9
- else if (ts.isIdentifier(input)) return input;
10
- else if (input instanceof Array) return writeArray(input);
11
- else if (typeof input === "object") return writeObject(input);
12
- else if (typeof input === "boolean") return writeBoolean(input);
13
- else if (typeof input === "bigint") return writeBigint(input);
14
- else if (typeof input === "number") return writeNumber(input);
15
- else if (typeof input === "string") return writeStrinng(input);
16
- // unreachable code
17
- else if (typeof input === "function")
18
- return ts.factory.createIdentifier("undefined");
19
- else
20
- throw new TypeError("Error on LiteralFactory.generate(): unknown type.");
21
- };
22
-
23
- const writeObject = (obj: object): ts.ObjectLiteralExpression =>
24
- ts.factory.createObjectLiteralExpression(
25
- Object.entries(obj)
26
- .filter((tuple) => tuple[1] !== undefined)
27
- .map(([key, value]) =>
28
- ts.factory.createPropertyAssignment(
29
- IdentifierFactory.identifier(key),
30
- write(value),
31
- ),
32
- ),
33
- true,
34
- );
35
-
36
- const writeArray = (array: any[]): ts.ArrayLiteralExpression =>
37
- ts.factory.createArrayLiteralExpression(array.map(write), true);
38
-
39
- const writeBoolean = (value: boolean): ts.Expression =>
40
- value ? ts.factory.createTrue() : ts.factory.createFalse();
41
-
42
- const writeNumber = (value: number): ts.Expression =>
43
- ExpressionFactory.number(value);
44
-
45
- const writeBigint = (value: bigint): ts.Expression =>
46
- ExpressionFactory.bigint(value);
47
-
48
- const writeStrinng = (value: string): ts.StringLiteral =>
49
- ts.factory.createStringLiteral(value);
50
- }
1
+ import ts from "typescript";
2
+
3
+ import { ExpressionFactory } from "./ExpressionFactory";
4
+ import { IdentifierFactory } from "./IdentifierFactory";
5
+
6
+ export namespace LiteralFactory {
7
+ export const write = (input: any): ts.Expression => {
8
+ if (input === null) return ts.factory.createNull();
9
+ else if (ts.isIdentifier(input)) return input;
10
+ else if (input instanceof Array) return writeArray(input);
11
+ else if (typeof input === "object") return writeObject(input);
12
+ else if (typeof input === "boolean") return writeBoolean(input);
13
+ else if (typeof input === "bigint") return writeBigint(input);
14
+ else if (typeof input === "number") return writeNumber(input);
15
+ else if (typeof input === "string") return writeStrinng(input);
16
+ // unreachable code
17
+ else if (typeof input === "function")
18
+ return ts.factory.createIdentifier("undefined");
19
+ else
20
+ throw new TypeError("Error on LiteralFactory.generate(): unknown type.");
21
+ };
22
+
23
+ const writeObject = (obj: object): ts.ObjectLiteralExpression =>
24
+ ts.factory.createObjectLiteralExpression(
25
+ Object.entries(obj)
26
+ .filter((tuple) => tuple[1] !== undefined)
27
+ .map(([key, value]) =>
28
+ ts.factory.createPropertyAssignment(
29
+ IdentifierFactory.identifier(key),
30
+ write(value),
31
+ ),
32
+ ),
33
+ true,
34
+ );
35
+
36
+ const writeArray = (array: any[]): ts.ArrayLiteralExpression =>
37
+ ts.factory.createArrayLiteralExpression(array.map(write), true);
38
+
39
+ const writeBoolean = (value: boolean): ts.Expression =>
40
+ value ? ts.factory.createTrue() : ts.factory.createFalse();
41
+
42
+ const writeNumber = (value: number): ts.Expression =>
43
+ ExpressionFactory.number(value);
44
+
45
+ const writeBigint = (value: bigint): ts.Expression =>
46
+ ExpressionFactory.bigint(value);
47
+
48
+ const writeStrinng = (value: string): ts.StringLiteral =>
49
+ ts.factory.createStringLiteral(value);
50
+ }