typia 3.8.2 → 3.8.3

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 (173) hide show
  1. package/README.md +1 -1
  2. package/lib/Primitive.d.ts +10 -10
  3. package/lib/factories/MetadataFactory.d.ts +1 -1
  4. package/lib/factories/MetadataFactory.js +1 -1
  5. package/lib/factories/MetadataFactory.js.map +1 -1
  6. package/lib/factories/internal/metadata/emplace_metadata_object.js +3 -1
  7. package/lib/factories/internal/metadata/emplace_metadata_object.js.map +1 -1
  8. package/lib/factories/internal/metadata/iterate_metadata_tuple.js +2 -0
  9. package/lib/factories/internal/metadata/iterate_metadata_tuple.js.map +1 -1
  10. package/lib/metadata/IMetadata.d.ts +1 -0
  11. package/lib/metadata/Metadata.d.ts +1 -0
  12. package/lib/metadata/Metadata.js +5 -0
  13. package/lib/metadata/Metadata.js.map +1 -1
  14. package/lib/programmers/CheckerProgrammer.js +17 -6
  15. package/lib/programmers/CheckerProgrammer.js.map +1 -1
  16. package/lib/programmers/internal/application_object.js +1 -0
  17. package/lib/programmers/internal/application_object.js.map +1 -1
  18. package/lib/programmers/internal/application_tuple.js +1 -1
  19. package/lib/programmers/internal/application_tuple.js.map +1 -1
  20. package/lib/schemas/IJsonSchema.d.ts +1 -0
  21. package/package.json +1 -1
  22. package/src/Primitive.ts +123 -120
  23. package/src/executable/TypiaGenerateWizard.ts +85 -85
  24. package/src/executable/TypiaSetupWizard.ts +133 -133
  25. package/src/executable/setup/CommandExecutor.ts +8 -8
  26. package/src/executable/setup/PackageManager.ts +71 -71
  27. package/src/executable/typia.ts +52 -52
  28. package/src/factories/CommentFactory.ts +14 -14
  29. package/src/factories/ExpressionFactory.ts +70 -70
  30. package/src/factories/IdentifierFactory.ts +63 -63
  31. package/src/factories/LiteralFactory.ts +39 -39
  32. package/src/factories/MetadataCollection.ts +121 -121
  33. package/src/factories/MetadataFactory.ts +62 -62
  34. package/src/factories/MetadataTagFactory.ts +297 -297
  35. package/src/factories/StatementFactory.ts +24 -24
  36. package/src/factories/TemplateFactory.ts +58 -58
  37. package/src/factories/TypeFactory.ts +124 -124
  38. package/src/factories/internal/metadata/MetadataHelper.ts +12 -12
  39. package/src/factories/internal/metadata/emplace_metadata_object.ts +143 -140
  40. package/src/factories/internal/metadata/iterate_metadata.ts +67 -67
  41. package/src/factories/internal/metadata/iterate_metadata_map.ts +41 -41
  42. package/src/factories/internal/metadata/iterate_metadata_native.ts +219 -219
  43. package/src/factories/internal/metadata/iterate_metadata_resolve.ts +27 -27
  44. package/src/factories/internal/metadata/iterate_metadata_set.ts +33 -33
  45. package/src/factories/internal/metadata/iterate_metadata_tuple.ts +48 -44
  46. package/src/factories/internal/metadata/iterate_metadata_union.ts +57 -57
  47. package/src/functional/$any.ts +2 -2
  48. package/src/functional/$is_between.ts +2 -2
  49. package/src/functional/$is_custom.ts +14 -14
  50. package/src/functional/$is_date.ts +3 -3
  51. package/src/functional/$is_datetime.ts +2 -2
  52. package/src/functional/$is_email.ts +4 -4
  53. package/src/functional/$is_ipv4.ts +4 -4
  54. package/src/functional/$is_ipv6.ts +4 -4
  55. package/src/functional/$is_url.ts +4 -4
  56. package/src/functional/$is_uuid.ts +4 -4
  57. package/src/functional/$join.ts +46 -46
  58. package/src/functional/$number.ts +12 -12
  59. package/src/functional/$rest.ts +3 -3
  60. package/src/functional/$string.ts +37 -37
  61. package/src/functional/$tail.ts +5 -5
  62. package/src/metadata/IMetadata.ts +1 -0
  63. package/src/metadata/Metadata.ts +539 -533
  64. package/src/metadata/MetadataConstant.ts +3 -3
  65. package/src/metadata/MetadataObject.ts +127 -127
  66. package/src/module.ts +2043 -2043
  67. package/src/programmers/ApplicationProgrammer.ts +62 -62
  68. package/src/programmers/AssertCloneProgrammer.ts +79 -79
  69. package/src/programmers/AssertParseProgrammer.ts +74 -74
  70. package/src/programmers/AssertProgrammer.ts +285 -285
  71. package/src/programmers/AssertPruneProgrammer.ts +76 -76
  72. package/src/programmers/AssertStringifyProgrammer.ts +74 -74
  73. package/src/programmers/CheckerProgrammer.ts +901 -875
  74. package/src/programmers/CloneProgrammer.ts +389 -389
  75. package/src/programmers/FeatureProgrammer.ts +500 -500
  76. package/src/programmers/IsCloneProgrammer.ts +86 -86
  77. package/src/programmers/IsParseProgrammer.ts +80 -80
  78. package/src/programmers/IsProgrammer.ts +212 -212
  79. package/src/programmers/IsPruneProgrammer.ts +81 -81
  80. package/src/programmers/IsStringifyProgrammer.ts +84 -84
  81. package/src/programmers/LiteralsProgrammer.ts +65 -65
  82. package/src/programmers/PruneProgrammer.ts +347 -347
  83. package/src/programmers/RandomProgrammer.ts +423 -423
  84. package/src/programmers/StringifyProgrammer.ts +798 -798
  85. package/src/programmers/TypiaProgrammer.ts +129 -129
  86. package/src/programmers/ValidateCloneProgrammer.ts +93 -93
  87. package/src/programmers/ValidateParseProgrammer.ts +78 -78
  88. package/src/programmers/ValidateProgrammer.ts +316 -316
  89. package/src/programmers/ValidatePruneProgrammer.ts +86 -86
  90. package/src/programmers/ValidateStringifyProgrammer.ts +92 -92
  91. package/src/programmers/helpers/CloneJoiner.ts +131 -131
  92. package/src/programmers/helpers/FunctionImporeter.ts +54 -54
  93. package/src/programmers/helpers/OptionPredicator.ts +15 -15
  94. package/src/programmers/helpers/PruneJoiner.ts +144 -144
  95. package/src/programmers/helpers/StringifyJoinder.ts +113 -113
  96. package/src/programmers/helpers/StringifyPredicator.ts +12 -12
  97. package/src/programmers/helpers/UnionExplorer.ts +275 -275
  98. package/src/programmers/helpers/UnionPredicator.ts +81 -81
  99. package/src/programmers/internal/application_array.ts +36 -36
  100. package/src/programmers/internal/application_boolean.ts +17 -17
  101. package/src/programmers/internal/application_constant.ts +30 -30
  102. package/src/programmers/internal/application_default_string.ts +33 -33
  103. package/src/programmers/internal/application_native.ts +32 -32
  104. package/src/programmers/internal/application_number.ts +73 -73
  105. package/src/programmers/internal/application_object.ts +155 -154
  106. package/src/programmers/internal/application_schema.ts +213 -213
  107. package/src/programmers/internal/application_string.ts +45 -45
  108. package/src/programmers/internal/application_templates.ts +26 -26
  109. package/src/programmers/internal/application_tuple.ts +31 -29
  110. package/src/programmers/internal/check_array.ts +30 -30
  111. package/src/programmers/internal/check_custom.ts +31 -31
  112. package/src/programmers/internal/check_dynamic_properties.ts +194 -194
  113. package/src/programmers/internal/check_everything.ts +28 -28
  114. package/src/programmers/internal/check_native.ts +21 -21
  115. package/src/programmers/internal/check_string_tags.ts +67 -67
  116. package/src/programmers/internal/check_union_array_like.ts +271 -271
  117. package/src/programmers/internal/check_union_tuple.ts +33 -33
  118. package/src/programmers/internal/random_custom.ts +29 -29
  119. package/src/programmers/internal/stringify_dynamic_properties.ts +167 -167
  120. package/src/programmers/internal/stringify_native.ts +7 -7
  121. package/src/programmers/internal/stringify_regular_properties.ts +83 -83
  122. package/src/schemas/IJsonSchema.ts +130 -129
  123. package/src/transform.ts +17 -17
  124. package/src/transformers/CallExpressionTransformer.ts +177 -177
  125. package/src/transformers/FileTransformer.ts +47 -47
  126. package/src/transformers/NodeTransformer.ts +13 -13
  127. package/src/transformers/features/miscellaneous/ApplicationTransformer.ts +114 -114
  128. package/src/transformers/features/miscellaneous/AssertCloneTransformer.ts +9 -9
  129. package/src/transformers/features/miscellaneous/AssertPruneTransformer.ts +9 -9
  130. package/src/transformers/features/miscellaneous/CloneTransformer.ts +9 -9
  131. package/src/transformers/features/miscellaneous/CreateAssertCloneTransformer.ts +9 -9
  132. package/src/transformers/features/miscellaneous/CreateAssertPruneTransformer.ts +9 -9
  133. package/src/transformers/features/miscellaneous/CreateCloneTransformer.ts +9 -9
  134. package/src/transformers/features/miscellaneous/CreateIsCloneTransformer.ts +9 -9
  135. package/src/transformers/features/miscellaneous/CreateIsPruneTransformer.ts +9 -9
  136. package/src/transformers/features/miscellaneous/CreatePruneTransformer.ts +9 -9
  137. package/src/transformers/features/miscellaneous/CreateRandomTransformer.ts +39 -39
  138. package/src/transformers/features/miscellaneous/CreateValidateCloneTransformer.ts +9 -9
  139. package/src/transformers/features/miscellaneous/CreateValidatePruneTransformer.ts +9 -9
  140. package/src/transformers/features/miscellaneous/IsCloneTransformer.ts +9 -9
  141. package/src/transformers/features/miscellaneous/IsPruneTransformer.ts +9 -9
  142. package/src/transformers/features/miscellaneous/LiteralsTransformer.ts +28 -28
  143. package/src/transformers/features/miscellaneous/MetadataTransformer.ts +52 -52
  144. package/src/transformers/features/miscellaneous/PruneTransformer.ts +9 -9
  145. package/src/transformers/features/miscellaneous/RandomTransformer.ts +42 -42
  146. package/src/transformers/features/miscellaneous/ValidateCloneTransformer.ts +9 -9
  147. package/src/transformers/features/miscellaneous/ValidatePruneTransformer.ts +9 -9
  148. package/src/transformers/features/parsers/AssertParseTransformer.ts +9 -9
  149. package/src/transformers/features/parsers/CreateAssertParseTransformer.ts +9 -9
  150. package/src/transformers/features/parsers/CreateIsParseTransformer.ts +9 -9
  151. package/src/transformers/features/parsers/CreateValidateParseTransformer.ts +9 -9
  152. package/src/transformers/features/parsers/IsParseTransformer.ts +9 -9
  153. package/src/transformers/features/parsers/ValidateParseTransformer.ts +9 -9
  154. package/src/transformers/features/stringifiers/AssertStringifyTransformer.ts +10 -10
  155. package/src/transformers/features/stringifiers/CreateAssertStringifyTransformer.ts +12 -12
  156. package/src/transformers/features/stringifiers/CreateIsStringifyTransformer.ts +9 -9
  157. package/src/transformers/features/stringifiers/CreateStringifyTransformer.ts +9 -9
  158. package/src/transformers/features/stringifiers/CreateValidateStringifyProgrammer.ts +12 -12
  159. package/src/transformers/features/stringifiers/IsStringifyTransformer.ts +9 -9
  160. package/src/transformers/features/stringifiers/StringifyTransformer.ts +9 -9
  161. package/src/transformers/features/stringifiers/ValidateStringifyTransformer.ts +10 -10
  162. package/src/transformers/features/validators/AssertTransformer.ts +11 -11
  163. package/src/transformers/features/validators/CreateAssertTransformer.ts +13 -13
  164. package/src/transformers/features/validators/CreateIsTransformer.ts +11 -11
  165. package/src/transformers/features/validators/CreateValidateTransformer.ts +13 -13
  166. package/src/transformers/features/validators/IsTransformer.ts +11 -11
  167. package/src/transformers/features/validators/ValidateTransformer.ts +11 -11
  168. package/src/transformers/internal/GenericTransformer.ts +97 -97
  169. package/src/utils/ArrayUtil.ts +44 -44
  170. package/src/utils/Escaper.ts +45 -45
  171. package/src/utils/MapUtil.ts +12 -12
  172. package/src/utils/PatternUtil.ts +30 -30
  173. package/src/utils/RandomGenerator.ts +81 -81
@@ -1,500 +1,500 @@
1
- import ts from "typescript";
2
-
3
- import { IdentifierFactory } from "../factories/IdentifierFactory";
4
- import { MetadataCollection } from "../factories/MetadataCollection";
5
- import { StatementFactory } from "../factories/StatementFactory";
6
- import { TypeFactory } from "../factories/TypeFactory";
7
- import { ValueFactory } from "../factories/ValueFactory";
8
-
9
- import { IJsDocTagInfo } from "../metadata/IJsDocTagInfo";
10
- import { IMetadataTag } from "../metadata/IMetadataTag";
11
- import { Metadata } from "../metadata/Metadata";
12
- import { MetadataObject } from "../metadata/MetadataObject";
13
-
14
- import { IProject } from "../transformers/IProject";
15
-
16
- import { CheckerProgrammer } from "./CheckerProgrammer";
17
- import { FunctionImporter } from "./helpers/FunctionImporeter";
18
- import { IExpressionEntry } from "./helpers/IExpressionEntry";
19
- import { UnionExplorer } from "./helpers/UnionExplorer";
20
- import { feature_object_entries } from "./internal/feature_object_entries";
21
-
22
- export namespace FeatureProgrammer {
23
- /* -----------------------------------------------------------
24
- PARAMETERS
25
- ----------------------------------------------------------- */
26
- export interface IConfig<Output extends ts.ConciseBody = ts.ConciseBody> {
27
- types: IConfig.ITypes;
28
-
29
- /**
30
- * Prefix name of functions for specific object types.
31
- */
32
- functors: string;
33
-
34
- /**
35
- * Prefix name of functions for union object types.
36
- */
37
- unioners: string;
38
-
39
- /**
40
- * Whether to archive access path or not.
41
- */
42
- path: boolean;
43
-
44
- /**
45
- * Whether to trace exception or not.
46
- */
47
- trace: boolean;
48
-
49
- addition?(collection: MetadataCollection): ts.Statement[];
50
-
51
- /**
52
- * Initializer of metadata.
53
- */
54
- initializer: (
55
- project: IProject,
56
- ) => (type: ts.Type) => [MetadataCollection, Metadata];
57
-
58
- /**
59
- * Decoder, station of every types.
60
- */
61
- decoder: Decoder<Metadata, Output>;
62
-
63
- /**
64
- * Object configurator.
65
- */
66
- objector: IConfig.IObjector;
67
-
68
- /**
69
- * Generator of functions for object types.
70
- */
71
- generator?: Partial<IConfig.IGenerator>;
72
- }
73
- export namespace IConfig {
74
- export interface ITypes {
75
- input: (type: ts.Type, name?: string) => ts.TypeNode;
76
- output: (type: ts.Type, name?: string) => ts.TypeNode;
77
- }
78
-
79
- export interface IObjector {
80
- /**
81
- * Type checker when union object type comes.
82
- */
83
- checker: Decoder<Metadata, ts.Expression>;
84
-
85
- /**
86
- * Decoder, function call expression generator of specific typed objects.
87
- */
88
- decoder: Decoder<MetadataObject, ts.Expression>;
89
-
90
- /**
91
- * Joiner of expressions from properties.
92
- */
93
- joiner(
94
- input: ts.Expression,
95
- entries: IExpressionEntry[],
96
- parent: MetadataObject,
97
- ): ts.ConciseBody;
98
-
99
- /**
100
- * Union type specificator.
101
- *
102
- * Expression of an algorithm specifying object type and calling
103
- * the `decoder` function of the specified object type.
104
- */
105
- unionizer: Decoder<MetadataObject[], ts.Expression>;
106
-
107
- /**
108
- * Handler of union type specification failure.
109
- *
110
- * @param value Expression of input parameter
111
- * @param expected Expected type name
112
- * @param explore Exploration info
113
- * @returns Statement of failure
114
- */
115
- failure(
116
- value: ts.Expression,
117
- expected: string,
118
- explore?: IExplore,
119
- ): ts.Statement;
120
-
121
- /**
122
- * Transformer of type checking expression by discrimination.
123
- *
124
- * When an object type has been specified by a discrimination without full
125
- * iteration, the `unionizer` will decode the object instance after
126
- * the last type checking.
127
- *
128
- * In such circumtance, you can transform the last type checking function.
129
- *
130
- * @param exp Current expression about type checking
131
- * @returns Transformed expression
132
- * @deprecated
133
- */
134
- is?(exp: ts.Expression): ts.Expression;
135
-
136
- /**
137
- * Transformer of non-undefined type checking by discrimination.
138
- *
139
- * When specifying an union type of objects, `typia` tries to find
140
- * descrimination way just by checking only one property type.
141
- * If succeeded to find the discrimination way, `typia` will check the target
142
- * property type and in the checking, non-undefined type checking would be
143
- * done.
144
- *
145
- * In such process, you can transform the non-undefined type checking.
146
- *
147
- * @param exp
148
- * @returns Transformed expression
149
- * @deprecated
150
- */
151
- required?(exp: ts.Expression): ts.Expression;
152
-
153
- /**
154
- * Conditon wrapper when unable to specify any object type.
155
- *
156
- * When failed to specify an object type through discrimination, full
157
- * iteration type checking would be happend. In such circumstance, you
158
- * can wrap the condition with additional function.
159
- *
160
- * @param condition Current condition
161
- * @returns A function wrapped current condition
162
- */
163
- full?: (
164
- condition: ts.Expression,
165
- ) => (
166
- input: ts.Expression,
167
- expected: string,
168
- explore: IExplore,
169
- ) => ts.Expression;
170
-
171
- /**
172
- * Return type.
173
- */
174
- type?: ts.TypeNode;
175
- }
176
- export interface IGenerator {
177
- /**
178
- *
179
- * @param col
180
- */
181
- functors(col: MetadataCollection): ts.VariableStatement[];
182
-
183
- /**
184
- *
185
- * @param col
186
- */
187
- unioners(col: MetadataCollection): ts.VariableStatement[];
188
- }
189
- }
190
-
191
- export interface IExplore {
192
- tracable: boolean;
193
- source: "top" | "object";
194
- from: "top" | "array" | "object";
195
- postfix: string;
196
- start?: number;
197
- }
198
-
199
- export interface Decoder<
200
- T,
201
- Output extends ts.ConciseBody = ts.ConciseBody,
202
- > {
203
- (
204
- input: ts.Expression,
205
- target: T,
206
- explore: IExplore,
207
- metaTags: IMetadataTag[],
208
- jsDocTags: ts.JSDocTagInfo[],
209
- ): Output;
210
- }
211
-
212
- /* -----------------------------------------------------------
213
- GENERATORS
214
- ----------------------------------------------------------- */
215
- export const analyze =
216
- (project: IProject) =>
217
- (config: IConfig) =>
218
- (importer: FunctionImporter) =>
219
- (type: ts.Type, name?: string) => {
220
- const [collection, meta] = config.initializer(project)(type);
221
-
222
- // ITERATE OVER ALL METADATA
223
- const output: ts.ConciseBody = config.decoder(
224
- ValueFactory.INPUT(),
225
- meta,
226
- {
227
- tracable: config.path || config.trace,
228
- source: "top",
229
- from: "top",
230
- postfix: '""',
231
- },
232
- [],
233
- [],
234
- );
235
-
236
- // RETURNS THE OPTIMAL ARROW FUNCTION
237
- const functors: ts.VariableStatement[] = (
238
- config.generator?.functors ?? write_functors(config)(importer)
239
- )(collection);
240
- const unioners: ts.VariableStatement[] = (
241
- config.generator?.unioners ?? write_unioners(config)(importer)
242
- )(collection);
243
- const added: ts.Statement[] = (config.addition ?? (() => []))(
244
- collection,
245
- );
246
-
247
- return ts.factory.createArrowFunction(
248
- undefined,
249
- undefined,
250
- PARAMETERS(config)(config.types.input(type, name))(
251
- ValueFactory.INPUT(),
252
- ),
253
- config.types.output(type, name),
254
- undefined,
255
- ts.factory.createBlock(
256
- [
257
- ...added,
258
- ...functors.filter((_, i) =>
259
- importer.hasLocal(`${config.functors}${i}`),
260
- ),
261
- ...unioners.filter((_, i) =>
262
- importer.hasLocal(`${config.unioners}${i}`),
263
- ),
264
- ...(ts.isBlock(output)
265
- ? output.statements
266
- : [ts.factory.createReturnStatement(output)]),
267
- ],
268
- true,
269
- ),
270
- );
271
- };
272
-
273
- export const write_functors =
274
- (config: IConfig) =>
275
- (importer: FunctionImporter) =>
276
- (collection: MetadataCollection) =>
277
- collection
278
- .objects()
279
- .map((obj, i) =>
280
- StatementFactory.constant(
281
- `${config.functors}${i}`,
282
- write_object(config)(importer)(obj),
283
- ),
284
- );
285
-
286
- export const write_unioners =
287
- (config: IConfig) =>
288
- (importer: FunctionImporter) =>
289
- (collection: MetadataCollection) =>
290
- collection
291
- .unions()
292
- .map((union, i) =>
293
- StatementFactory.constant(
294
- importer.useLocal(`${config.unioners}${i}`),
295
- write_union(config)(union),
296
- ),
297
- );
298
-
299
- const write_object =
300
- (config: IConfig) =>
301
- (importer: FunctionImporter) =>
302
- (obj: MetadataObject) =>
303
- ts.factory.createArrowFunction(
304
- undefined,
305
- undefined,
306
- PARAMETERS(config)(TypeFactory.keyword("any"))(
307
- ValueFactory.INPUT(),
308
- ),
309
- config.objector.type ?? TypeFactory.keyword("any"),
310
- undefined,
311
- config.objector.joiner(
312
- ts.factory.createIdentifier("input"),
313
- feature_object_entries(config)(importer)(obj)(
314
- ts.factory.createIdentifier("input"),
315
- ),
316
- obj,
317
- ),
318
- );
319
-
320
- const write_union = (config: IConfig) => {
321
- const explorer = UnionExplorer.object(config);
322
- const input = ValueFactory.INPUT();
323
-
324
- return (meta: MetadataObject[]) =>
325
- ts.factory.createArrowFunction(
326
- undefined,
327
- undefined,
328
- PARAMETERS(config)(TypeFactory.keyword("any"))(
329
- ValueFactory.INPUT(),
330
- ),
331
- TypeFactory.keyword("any"),
332
- undefined,
333
- explorer(
334
- input,
335
- meta,
336
- {
337
- tracable: config.path || config.trace,
338
- source: "object",
339
- from: "object",
340
- postfix: "",
341
- },
342
- [],
343
- [],
344
- ),
345
- );
346
- };
347
-
348
- /* -----------------------------------------------------------
349
- DECODERS
350
- ----------------------------------------------------------- */
351
- export const decode_array =
352
- (config: Pick<IConfig, "trace" | "path" | "decoder">) =>
353
- (importer: FunctionImporter) =>
354
- (
355
- combiner: (
356
- input: ts.Expression,
357
- arrow: ts.ArrowFunction,
358
- metaTags: IMetadataTag[],
359
- jsDocTags: ts.JSDocTagInfo[],
360
- ) => ts.Expression,
361
- ) => {
362
- const rand: string = importer.increment().toString();
363
- const tail =
364
- config.path || config.trace
365
- ? [
366
- IdentifierFactory.parameter(
367
- "_index" + rand,
368
- TypeFactory.keyword("number"),
369
- ),
370
- ]
371
- : [];
372
-
373
- return (
374
- input: ts.Expression,
375
- meta: Metadata,
376
- explore: IExplore,
377
- metaTags: IMetadataTag[],
378
- jsDocTags: IJsDocTagInfo[],
379
- ) => {
380
- const arrow: ts.ArrowFunction = ts.factory.createArrowFunction(
381
- undefined,
382
- undefined,
383
- [
384
- IdentifierFactory.parameter(
385
- "elem",
386
- TypeFactory.keyword("any"),
387
- ),
388
- ...tail,
389
- ],
390
- undefined,
391
- undefined,
392
- config.decoder(
393
- ValueFactory.INPUT("elem"),
394
- meta,
395
- {
396
- tracable: explore.tracable,
397
- source: explore.source,
398
- from: "array",
399
- postfix: INDEX_SYMBOL(explore.start ?? null)(
400
- explore.postfix,
401
- )(rand),
402
- },
403
- metaTags,
404
- jsDocTags,
405
- ),
406
- );
407
- return combiner(input, arrow, metaTags, jsDocTags);
408
- };
409
- };
410
-
411
- export const decode_object =
412
- (config: Pick<IConfig, "trace" | "path" | "functors">) =>
413
- (importer: FunctionImporter) =>
414
- (input: ts.Expression, obj: MetadataObject, explore: IExplore) =>
415
- ts.factory.createCallExpression(
416
- ts.factory.createIdentifier(
417
- importer.useLocal(`${config.functors}${obj.index}`),
418
- ),
419
- undefined,
420
- get_object_arguments(config)(explore)(input),
421
- );
422
-
423
- export const get_object_arguments =
424
- (config: Pick<IConfig, "path" | "trace">) =>
425
- (explore: FeatureProgrammer.IExplore) => {
426
- const tail: ts.Expression[] =
427
- config.path === false && config.trace === false
428
- ? []
429
- : config.path === true && config.trace === true
430
- ? [
431
- ts.factory.createIdentifier(
432
- explore.postfix
433
- ? `_path + ${explore.postfix}`
434
- : "_path",
435
- ),
436
- explore.source === "object"
437
- ? ts.factory.createIdentifier(
438
- `${explore.tracable} && _exceptionable`,
439
- )
440
- : explore.tracable
441
- ? ts.factory.createTrue()
442
- : ts.factory.createFalse(),
443
- ]
444
- : config.path === true
445
- ? [
446
- ts.factory.createIdentifier(
447
- explore.postfix
448
- ? `_path + ${explore.postfix}`
449
- : "_path",
450
- ),
451
- ]
452
- : [
453
- explore.source === "object"
454
- ? ts.factory.createIdentifier(
455
- `${explore.tracable} && _exceptionable`,
456
- )
457
- : explore.tracable
458
- ? ts.factory.createTrue()
459
- : ts.factory.createFalse(),
460
- ];
461
- return (input: ts.Expression) => [input, ...tail];
462
- };
463
- }
464
-
465
- const INDEX_SYMBOL =
466
- (start: number | null) => (prev: string) => (rand: string) => {
467
- const tail: string =
468
- start !== null
469
- ? `"[" + (${start} + _index${rand}) + "]"`
470
- : `"[" + _index${rand} + "]"`;
471
- if (prev === "") return tail;
472
- else if (prev[prev.length - 1] === `"`)
473
- return prev.substring(0, prev.length - 1) + tail.substring(1);
474
- return prev + ` + ${tail}`;
475
- };
476
-
477
- const PARAMETERS =
478
- (props: Pick<CheckerProgrammer.IConfig, "path" | "trace">) =>
479
- (type: ts.TypeNode) => {
480
- const tail: ts.ParameterDeclaration[] = [];
481
- if (props.path)
482
- tail.push(
483
- IdentifierFactory.parameter(
484
- "_path",
485
- TypeFactory.keyword("string"),
486
- ),
487
- );
488
- if (props.trace)
489
- tail.push(
490
- IdentifierFactory.parameter(
491
- "_exceptionable",
492
- TypeFactory.keyword("boolean"),
493
- ts.factory.createTrue(),
494
- ),
495
- );
496
- return (input: ts.Identifier) => [
497
- IdentifierFactory.parameter(input, type),
498
- ...tail,
499
- ];
500
- };
1
+ import ts from "typescript";
2
+
3
+ import { IdentifierFactory } from "../factories/IdentifierFactory";
4
+ import { MetadataCollection } from "../factories/MetadataCollection";
5
+ import { StatementFactory } from "../factories/StatementFactory";
6
+ import { TypeFactory } from "../factories/TypeFactory";
7
+ import { ValueFactory } from "../factories/ValueFactory";
8
+
9
+ import { IJsDocTagInfo } from "../metadata/IJsDocTagInfo";
10
+ import { IMetadataTag } from "../metadata/IMetadataTag";
11
+ import { Metadata } from "../metadata/Metadata";
12
+ import { MetadataObject } from "../metadata/MetadataObject";
13
+
14
+ import { IProject } from "../transformers/IProject";
15
+
16
+ import { CheckerProgrammer } from "./CheckerProgrammer";
17
+ import { FunctionImporter } from "./helpers/FunctionImporeter";
18
+ import { IExpressionEntry } from "./helpers/IExpressionEntry";
19
+ import { UnionExplorer } from "./helpers/UnionExplorer";
20
+ import { feature_object_entries } from "./internal/feature_object_entries";
21
+
22
+ export namespace FeatureProgrammer {
23
+ /* -----------------------------------------------------------
24
+ PARAMETERS
25
+ ----------------------------------------------------------- */
26
+ export interface IConfig<Output extends ts.ConciseBody = ts.ConciseBody> {
27
+ types: IConfig.ITypes;
28
+
29
+ /**
30
+ * Prefix name of functions for specific object types.
31
+ */
32
+ functors: string;
33
+
34
+ /**
35
+ * Prefix name of functions for union object types.
36
+ */
37
+ unioners: string;
38
+
39
+ /**
40
+ * Whether to archive access path or not.
41
+ */
42
+ path: boolean;
43
+
44
+ /**
45
+ * Whether to trace exception or not.
46
+ */
47
+ trace: boolean;
48
+
49
+ addition?(collection: MetadataCollection): ts.Statement[];
50
+
51
+ /**
52
+ * Initializer of metadata.
53
+ */
54
+ initializer: (
55
+ project: IProject,
56
+ ) => (type: ts.Type) => [MetadataCollection, Metadata];
57
+
58
+ /**
59
+ * Decoder, station of every types.
60
+ */
61
+ decoder: Decoder<Metadata, Output>;
62
+
63
+ /**
64
+ * Object configurator.
65
+ */
66
+ objector: IConfig.IObjector;
67
+
68
+ /**
69
+ * Generator of functions for object types.
70
+ */
71
+ generator?: Partial<IConfig.IGenerator>;
72
+ }
73
+ export namespace IConfig {
74
+ export interface ITypes {
75
+ input: (type: ts.Type, name?: string) => ts.TypeNode;
76
+ output: (type: ts.Type, name?: string) => ts.TypeNode;
77
+ }
78
+
79
+ export interface IObjector {
80
+ /**
81
+ * Type checker when union object type comes.
82
+ */
83
+ checker: Decoder<Metadata, ts.Expression>;
84
+
85
+ /**
86
+ * Decoder, function call expression generator of specific typed objects.
87
+ */
88
+ decoder: Decoder<MetadataObject, ts.Expression>;
89
+
90
+ /**
91
+ * Joiner of expressions from properties.
92
+ */
93
+ joiner(
94
+ input: ts.Expression,
95
+ entries: IExpressionEntry[],
96
+ parent: MetadataObject,
97
+ ): ts.ConciseBody;
98
+
99
+ /**
100
+ * Union type specificator.
101
+ *
102
+ * Expression of an algorithm specifying object type and calling
103
+ * the `decoder` function of the specified object type.
104
+ */
105
+ unionizer: Decoder<MetadataObject[], ts.Expression>;
106
+
107
+ /**
108
+ * Handler of union type specification failure.
109
+ *
110
+ * @param value Expression of input parameter
111
+ * @param expected Expected type name
112
+ * @param explore Exploration info
113
+ * @returns Statement of failure
114
+ */
115
+ failure(
116
+ value: ts.Expression,
117
+ expected: string,
118
+ explore?: IExplore,
119
+ ): ts.Statement;
120
+
121
+ /**
122
+ * Transformer of type checking expression by discrimination.
123
+ *
124
+ * When an object type has been specified by a discrimination without full
125
+ * iteration, the `unionizer` will decode the object instance after
126
+ * the last type checking.
127
+ *
128
+ * In such circumtance, you can transform the last type checking function.
129
+ *
130
+ * @param exp Current expression about type checking
131
+ * @returns Transformed expression
132
+ * @deprecated
133
+ */
134
+ is?(exp: ts.Expression): ts.Expression;
135
+
136
+ /**
137
+ * Transformer of non-undefined type checking by discrimination.
138
+ *
139
+ * When specifying an union type of objects, `typia` tries to find
140
+ * descrimination way just by checking only one property type.
141
+ * If succeeded to find the discrimination way, `typia` will check the target
142
+ * property type and in the checking, non-undefined type checking would be
143
+ * done.
144
+ *
145
+ * In such process, you can transform the non-undefined type checking.
146
+ *
147
+ * @param exp
148
+ * @returns Transformed expression
149
+ * @deprecated
150
+ */
151
+ required?(exp: ts.Expression): ts.Expression;
152
+
153
+ /**
154
+ * Conditon wrapper when unable to specify any object type.
155
+ *
156
+ * When failed to specify an object type through discrimination, full
157
+ * iteration type checking would be happend. In such circumstance, you
158
+ * can wrap the condition with additional function.
159
+ *
160
+ * @param condition Current condition
161
+ * @returns A function wrapped current condition
162
+ */
163
+ full?: (
164
+ condition: ts.Expression,
165
+ ) => (
166
+ input: ts.Expression,
167
+ expected: string,
168
+ explore: IExplore,
169
+ ) => ts.Expression;
170
+
171
+ /**
172
+ * Return type.
173
+ */
174
+ type?: ts.TypeNode;
175
+ }
176
+ export interface IGenerator {
177
+ /**
178
+ *
179
+ * @param col
180
+ */
181
+ functors(col: MetadataCollection): ts.VariableStatement[];
182
+
183
+ /**
184
+ *
185
+ * @param col
186
+ */
187
+ unioners(col: MetadataCollection): ts.VariableStatement[];
188
+ }
189
+ }
190
+
191
+ export interface IExplore {
192
+ tracable: boolean;
193
+ source: "top" | "object";
194
+ from: "top" | "array" | "object";
195
+ postfix: string;
196
+ start?: number;
197
+ }
198
+
199
+ export interface Decoder<
200
+ T,
201
+ Output extends ts.ConciseBody = ts.ConciseBody,
202
+ > {
203
+ (
204
+ input: ts.Expression,
205
+ target: T,
206
+ explore: IExplore,
207
+ metaTags: IMetadataTag[],
208
+ jsDocTags: ts.JSDocTagInfo[],
209
+ ): Output;
210
+ }
211
+
212
+ /* -----------------------------------------------------------
213
+ GENERATORS
214
+ ----------------------------------------------------------- */
215
+ export const analyze =
216
+ (project: IProject) =>
217
+ (config: IConfig) =>
218
+ (importer: FunctionImporter) =>
219
+ (type: ts.Type, name?: string) => {
220
+ const [collection, meta] = config.initializer(project)(type);
221
+
222
+ // ITERATE OVER ALL METADATA
223
+ const output: ts.ConciseBody = config.decoder(
224
+ ValueFactory.INPUT(),
225
+ meta,
226
+ {
227
+ tracable: config.path || config.trace,
228
+ source: "top",
229
+ from: "top",
230
+ postfix: '""',
231
+ },
232
+ [],
233
+ [],
234
+ );
235
+
236
+ // RETURNS THE OPTIMAL ARROW FUNCTION
237
+ const functors: ts.VariableStatement[] = (
238
+ config.generator?.functors ?? write_functors(config)(importer)
239
+ )(collection);
240
+ const unioners: ts.VariableStatement[] = (
241
+ config.generator?.unioners ?? write_unioners(config)(importer)
242
+ )(collection);
243
+ const added: ts.Statement[] = (config.addition ?? (() => []))(
244
+ collection,
245
+ );
246
+
247
+ return ts.factory.createArrowFunction(
248
+ undefined,
249
+ undefined,
250
+ PARAMETERS(config)(config.types.input(type, name))(
251
+ ValueFactory.INPUT(),
252
+ ),
253
+ config.types.output(type, name),
254
+ undefined,
255
+ ts.factory.createBlock(
256
+ [
257
+ ...added,
258
+ ...functors.filter((_, i) =>
259
+ importer.hasLocal(`${config.functors}${i}`),
260
+ ),
261
+ ...unioners.filter((_, i) =>
262
+ importer.hasLocal(`${config.unioners}${i}`),
263
+ ),
264
+ ...(ts.isBlock(output)
265
+ ? output.statements
266
+ : [ts.factory.createReturnStatement(output)]),
267
+ ],
268
+ true,
269
+ ),
270
+ );
271
+ };
272
+
273
+ export const write_functors =
274
+ (config: IConfig) =>
275
+ (importer: FunctionImporter) =>
276
+ (collection: MetadataCollection) =>
277
+ collection
278
+ .objects()
279
+ .map((obj, i) =>
280
+ StatementFactory.constant(
281
+ `${config.functors}${i}`,
282
+ write_object(config)(importer)(obj),
283
+ ),
284
+ );
285
+
286
+ export const write_unioners =
287
+ (config: IConfig) =>
288
+ (importer: FunctionImporter) =>
289
+ (collection: MetadataCollection) =>
290
+ collection
291
+ .unions()
292
+ .map((union, i) =>
293
+ StatementFactory.constant(
294
+ importer.useLocal(`${config.unioners}${i}`),
295
+ write_union(config)(union),
296
+ ),
297
+ );
298
+
299
+ const write_object =
300
+ (config: IConfig) =>
301
+ (importer: FunctionImporter) =>
302
+ (obj: MetadataObject) =>
303
+ ts.factory.createArrowFunction(
304
+ undefined,
305
+ undefined,
306
+ PARAMETERS(config)(TypeFactory.keyword("any"))(
307
+ ValueFactory.INPUT(),
308
+ ),
309
+ config.objector.type ?? TypeFactory.keyword("any"),
310
+ undefined,
311
+ config.objector.joiner(
312
+ ts.factory.createIdentifier("input"),
313
+ feature_object_entries(config)(importer)(obj)(
314
+ ts.factory.createIdentifier("input"),
315
+ ),
316
+ obj,
317
+ ),
318
+ );
319
+
320
+ const write_union = (config: IConfig) => {
321
+ const explorer = UnionExplorer.object(config);
322
+ const input = ValueFactory.INPUT();
323
+
324
+ return (meta: MetadataObject[]) =>
325
+ ts.factory.createArrowFunction(
326
+ undefined,
327
+ undefined,
328
+ PARAMETERS(config)(TypeFactory.keyword("any"))(
329
+ ValueFactory.INPUT(),
330
+ ),
331
+ TypeFactory.keyword("any"),
332
+ undefined,
333
+ explorer(
334
+ input,
335
+ meta,
336
+ {
337
+ tracable: config.path || config.trace,
338
+ source: "object",
339
+ from: "object",
340
+ postfix: "",
341
+ },
342
+ [],
343
+ [],
344
+ ),
345
+ );
346
+ };
347
+
348
+ /* -----------------------------------------------------------
349
+ DECODERS
350
+ ----------------------------------------------------------- */
351
+ export const decode_array =
352
+ (config: Pick<IConfig, "trace" | "path" | "decoder">) =>
353
+ (importer: FunctionImporter) =>
354
+ (
355
+ combiner: (
356
+ input: ts.Expression,
357
+ arrow: ts.ArrowFunction,
358
+ metaTags: IMetadataTag[],
359
+ jsDocTags: ts.JSDocTagInfo[],
360
+ ) => ts.Expression,
361
+ ) => {
362
+ const rand: string = importer.increment().toString();
363
+ const tail =
364
+ config.path || config.trace
365
+ ? [
366
+ IdentifierFactory.parameter(
367
+ "_index" + rand,
368
+ TypeFactory.keyword("number"),
369
+ ),
370
+ ]
371
+ : [];
372
+
373
+ return (
374
+ input: ts.Expression,
375
+ meta: Metadata,
376
+ explore: IExplore,
377
+ metaTags: IMetadataTag[],
378
+ jsDocTags: IJsDocTagInfo[],
379
+ ) => {
380
+ const arrow: ts.ArrowFunction = ts.factory.createArrowFunction(
381
+ undefined,
382
+ undefined,
383
+ [
384
+ IdentifierFactory.parameter(
385
+ "elem",
386
+ TypeFactory.keyword("any"),
387
+ ),
388
+ ...tail,
389
+ ],
390
+ undefined,
391
+ undefined,
392
+ config.decoder(
393
+ ValueFactory.INPUT("elem"),
394
+ meta,
395
+ {
396
+ tracable: explore.tracable,
397
+ source: explore.source,
398
+ from: "array",
399
+ postfix: INDEX_SYMBOL(explore.start ?? null)(
400
+ explore.postfix,
401
+ )(rand),
402
+ },
403
+ metaTags,
404
+ jsDocTags,
405
+ ),
406
+ );
407
+ return combiner(input, arrow, metaTags, jsDocTags);
408
+ };
409
+ };
410
+
411
+ export const decode_object =
412
+ (config: Pick<IConfig, "trace" | "path" | "functors">) =>
413
+ (importer: FunctionImporter) =>
414
+ (input: ts.Expression, obj: MetadataObject, explore: IExplore) =>
415
+ ts.factory.createCallExpression(
416
+ ts.factory.createIdentifier(
417
+ importer.useLocal(`${config.functors}${obj.index}`),
418
+ ),
419
+ undefined,
420
+ get_object_arguments(config)(explore)(input),
421
+ );
422
+
423
+ export const get_object_arguments =
424
+ (config: Pick<IConfig, "path" | "trace">) =>
425
+ (explore: FeatureProgrammer.IExplore) => {
426
+ const tail: ts.Expression[] =
427
+ config.path === false && config.trace === false
428
+ ? []
429
+ : config.path === true && config.trace === true
430
+ ? [
431
+ ts.factory.createIdentifier(
432
+ explore.postfix
433
+ ? `_path + ${explore.postfix}`
434
+ : "_path",
435
+ ),
436
+ explore.source === "object"
437
+ ? ts.factory.createIdentifier(
438
+ `${explore.tracable} && _exceptionable`,
439
+ )
440
+ : explore.tracable
441
+ ? ts.factory.createTrue()
442
+ : ts.factory.createFalse(),
443
+ ]
444
+ : config.path === true
445
+ ? [
446
+ ts.factory.createIdentifier(
447
+ explore.postfix
448
+ ? `_path + ${explore.postfix}`
449
+ : "_path",
450
+ ),
451
+ ]
452
+ : [
453
+ explore.source === "object"
454
+ ? ts.factory.createIdentifier(
455
+ `${explore.tracable} && _exceptionable`,
456
+ )
457
+ : explore.tracable
458
+ ? ts.factory.createTrue()
459
+ : ts.factory.createFalse(),
460
+ ];
461
+ return (input: ts.Expression) => [input, ...tail];
462
+ };
463
+ }
464
+
465
+ const INDEX_SYMBOL =
466
+ (start: number | null) => (prev: string) => (rand: string) => {
467
+ const tail: string =
468
+ start !== null
469
+ ? `"[" + (${start} + _index${rand}) + "]"`
470
+ : `"[" + _index${rand} + "]"`;
471
+ if (prev === "") return tail;
472
+ else if (prev[prev.length - 1] === `"`)
473
+ return prev.substring(0, prev.length - 1) + tail.substring(1);
474
+ return prev + ` + ${tail}`;
475
+ };
476
+
477
+ const PARAMETERS =
478
+ (props: Pick<CheckerProgrammer.IConfig, "path" | "trace">) =>
479
+ (type: ts.TypeNode) => {
480
+ const tail: ts.ParameterDeclaration[] = [];
481
+ if (props.path)
482
+ tail.push(
483
+ IdentifierFactory.parameter(
484
+ "_path",
485
+ TypeFactory.keyword("string"),
486
+ ),
487
+ );
488
+ if (props.trace)
489
+ tail.push(
490
+ IdentifierFactory.parameter(
491
+ "_exceptionable",
492
+ TypeFactory.keyword("boolean"),
493
+ ts.factory.createTrue(),
494
+ ),
495
+ );
496
+ return (input: ts.Identifier) => [
497
+ IdentifierFactory.parameter(input, type),
498
+ ...tail,
499
+ ];
500
+ };