typia 5.2.2 → 5.2.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 (74) hide show
  1. package/lib/Primitive.d.ts +1 -1
  2. package/lib/executable/setup/CommandExecutor.js +2 -2
  3. package/lib/executable/setup/CommandExecutor.js.map +1 -1
  4. package/package.json +1 -2
  5. package/src/CamelCase.ts +116 -116
  6. package/src/PascalCase.ts +116 -116
  7. package/src/Primitive.ts +135 -135
  8. package/src/Resolved.ts +116 -116
  9. package/src/SnakeCase.ts +156 -156
  10. package/src/executable/TypiaSetupWizard.ts +142 -142
  11. package/src/executable/setup/CommandExecutor.ts +2 -2
  12. package/src/factories/JsonMetadataFactory.ts +50 -50
  13. package/src/factories/MetadataCollection.ts +282 -282
  14. package/src/functional/$convention.ts +40 -40
  15. package/src/functional/Namespace.ts +164 -164
  16. package/src/module.ts +662 -662
  17. package/src/notations.ts +855 -855
  18. package/src/programmers/helpers/AtomicPredicator.ts +31 -31
  19. package/src/programmers/helpers/NotationJoiner.ts +146 -146
  20. package/src/programmers/misc/MiscCloneProgrammer.ts +790 -790
  21. package/src/programmers/misc/MiscPruneProgrammer.ts +552 -552
  22. package/src/programmers/notations/NotationAssertGeneralProgrammer.ts +72 -72
  23. package/src/programmers/notations/NotationGeneralProgrammer.ts +720 -720
  24. package/src/programmers/notations/NotationIsGeneralProgrammer.ts +79 -79
  25. package/src/programmers/notations/NotationValidateGeneralProgrammer.ts +88 -88
  26. package/src/transformers/CallExpressionTransformer.ts +380 -380
  27. package/src/transformers/features/json/JsonAssertParseTransformer.ts +10 -10
  28. package/src/transformers/features/json/JsonAssertStringifyTransformer.ts +10 -10
  29. package/src/transformers/features/json/JsonCreateAssertParseTransformer.ts +12 -12
  30. package/src/transformers/features/json/JsonCreateAssertStringifyTransformer.ts +12 -12
  31. package/src/transformers/features/json/JsonCreateIsParseTransformer.ts +9 -9
  32. package/src/transformers/features/json/JsonCreateIsStringifyTransformer.ts +12 -12
  33. package/src/transformers/features/json/JsonCreateStringifyTransformer.ts +9 -9
  34. package/src/transformers/features/json/JsonCreateValidateParseTransformer.ts +12 -12
  35. package/src/transformers/features/json/JsonCreateValidateStringifyProgrammer.ts +12 -12
  36. package/src/transformers/features/json/JsonIsParseTransformer.ts +9 -9
  37. package/src/transformers/features/json/JsonIsStringifyTransformer.ts +10 -10
  38. package/src/transformers/features/json/JsonStringifyTransformer.ts +9 -9
  39. package/src/transformers/features/json/JsonValidateParseTransformer.ts +10 -10
  40. package/src/transformers/features/json/JsonValidateStringifyTransformer.ts +10 -10
  41. package/src/transformers/features/misc/MiscAssertCloneTransformer.ts +10 -10
  42. package/src/transformers/features/misc/MiscAssertPruneTransformer.ts +10 -10
  43. package/src/transformers/features/misc/MiscCloneTransformer.ts +9 -9
  44. package/src/transformers/features/misc/MiscCreateAssertCloneTransformer.ts +12 -12
  45. package/src/transformers/features/misc/MiscCreateAssertPruneTransformer.ts +12 -12
  46. package/src/transformers/features/misc/MiscCreateCloneTransformer.ts +9 -9
  47. package/src/transformers/features/misc/MiscCreateIsCloneTransformer.ts +9 -9
  48. package/src/transformers/features/misc/MiscCreateIsPruneTransformer.ts +9 -9
  49. package/src/transformers/features/misc/MiscCreatePruneTransformer.ts +9 -9
  50. package/src/transformers/features/misc/MiscCreateValidateCloneTransformer.ts +12 -12
  51. package/src/transformers/features/misc/MiscCreateValidatePruneTransformer.ts +12 -12
  52. package/src/transformers/features/misc/MiscIsCloneTransformer.ts +9 -9
  53. package/src/transformers/features/misc/MiscIsPruneTransformer.ts +9 -9
  54. package/src/transformers/features/misc/MiscPruneTransformer.ts +9 -9
  55. package/src/transformers/features/misc/MiscValidateCloneTransformer.ts +10 -10
  56. package/src/transformers/features/misc/MiscValidatePruneTransformer.ts +10 -10
  57. package/src/transformers/features/notations/NotationAssertGeneralTransformer.ts +15 -15
  58. package/src/transformers/features/notations/NotationCreateAssertGeneralTransformer.ts +15 -15
  59. package/src/transformers/features/notations/NotationCreateGeneralTransformer.ts +15 -15
  60. package/src/transformers/features/notations/NotationCreateIsGeneralTransformer.ts +15 -15
  61. package/src/transformers/features/notations/NotationCreateValidateGeneralTransformer.ts +17 -17
  62. package/src/transformers/features/notations/NotationGeneralTransformer.ts +11 -11
  63. package/src/transformers/features/notations/NotationIsGeneralTransformer.ts +15 -15
  64. package/src/transformers/features/notations/NotationValidateGeneralTransformer.ts +17 -17
  65. package/src/transformers/features/protobuf/ProtobufCreateAssertDecodeTransformer.ts +12 -12
  66. package/src/transformers/features/protobuf/ProtobufCreateAssertEncodeTransformer.ts +12 -12
  67. package/src/transformers/features/protobuf/ProtobufCreateDecodeTransformer.ts +9 -9
  68. package/src/transformers/features/protobuf/ProtobufCreateEncodeTransformer.ts +9 -9
  69. package/src/transformers/features/protobuf/ProtobufCreateIsDecodeTransformer.ts +12 -12
  70. package/src/transformers/features/protobuf/ProtobufCreateIsEncodeTransformer.ts +12 -12
  71. package/src/transformers/features/protobuf/ProtobufCreateValidateDecodeTransformer.ts +12 -12
  72. package/src/transformers/features/protobuf/ProtobufCreateValidateEncodeTransformer.ts +12 -12
  73. package/src/utils/NamingConvention.ts +91 -91
  74. package/src/utils/StringUtil.ts +4 -4
@@ -1,720 +1,720 @@
1
- import ts from "typescript";
2
-
3
- import { ExpressionFactory } from "../../factories/ExpressionFactory";
4
- import { IdentifierFactory } from "../../factories/IdentifierFactory";
5
- import { MetadataCollection } from "../../factories/MetadataCollection";
6
- import { MetadataFactory } from "../../factories/MetadataFactory";
7
- import { StatementFactory } from "../../factories/StatementFactory";
8
- import { TypeFactory } from "../../factories/TypeFactory";
9
-
10
- import { Metadata } from "../../schemas/metadata/Metadata";
11
- import { MetadataArray } from "../../schemas/metadata/MetadataArray";
12
- import { MetadataTuple } from "../../schemas/metadata/MetadataTuple";
13
- import { MetadataTupleType } from "../../schemas/metadata/MetadataTupleType";
14
-
15
- import { IProject } from "../../transformers/IProject";
16
- import { TransformerError } from "../../transformers/TransformerError";
17
-
18
- import { StringUtil } from "../../utils/StringUtil";
19
-
20
- import { FeatureProgrammer } from "../FeatureProgrammer";
21
- import { IsProgrammer } from "../IsProgrammer";
22
- import { FunctionImporter } from "../helpers/FunctionImporeter";
23
- import { NotationJoiner } from "../helpers/NotationJoiner";
24
- import { UnionExplorer } from "../helpers/UnionExplorer";
25
- import { decode_union_object } from "../internal/decode_union_object";
26
- import { wrap_metadata_rest_tuple } from "../internal/wrap_metadata_rest_tuple";
27
-
28
- export namespace NotationGeneralProgrammer {
29
- export const returnType =
30
- (rename: (str: string) => string) => (type: string) =>
31
- `typia.${StringUtil.capitalize(rename.name)}Case<${type}>`;
32
-
33
- export const write =
34
- (rename: (str: string) => string) =>
35
- (project: IProject) =>
36
- (modulo: ts.LeftHandSideExpression) => {
37
- const importer: FunctionImporter = new FunctionImporter(
38
- modulo.getText(),
39
- );
40
- return FeatureProgrammer.write(project)({
41
- ...configure(rename)(project)(importer),
42
- addition: (collection) => [
43
- ...IsProgrammer.write_function_statements(project)(
44
- importer,
45
- )(collection),
46
- ...importer.declare(modulo),
47
- ],
48
- })(importer);
49
- };
50
-
51
- const write_array_functions =
52
- (config: FeatureProgrammer.IConfig) =>
53
- (importer: FunctionImporter) =>
54
- (collection: MetadataCollection): ts.VariableStatement[] =>
55
- collection
56
- .arrays()
57
- .filter((a) => a.recursive)
58
- .map((type, i) =>
59
- StatementFactory.constant(
60
- `${config.prefix}a${i}`,
61
- ts.factory.createArrowFunction(
62
- undefined,
63
- undefined,
64
- FeatureProgrammer.parameterDeclarations(config)(
65
- TypeFactory.keyword("any"),
66
- )(ts.factory.createIdentifier("input")),
67
- TypeFactory.keyword("any"),
68
- undefined,
69
- decode_array_inline(config)(importer)(
70
- ts.factory.createIdentifier("input"),
71
- MetadataArray.create({
72
- type,
73
- tags: [],
74
- }),
75
- {
76
- tracable: config.trace,
77
- source: "function",
78
- from: "array",
79
- postfix: "",
80
- },
81
- ),
82
- ),
83
- ),
84
- );
85
-
86
- const write_tuple_functions =
87
- (project: IProject) =>
88
- (config: FeatureProgrammer.IConfig) =>
89
- (importer: FunctionImporter) =>
90
- (collection: MetadataCollection): ts.VariableStatement[] =>
91
- collection
92
- .tuples()
93
- .filter((t) => t.recursive)
94
- .map((tuple, i) =>
95
- StatementFactory.constant(
96
- `${config.prefix}t${i}`,
97
- ts.factory.createArrowFunction(
98
- undefined,
99
- undefined,
100
- FeatureProgrammer.parameterDeclarations(config)(
101
- TypeFactory.keyword("any"),
102
- )(ts.factory.createIdentifier("input")),
103
- TypeFactory.keyword("any"),
104
- undefined,
105
- decode_tuple_inline(project)(config)(importer)(
106
- ts.factory.createIdentifier("input"),
107
- tuple,
108
- {
109
- tracable: config.trace,
110
- source: "function",
111
- from: "array",
112
- postfix: "",
113
- },
114
- ),
115
- ),
116
- ),
117
- );
118
-
119
- /* -----------------------------------------------------------
120
- DECODERS
121
- ----------------------------------------------------------- */
122
- const decode =
123
- (project: IProject) =>
124
- (config: FeatureProgrammer.IConfig) =>
125
- (importer: FunctionImporter) =>
126
- (
127
- input: ts.Expression,
128
- meta: Metadata,
129
- explore: FeatureProgrammer.IExplore,
130
- ): ts.Expression => {
131
- // ANY TYPE
132
- if (
133
- meta.any ||
134
- meta.arrays.some((a) => a.type.value.any) ||
135
- meta.tuples.some(
136
- (t) =>
137
- !!t.type.elements.length &&
138
- t.type.elements.every((e) => e.any),
139
- )
140
- )
141
- return ts.factory.createCallExpression(
142
- importer.use("any"),
143
- undefined,
144
- [input],
145
- );
146
-
147
- interface IUnion {
148
- type: string;
149
- is: () => ts.Expression;
150
- value: () => ts.Expression;
151
- }
152
- const unions: IUnion[] = [];
153
-
154
- //----
155
- // LIST UP UNION TYPES
156
- //----
157
- // FUNCTIONAL
158
- if (meta.functional)
159
- unions.push({
160
- type: "functional",
161
- is: () =>
162
- ts.factory.createStrictEquality(
163
- ts.factory.createStringLiteral("function"),
164
- ts.factory.createTypeOfExpression(input),
165
- ),
166
- value: () => ts.factory.createIdentifier("undefined"),
167
- });
168
-
169
- // TUPLES
170
- for (const tuple of meta.tuples)
171
- unions.push({
172
- type: "tuple",
173
- is: () =>
174
- IsProgrammer.decode(project)(importer)(
175
- input,
176
- (() => {
177
- const partial = Metadata.initialize();
178
- partial.tuples.push(tuple);
179
- return partial;
180
- })(),
181
- explore,
182
- ),
183
- value: () =>
184
- decode_tuple(project)(config)(importer)(
185
- input,
186
- tuple,
187
- explore,
188
- ),
189
- });
190
-
191
- // ARRAYS
192
- if (meta.arrays.length)
193
- unions.push({
194
- type: "array",
195
- is: () => ExpressionFactory.isArray(input),
196
- value: () =>
197
- explore_arrays(project)(config)(importer)(
198
- input,
199
- meta.arrays,
200
- {
201
- ...explore,
202
- from: "array",
203
- },
204
- ),
205
- });
206
-
207
- // NATIVE TYPES
208
- if (meta.sets.length)
209
- unions.push({
210
- type: "set",
211
- is: () => ExpressionFactory.isInstanceOf("Set")(input),
212
- value: () =>
213
- explore_sets(project)(config)(importer)(
214
- input,
215
- meta.sets,
216
- { ...explore, from: "array" },
217
- ),
218
- });
219
- if (meta.maps.length)
220
- unions.push({
221
- type: "map",
222
- is: () => ExpressionFactory.isInstanceOf("Map")(input),
223
- value: () =>
224
- explore_maps(project)(config)(importer)(
225
- input,
226
- meta.maps,
227
- {
228
- ...explore,
229
- from: "array",
230
- },
231
- ),
232
- });
233
- for (const native of meta.natives) {
234
- if (native === "WeakSet" || native === "WeakMap") continue;
235
- unions.push({
236
- type: "native",
237
- is: () => ExpressionFactory.isInstanceOf(native)(input),
238
- value: () =>
239
- native === "Boolean" ||
240
- native === "Number" ||
241
- native === "String"
242
- ? ts.factory.createCallExpression(
243
- IdentifierFactory.access(input)("valueOf"),
244
- undefined,
245
- undefined,
246
- )
247
- : decode_native(native)(input),
248
- });
249
- }
250
-
251
- // OBJECTS
252
- if (meta.objects.length)
253
- unions.push({
254
- type: "object",
255
- is: () =>
256
- ExpressionFactory.isObject({
257
- checkNull: true,
258
- checkArray: false,
259
- })(input),
260
- value: () =>
261
- explore_objects(config)(importer)(input, meta, {
262
- ...explore,
263
- from: "object",
264
- }),
265
- });
266
-
267
- // COMPOSITION
268
- let last: ts.Expression = input;
269
- for (const u of unions.reverse())
270
- last = ts.factory.createConditionalExpression(
271
- u.is(),
272
- undefined,
273
- u.value(),
274
- undefined,
275
- last,
276
- );
277
- return ts.factory.createAsExpression(
278
- last,
279
- TypeFactory.keyword("any"),
280
- );
281
- };
282
-
283
- const decode_object = (importer: FunctionImporter) =>
284
- FeatureProgrammer.decode_object({
285
- trace: false,
286
- path: false,
287
- prefix: PREFIX,
288
- })(importer);
289
-
290
- const decode_array =
291
- (config: FeatureProgrammer.IConfig) =>
292
- (importer: FunctionImporter) =>
293
- (
294
- input: ts.Expression,
295
- array: MetadataArray,
296
- explore: FeatureProgrammer.IExplore,
297
- ) =>
298
- array.type.recursive
299
- ? ts.factory.createCallExpression(
300
- ts.factory.createIdentifier(
301
- importer.useLocal(
302
- `${config.prefix}a${array.type.index}`,
303
- ),
304
- ),
305
- undefined,
306
- FeatureProgrammer.argumentsArray(config)({
307
- ...explore,
308
- source: "function",
309
- from: "array",
310
- })(input),
311
- )
312
- : decode_array_inline(config)(importer)(input, array, explore);
313
-
314
- const decode_array_inline =
315
- (config: FeatureProgrammer.IConfig) =>
316
- (importer: FunctionImporter) =>
317
- (
318
- input: ts.Expression,
319
- array: MetadataArray,
320
- explore: FeatureProgrammer.IExplore,
321
- ) =>
322
- FeatureProgrammer.decode_array(config)(importer)(
323
- NotationJoiner.array,
324
- )(input, array, explore);
325
-
326
- const decode_tuple =
327
- (project: IProject) =>
328
- (config: FeatureProgrammer.IConfig) =>
329
- (importer: FunctionImporter) =>
330
- (
331
- input: ts.Expression,
332
- tuple: MetadataTuple,
333
- explore: FeatureProgrammer.IExplore,
334
- ): ts.Expression =>
335
- tuple.type.recursive
336
- ? ts.factory.createCallExpression(
337
- ts.factory.createIdentifier(
338
- importer.useLocal(
339
- `${config.prefix}t${tuple.type.index}`,
340
- ),
341
- ),
342
- undefined,
343
- FeatureProgrammer.argumentsArray(config)({
344
- ...explore,
345
- source: "function",
346
- })(input),
347
- )
348
- : decode_tuple_inline(project)(config)(importer)(
349
- input,
350
- tuple.type,
351
- explore,
352
- );
353
-
354
- const decode_tuple_inline =
355
- (project: IProject) =>
356
- (config: FeatureProgrammer.IConfig) =>
357
- (importer: FunctionImporter) =>
358
- (
359
- input: ts.Expression,
360
- tuple: MetadataTupleType,
361
- explore: FeatureProgrammer.IExplore,
362
- ): ts.Expression => {
363
- const children: ts.Expression[] = tuple.elements
364
- .filter((m) => m.rest === null)
365
- .map((elem, index) =>
366
- decode(project)(config)(importer)(
367
- ts.factory.createElementAccessExpression(input, index),
368
- elem,
369
- {
370
- ...explore,
371
- from: "array",
372
- postfix: explore.postfix.length
373
- ? `${explore.postfix.slice(0, -1)}[${index}]"`
374
- : `"[${index}]"`,
375
- },
376
- ),
377
- );
378
- const rest = (() => {
379
- if (tuple.elements.length === 0) return null;
380
-
381
- const last: Metadata = tuple.elements.at(-1)!;
382
- const rest: Metadata | null = last.rest;
383
- if (rest === null) return null;
384
-
385
- return decode(project)(config)(importer)(
386
- ts.factory.createCallExpression(
387
- IdentifierFactory.access(input)("slice"),
388
- undefined,
389
- [
390
- ts.factory.createNumericLiteral(
391
- tuple.elements.length - 1,
392
- ),
393
- ],
394
- ),
395
- wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
396
- {
397
- ...explore,
398
- start: tuple.elements.length - 1,
399
- },
400
- );
401
- })();
402
- return NotationJoiner.tuple(children, rest);
403
- };
404
-
405
- /* -----------------------------------------------------------
406
- NATIVE CLASSES
407
- ----------------------------------------------------------- */
408
- const decode_native = (type: string) => (input: ts.Expression) =>
409
- type === "Date"
410
- ? ts.factory.createNewExpression(
411
- ts.factory.createIdentifier(type),
412
- undefined,
413
- [input],
414
- )
415
- : input;
416
-
417
- /* -----------------------------------------------------------
418
- EXPLORERS FOR UNION TYPES
419
- ----------------------------------------------------------- */
420
- const explore_sets =
421
- (project: IProject) =>
422
- (config: FeatureProgrammer.IConfig) =>
423
- (importer: FunctionImporter) =>
424
- (
425
- input: ts.Expression,
426
- sets: Metadata[],
427
- explore: FeatureProgrammer.IExplore,
428
- ): ts.Expression =>
429
- ts.factory.createCallExpression(
430
- UnionExplorer.set({
431
- checker: IsProgrammer.decode(project)(importer),
432
- decoder: (input, array, explore) =>
433
- ts.factory.createNewExpression(
434
- ts.factory.createIdentifier("Set"),
435
- [TypeFactory.keyword("any")],
436
- [
437
- decode_array(config)(importer)(
438
- input,
439
- array,
440
- explore,
441
- ),
442
- ],
443
- ),
444
- empty: ts.factory.createNewExpression(
445
- ts.factory.createIdentifier("Set"),
446
- [TypeFactory.keyword("any")],
447
- [],
448
- ),
449
- success: ts.factory.createTrue(),
450
- failure: (input, expected) =>
451
- create_throw_error(importer)(expected)(input),
452
- })([])(input, sets, explore),
453
- undefined,
454
- undefined,
455
- );
456
-
457
- const explore_maps =
458
- (project: IProject) =>
459
- (config: FeatureProgrammer.IConfig) =>
460
- (importer: FunctionImporter) =>
461
- (
462
- input: ts.Expression,
463
- maps: Metadata.Entry[],
464
- explore: FeatureProgrammer.IExplore,
465
- ): ts.Expression =>
466
- ts.factory.createCallExpression(
467
- UnionExplorer.map({
468
- checker: (top, entry, explore) => {
469
- const func = IsProgrammer.decode(project)(importer);
470
- return ts.factory.createLogicalAnd(
471
- func(
472
- ts.factory.createElementAccessExpression(
473
- top,
474
- 0,
475
- ),
476
- entry[0],
477
- {
478
- ...explore,
479
- postfix: `${explore.postfix}[0]`,
480
- },
481
- ),
482
- func(
483
- ts.factory.createElementAccessExpression(
484
- top,
485
- 1,
486
- ),
487
- entry[1],
488
- {
489
- ...explore,
490
- postfix: `${explore.postfix}[1]`,
491
- },
492
- ),
493
- );
494
- },
495
- decoder: (input, array, explore) =>
496
- ts.factory.createNewExpression(
497
- ts.factory.createIdentifier("Map"),
498
- [
499
- TypeFactory.keyword("any"),
500
- TypeFactory.keyword("any"),
501
- ],
502
- [
503
- decode_array(config)(importer)(
504
- input,
505
- array,
506
- explore,
507
- ),
508
- ],
509
- ),
510
- empty: ts.factory.createNewExpression(
511
- ts.factory.createIdentifier("Map"),
512
- [
513
- TypeFactory.keyword("any"),
514
- TypeFactory.keyword("any"),
515
- ],
516
- [],
517
- ),
518
- success: ts.factory.createTrue(),
519
- failure: (input, expected) =>
520
- create_throw_error(importer)(expected)(input),
521
- })([])(input, maps, explore),
522
- undefined,
523
- undefined,
524
- );
525
-
526
- const explore_objects =
527
- (config: FeatureProgrammer.IConfig) =>
528
- (importer: FunctionImporter) =>
529
- (
530
- input: ts.Expression,
531
- meta: Metadata,
532
- explore: FeatureProgrammer.IExplore,
533
- ) => {
534
- if (meta.objects.length === 1)
535
- return decode_object(importer)(
536
- input,
537
- meta.objects[0]!,
538
- explore,
539
- );
540
-
541
- return ts.factory.createCallExpression(
542
- ts.factory.createIdentifier(
543
- importer.useLocal(`${PREFIX}u${meta.union_index!}`),
544
- ),
545
- undefined,
546
- FeatureProgrammer.argumentsArray(config)(explore)(input),
547
- );
548
- };
549
-
550
- const explore_arrays =
551
- (project: IProject) =>
552
- (config: FeatureProgrammer.IConfig) =>
553
- (importer: FunctionImporter) =>
554
- (
555
- input: ts.Expression,
556
- elements: MetadataArray[],
557
- explore: FeatureProgrammer.IExplore,
558
- ): ts.Expression =>
559
- explore_array_like_union_types(config)(importer)(
560
- UnionExplorer.array({
561
- checker: IsProgrammer.decode(project)(importer),
562
- decoder: decode_array(config)(importer),
563
- empty: ts.factory.createIdentifier("[]"),
564
- success: ts.factory.createTrue(),
565
- failure: (input, expected) =>
566
- create_throw_error(importer)(expected)(input),
567
- }),
568
- )(input, elements, explore);
569
-
570
- const explore_array_like_union_types =
571
- (config: FeatureProgrammer.IConfig) =>
572
- (importer: FunctionImporter) =>
573
- <T extends MetadataArray | MetadataTuple>(
574
- factory: (
575
- parameters: ts.ParameterDeclaration[],
576
- ) => (
577
- input: ts.Expression,
578
- elements: T[],
579
- explore: FeatureProgrammer.IExplore,
580
- ) => ts.ArrowFunction,
581
- ) =>
582
- (
583
- input: ts.Expression,
584
- elements: T[],
585
- explore: FeatureProgrammer.IExplore,
586
- ): ts.Expression => {
587
- const arrow =
588
- (parameters: ts.ParameterDeclaration[]) =>
589
- (explore: FeatureProgrammer.IExplore) =>
590
- (input: ts.Expression): ts.ArrowFunction =>
591
- factory(parameters)(input, elements, explore);
592
- if (elements.every((e) => e.type.recursive === false))
593
- ts.factory.createCallExpression(
594
- arrow([])(explore)(input),
595
- undefined,
596
- [],
597
- );
598
-
599
- explore = {
600
- ...explore,
601
- source: "function",
602
- from: "array",
603
- };
604
- return ts.factory.createCallExpression(
605
- ts.factory.createIdentifier(
606
- importer.emplaceUnion(
607
- config.prefix,
608
- elements.map((e) => e.type.name).join(" | "),
609
- () =>
610
- arrow(
611
- FeatureProgrammer.parameterDeclarations(config)(
612
- TypeFactory.keyword("any"),
613
- )(ts.factory.createIdentifier("input")),
614
- )({
615
- ...explore,
616
- postfix: "",
617
- })(ts.factory.createIdentifier("input")),
618
- ),
619
- ),
620
- undefined,
621
- FeatureProgrammer.argumentsArray(config)(explore)(input),
622
- );
623
- };
624
-
625
- /* -----------------------------------------------------------
626
- CONFIGURATIONS
627
- ----------------------------------------------------------- */
628
- const PREFIX = "$c";
629
-
630
- const configure =
631
- (rename: (str: string) => string) =>
632
- (project: IProject) =>
633
- (importer: FunctionImporter): FeatureProgrammer.IConfig => {
634
- const config: FeatureProgrammer.IConfig = {
635
- types: {
636
- input: (type, name) =>
637
- ts.factory.createTypeReferenceNode(
638
- name ??
639
- TypeFactory.getFullName(project.checker)(type),
640
- ),
641
- output: (type, name) =>
642
- ts.factory.createTypeReferenceNode(
643
- returnType(rename)(
644
- name ??
645
- TypeFactory.getFullName(project.checker)(
646
- type,
647
- ),
648
- ),
649
- ),
650
- },
651
- prefix: PREFIX,
652
- trace: false,
653
- path: false,
654
- initializer,
655
- decoder: () => decode(project)(config)(importer),
656
- objector: {
657
- checker: () => IsProgrammer.decode(project)(importer),
658
- decoder: () => decode_object(importer),
659
- joiner: NotationJoiner.object(rename),
660
- unionizer: decode_union_object(
661
- IsProgrammer.decode_object(importer),
662
- )(decode_object(importer))((exp) => exp)(
663
- (input, expected) =>
664
- create_throw_error(importer)(expected)(input),
665
- ),
666
- failure: (input, expected) =>
667
- create_throw_error(importer)(expected)(input),
668
- },
669
- generator: {
670
- arrays: () => write_array_functions(config)(importer),
671
- tuples: () =>
672
- write_tuple_functions(project)(config)(importer),
673
- },
674
- };
675
- return config;
676
- };
677
-
678
- const initializer: FeatureProgrammer.IConfig["initializer"] =
679
- ({ checker }) =>
680
- (importer) =>
681
- (type) => {
682
- const collection = new MetadataCollection();
683
- const result = MetadataFactory.analyze(checker)({
684
- escape: false,
685
- constant: true,
686
- absorb: true,
687
- })(collection)(type);
688
- if (result.success === false)
689
- throw TransformerError.from(`typia.misc.${importer.method}`)(
690
- result.errors,
691
- );
692
- return [collection, result.data];
693
- };
694
-
695
- const create_throw_error =
696
- (importer: FunctionImporter) =>
697
- (expected: string) =>
698
- (value: ts.Expression) =>
699
- ts.factory.createExpressionStatement(
700
- ts.factory.createCallExpression(
701
- importer.use("throws"),
702
- [],
703
- [
704
- ts.factory.createObjectLiteralExpression(
705
- [
706
- ts.factory.createPropertyAssignment(
707
- "expected",
708
- ts.factory.createStringLiteral(expected),
709
- ),
710
- ts.factory.createPropertyAssignment(
711
- "value",
712
- value,
713
- ),
714
- ],
715
- true,
716
- ),
717
- ],
718
- ),
719
- );
720
- }
1
+ import ts from "typescript";
2
+
3
+ import { ExpressionFactory } from "../../factories/ExpressionFactory";
4
+ import { IdentifierFactory } from "../../factories/IdentifierFactory";
5
+ import { MetadataCollection } from "../../factories/MetadataCollection";
6
+ import { MetadataFactory } from "../../factories/MetadataFactory";
7
+ import { StatementFactory } from "../../factories/StatementFactory";
8
+ import { TypeFactory } from "../../factories/TypeFactory";
9
+
10
+ import { Metadata } from "../../schemas/metadata/Metadata";
11
+ import { MetadataArray } from "../../schemas/metadata/MetadataArray";
12
+ import { MetadataTuple } from "../../schemas/metadata/MetadataTuple";
13
+ import { MetadataTupleType } from "../../schemas/metadata/MetadataTupleType";
14
+
15
+ import { IProject } from "../../transformers/IProject";
16
+ import { TransformerError } from "../../transformers/TransformerError";
17
+
18
+ import { StringUtil } from "../../utils/StringUtil";
19
+
20
+ import { FeatureProgrammer } from "../FeatureProgrammer";
21
+ import { IsProgrammer } from "../IsProgrammer";
22
+ import { FunctionImporter } from "../helpers/FunctionImporeter";
23
+ import { NotationJoiner } from "../helpers/NotationJoiner";
24
+ import { UnionExplorer } from "../helpers/UnionExplorer";
25
+ import { decode_union_object } from "../internal/decode_union_object";
26
+ import { wrap_metadata_rest_tuple } from "../internal/wrap_metadata_rest_tuple";
27
+
28
+ export namespace NotationGeneralProgrammer {
29
+ export const returnType =
30
+ (rename: (str: string) => string) => (type: string) =>
31
+ `typia.${StringUtil.capitalize(rename.name)}Case<${type}>`;
32
+
33
+ export const write =
34
+ (rename: (str: string) => string) =>
35
+ (project: IProject) =>
36
+ (modulo: ts.LeftHandSideExpression) => {
37
+ const importer: FunctionImporter = new FunctionImporter(
38
+ modulo.getText(),
39
+ );
40
+ return FeatureProgrammer.write(project)({
41
+ ...configure(rename)(project)(importer),
42
+ addition: (collection) => [
43
+ ...IsProgrammer.write_function_statements(project)(
44
+ importer,
45
+ )(collection),
46
+ ...importer.declare(modulo),
47
+ ],
48
+ })(importer);
49
+ };
50
+
51
+ const write_array_functions =
52
+ (config: FeatureProgrammer.IConfig) =>
53
+ (importer: FunctionImporter) =>
54
+ (collection: MetadataCollection): ts.VariableStatement[] =>
55
+ collection
56
+ .arrays()
57
+ .filter((a) => a.recursive)
58
+ .map((type, i) =>
59
+ StatementFactory.constant(
60
+ `${config.prefix}a${i}`,
61
+ ts.factory.createArrowFunction(
62
+ undefined,
63
+ undefined,
64
+ FeatureProgrammer.parameterDeclarations(config)(
65
+ TypeFactory.keyword("any"),
66
+ )(ts.factory.createIdentifier("input")),
67
+ TypeFactory.keyword("any"),
68
+ undefined,
69
+ decode_array_inline(config)(importer)(
70
+ ts.factory.createIdentifier("input"),
71
+ MetadataArray.create({
72
+ type,
73
+ tags: [],
74
+ }),
75
+ {
76
+ tracable: config.trace,
77
+ source: "function",
78
+ from: "array",
79
+ postfix: "",
80
+ },
81
+ ),
82
+ ),
83
+ ),
84
+ );
85
+
86
+ const write_tuple_functions =
87
+ (project: IProject) =>
88
+ (config: FeatureProgrammer.IConfig) =>
89
+ (importer: FunctionImporter) =>
90
+ (collection: MetadataCollection): ts.VariableStatement[] =>
91
+ collection
92
+ .tuples()
93
+ .filter((t) => t.recursive)
94
+ .map((tuple, i) =>
95
+ StatementFactory.constant(
96
+ `${config.prefix}t${i}`,
97
+ ts.factory.createArrowFunction(
98
+ undefined,
99
+ undefined,
100
+ FeatureProgrammer.parameterDeclarations(config)(
101
+ TypeFactory.keyword("any"),
102
+ )(ts.factory.createIdentifier("input")),
103
+ TypeFactory.keyword("any"),
104
+ undefined,
105
+ decode_tuple_inline(project)(config)(importer)(
106
+ ts.factory.createIdentifier("input"),
107
+ tuple,
108
+ {
109
+ tracable: config.trace,
110
+ source: "function",
111
+ from: "array",
112
+ postfix: "",
113
+ },
114
+ ),
115
+ ),
116
+ ),
117
+ );
118
+
119
+ /* -----------------------------------------------------------
120
+ DECODERS
121
+ ----------------------------------------------------------- */
122
+ const decode =
123
+ (project: IProject) =>
124
+ (config: FeatureProgrammer.IConfig) =>
125
+ (importer: FunctionImporter) =>
126
+ (
127
+ input: ts.Expression,
128
+ meta: Metadata,
129
+ explore: FeatureProgrammer.IExplore,
130
+ ): ts.Expression => {
131
+ // ANY TYPE
132
+ if (
133
+ meta.any ||
134
+ meta.arrays.some((a) => a.type.value.any) ||
135
+ meta.tuples.some(
136
+ (t) =>
137
+ !!t.type.elements.length &&
138
+ t.type.elements.every((e) => e.any),
139
+ )
140
+ )
141
+ return ts.factory.createCallExpression(
142
+ importer.use("any"),
143
+ undefined,
144
+ [input],
145
+ );
146
+
147
+ interface IUnion {
148
+ type: string;
149
+ is: () => ts.Expression;
150
+ value: () => ts.Expression;
151
+ }
152
+ const unions: IUnion[] = [];
153
+
154
+ //----
155
+ // LIST UP UNION TYPES
156
+ //----
157
+ // FUNCTIONAL
158
+ if (meta.functional)
159
+ unions.push({
160
+ type: "functional",
161
+ is: () =>
162
+ ts.factory.createStrictEquality(
163
+ ts.factory.createStringLiteral("function"),
164
+ ts.factory.createTypeOfExpression(input),
165
+ ),
166
+ value: () => ts.factory.createIdentifier("undefined"),
167
+ });
168
+
169
+ // TUPLES
170
+ for (const tuple of meta.tuples)
171
+ unions.push({
172
+ type: "tuple",
173
+ is: () =>
174
+ IsProgrammer.decode(project)(importer)(
175
+ input,
176
+ (() => {
177
+ const partial = Metadata.initialize();
178
+ partial.tuples.push(tuple);
179
+ return partial;
180
+ })(),
181
+ explore,
182
+ ),
183
+ value: () =>
184
+ decode_tuple(project)(config)(importer)(
185
+ input,
186
+ tuple,
187
+ explore,
188
+ ),
189
+ });
190
+
191
+ // ARRAYS
192
+ if (meta.arrays.length)
193
+ unions.push({
194
+ type: "array",
195
+ is: () => ExpressionFactory.isArray(input),
196
+ value: () =>
197
+ explore_arrays(project)(config)(importer)(
198
+ input,
199
+ meta.arrays,
200
+ {
201
+ ...explore,
202
+ from: "array",
203
+ },
204
+ ),
205
+ });
206
+
207
+ // NATIVE TYPES
208
+ if (meta.sets.length)
209
+ unions.push({
210
+ type: "set",
211
+ is: () => ExpressionFactory.isInstanceOf("Set")(input),
212
+ value: () =>
213
+ explore_sets(project)(config)(importer)(
214
+ input,
215
+ meta.sets,
216
+ { ...explore, from: "array" },
217
+ ),
218
+ });
219
+ if (meta.maps.length)
220
+ unions.push({
221
+ type: "map",
222
+ is: () => ExpressionFactory.isInstanceOf("Map")(input),
223
+ value: () =>
224
+ explore_maps(project)(config)(importer)(
225
+ input,
226
+ meta.maps,
227
+ {
228
+ ...explore,
229
+ from: "array",
230
+ },
231
+ ),
232
+ });
233
+ for (const native of meta.natives) {
234
+ if (native === "WeakSet" || native === "WeakMap") continue;
235
+ unions.push({
236
+ type: "native",
237
+ is: () => ExpressionFactory.isInstanceOf(native)(input),
238
+ value: () =>
239
+ native === "Boolean" ||
240
+ native === "Number" ||
241
+ native === "String"
242
+ ? ts.factory.createCallExpression(
243
+ IdentifierFactory.access(input)("valueOf"),
244
+ undefined,
245
+ undefined,
246
+ )
247
+ : decode_native(native)(input),
248
+ });
249
+ }
250
+
251
+ // OBJECTS
252
+ if (meta.objects.length)
253
+ unions.push({
254
+ type: "object",
255
+ is: () =>
256
+ ExpressionFactory.isObject({
257
+ checkNull: true,
258
+ checkArray: false,
259
+ })(input),
260
+ value: () =>
261
+ explore_objects(config)(importer)(input, meta, {
262
+ ...explore,
263
+ from: "object",
264
+ }),
265
+ });
266
+
267
+ // COMPOSITION
268
+ let last: ts.Expression = input;
269
+ for (const u of unions.reverse())
270
+ last = ts.factory.createConditionalExpression(
271
+ u.is(),
272
+ undefined,
273
+ u.value(),
274
+ undefined,
275
+ last,
276
+ );
277
+ return ts.factory.createAsExpression(
278
+ last,
279
+ TypeFactory.keyword("any"),
280
+ );
281
+ };
282
+
283
+ const decode_object = (importer: FunctionImporter) =>
284
+ FeatureProgrammer.decode_object({
285
+ trace: false,
286
+ path: false,
287
+ prefix: PREFIX,
288
+ })(importer);
289
+
290
+ const decode_array =
291
+ (config: FeatureProgrammer.IConfig) =>
292
+ (importer: FunctionImporter) =>
293
+ (
294
+ input: ts.Expression,
295
+ array: MetadataArray,
296
+ explore: FeatureProgrammer.IExplore,
297
+ ) =>
298
+ array.type.recursive
299
+ ? ts.factory.createCallExpression(
300
+ ts.factory.createIdentifier(
301
+ importer.useLocal(
302
+ `${config.prefix}a${array.type.index}`,
303
+ ),
304
+ ),
305
+ undefined,
306
+ FeatureProgrammer.argumentsArray(config)({
307
+ ...explore,
308
+ source: "function",
309
+ from: "array",
310
+ })(input),
311
+ )
312
+ : decode_array_inline(config)(importer)(input, array, explore);
313
+
314
+ const decode_array_inline =
315
+ (config: FeatureProgrammer.IConfig) =>
316
+ (importer: FunctionImporter) =>
317
+ (
318
+ input: ts.Expression,
319
+ array: MetadataArray,
320
+ explore: FeatureProgrammer.IExplore,
321
+ ) =>
322
+ FeatureProgrammer.decode_array(config)(importer)(
323
+ NotationJoiner.array,
324
+ )(input, array, explore);
325
+
326
+ const decode_tuple =
327
+ (project: IProject) =>
328
+ (config: FeatureProgrammer.IConfig) =>
329
+ (importer: FunctionImporter) =>
330
+ (
331
+ input: ts.Expression,
332
+ tuple: MetadataTuple,
333
+ explore: FeatureProgrammer.IExplore,
334
+ ): ts.Expression =>
335
+ tuple.type.recursive
336
+ ? ts.factory.createCallExpression(
337
+ ts.factory.createIdentifier(
338
+ importer.useLocal(
339
+ `${config.prefix}t${tuple.type.index}`,
340
+ ),
341
+ ),
342
+ undefined,
343
+ FeatureProgrammer.argumentsArray(config)({
344
+ ...explore,
345
+ source: "function",
346
+ })(input),
347
+ )
348
+ : decode_tuple_inline(project)(config)(importer)(
349
+ input,
350
+ tuple.type,
351
+ explore,
352
+ );
353
+
354
+ const decode_tuple_inline =
355
+ (project: IProject) =>
356
+ (config: FeatureProgrammer.IConfig) =>
357
+ (importer: FunctionImporter) =>
358
+ (
359
+ input: ts.Expression,
360
+ tuple: MetadataTupleType,
361
+ explore: FeatureProgrammer.IExplore,
362
+ ): ts.Expression => {
363
+ const children: ts.Expression[] = tuple.elements
364
+ .filter((m) => m.rest === null)
365
+ .map((elem, index) =>
366
+ decode(project)(config)(importer)(
367
+ ts.factory.createElementAccessExpression(input, index),
368
+ elem,
369
+ {
370
+ ...explore,
371
+ from: "array",
372
+ postfix: explore.postfix.length
373
+ ? `${explore.postfix.slice(0, -1)}[${index}]"`
374
+ : `"[${index}]"`,
375
+ },
376
+ ),
377
+ );
378
+ const rest = (() => {
379
+ if (tuple.elements.length === 0) return null;
380
+
381
+ const last: Metadata = tuple.elements.at(-1)!;
382
+ const rest: Metadata | null = last.rest;
383
+ if (rest === null) return null;
384
+
385
+ return decode(project)(config)(importer)(
386
+ ts.factory.createCallExpression(
387
+ IdentifierFactory.access(input)("slice"),
388
+ undefined,
389
+ [
390
+ ts.factory.createNumericLiteral(
391
+ tuple.elements.length - 1,
392
+ ),
393
+ ],
394
+ ),
395
+ wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
396
+ {
397
+ ...explore,
398
+ start: tuple.elements.length - 1,
399
+ },
400
+ );
401
+ })();
402
+ return NotationJoiner.tuple(children, rest);
403
+ };
404
+
405
+ /* -----------------------------------------------------------
406
+ NATIVE CLASSES
407
+ ----------------------------------------------------------- */
408
+ const decode_native = (type: string) => (input: ts.Expression) =>
409
+ type === "Date"
410
+ ? ts.factory.createNewExpression(
411
+ ts.factory.createIdentifier(type),
412
+ undefined,
413
+ [input],
414
+ )
415
+ : input;
416
+
417
+ /* -----------------------------------------------------------
418
+ EXPLORERS FOR UNION TYPES
419
+ ----------------------------------------------------------- */
420
+ const explore_sets =
421
+ (project: IProject) =>
422
+ (config: FeatureProgrammer.IConfig) =>
423
+ (importer: FunctionImporter) =>
424
+ (
425
+ input: ts.Expression,
426
+ sets: Metadata[],
427
+ explore: FeatureProgrammer.IExplore,
428
+ ): ts.Expression =>
429
+ ts.factory.createCallExpression(
430
+ UnionExplorer.set({
431
+ checker: IsProgrammer.decode(project)(importer),
432
+ decoder: (input, array, explore) =>
433
+ ts.factory.createNewExpression(
434
+ ts.factory.createIdentifier("Set"),
435
+ [TypeFactory.keyword("any")],
436
+ [
437
+ decode_array(config)(importer)(
438
+ input,
439
+ array,
440
+ explore,
441
+ ),
442
+ ],
443
+ ),
444
+ empty: ts.factory.createNewExpression(
445
+ ts.factory.createIdentifier("Set"),
446
+ [TypeFactory.keyword("any")],
447
+ [],
448
+ ),
449
+ success: ts.factory.createTrue(),
450
+ failure: (input, expected) =>
451
+ create_throw_error(importer)(expected)(input),
452
+ })([])(input, sets, explore),
453
+ undefined,
454
+ undefined,
455
+ );
456
+
457
+ const explore_maps =
458
+ (project: IProject) =>
459
+ (config: FeatureProgrammer.IConfig) =>
460
+ (importer: FunctionImporter) =>
461
+ (
462
+ input: ts.Expression,
463
+ maps: Metadata.Entry[],
464
+ explore: FeatureProgrammer.IExplore,
465
+ ): ts.Expression =>
466
+ ts.factory.createCallExpression(
467
+ UnionExplorer.map({
468
+ checker: (top, entry, explore) => {
469
+ const func = IsProgrammer.decode(project)(importer);
470
+ return ts.factory.createLogicalAnd(
471
+ func(
472
+ ts.factory.createElementAccessExpression(
473
+ top,
474
+ 0,
475
+ ),
476
+ entry[0],
477
+ {
478
+ ...explore,
479
+ postfix: `${explore.postfix}[0]`,
480
+ },
481
+ ),
482
+ func(
483
+ ts.factory.createElementAccessExpression(
484
+ top,
485
+ 1,
486
+ ),
487
+ entry[1],
488
+ {
489
+ ...explore,
490
+ postfix: `${explore.postfix}[1]`,
491
+ },
492
+ ),
493
+ );
494
+ },
495
+ decoder: (input, array, explore) =>
496
+ ts.factory.createNewExpression(
497
+ ts.factory.createIdentifier("Map"),
498
+ [
499
+ TypeFactory.keyword("any"),
500
+ TypeFactory.keyword("any"),
501
+ ],
502
+ [
503
+ decode_array(config)(importer)(
504
+ input,
505
+ array,
506
+ explore,
507
+ ),
508
+ ],
509
+ ),
510
+ empty: ts.factory.createNewExpression(
511
+ ts.factory.createIdentifier("Map"),
512
+ [
513
+ TypeFactory.keyword("any"),
514
+ TypeFactory.keyword("any"),
515
+ ],
516
+ [],
517
+ ),
518
+ success: ts.factory.createTrue(),
519
+ failure: (input, expected) =>
520
+ create_throw_error(importer)(expected)(input),
521
+ })([])(input, maps, explore),
522
+ undefined,
523
+ undefined,
524
+ );
525
+
526
+ const explore_objects =
527
+ (config: FeatureProgrammer.IConfig) =>
528
+ (importer: FunctionImporter) =>
529
+ (
530
+ input: ts.Expression,
531
+ meta: Metadata,
532
+ explore: FeatureProgrammer.IExplore,
533
+ ) => {
534
+ if (meta.objects.length === 1)
535
+ return decode_object(importer)(
536
+ input,
537
+ meta.objects[0]!,
538
+ explore,
539
+ );
540
+
541
+ return ts.factory.createCallExpression(
542
+ ts.factory.createIdentifier(
543
+ importer.useLocal(`${PREFIX}u${meta.union_index!}`),
544
+ ),
545
+ undefined,
546
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
547
+ );
548
+ };
549
+
550
+ const explore_arrays =
551
+ (project: IProject) =>
552
+ (config: FeatureProgrammer.IConfig) =>
553
+ (importer: FunctionImporter) =>
554
+ (
555
+ input: ts.Expression,
556
+ elements: MetadataArray[],
557
+ explore: FeatureProgrammer.IExplore,
558
+ ): ts.Expression =>
559
+ explore_array_like_union_types(config)(importer)(
560
+ UnionExplorer.array({
561
+ checker: IsProgrammer.decode(project)(importer),
562
+ decoder: decode_array(config)(importer),
563
+ empty: ts.factory.createIdentifier("[]"),
564
+ success: ts.factory.createTrue(),
565
+ failure: (input, expected) =>
566
+ create_throw_error(importer)(expected)(input),
567
+ }),
568
+ )(input, elements, explore);
569
+
570
+ const explore_array_like_union_types =
571
+ (config: FeatureProgrammer.IConfig) =>
572
+ (importer: FunctionImporter) =>
573
+ <T extends MetadataArray | MetadataTuple>(
574
+ factory: (
575
+ parameters: ts.ParameterDeclaration[],
576
+ ) => (
577
+ input: ts.Expression,
578
+ elements: T[],
579
+ explore: FeatureProgrammer.IExplore,
580
+ ) => ts.ArrowFunction,
581
+ ) =>
582
+ (
583
+ input: ts.Expression,
584
+ elements: T[],
585
+ explore: FeatureProgrammer.IExplore,
586
+ ): ts.Expression => {
587
+ const arrow =
588
+ (parameters: ts.ParameterDeclaration[]) =>
589
+ (explore: FeatureProgrammer.IExplore) =>
590
+ (input: ts.Expression): ts.ArrowFunction =>
591
+ factory(parameters)(input, elements, explore);
592
+ if (elements.every((e) => e.type.recursive === false))
593
+ ts.factory.createCallExpression(
594
+ arrow([])(explore)(input),
595
+ undefined,
596
+ [],
597
+ );
598
+
599
+ explore = {
600
+ ...explore,
601
+ source: "function",
602
+ from: "array",
603
+ };
604
+ return ts.factory.createCallExpression(
605
+ ts.factory.createIdentifier(
606
+ importer.emplaceUnion(
607
+ config.prefix,
608
+ elements.map((e) => e.type.name).join(" | "),
609
+ () =>
610
+ arrow(
611
+ FeatureProgrammer.parameterDeclarations(config)(
612
+ TypeFactory.keyword("any"),
613
+ )(ts.factory.createIdentifier("input")),
614
+ )({
615
+ ...explore,
616
+ postfix: "",
617
+ })(ts.factory.createIdentifier("input")),
618
+ ),
619
+ ),
620
+ undefined,
621
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
622
+ );
623
+ };
624
+
625
+ /* -----------------------------------------------------------
626
+ CONFIGURATIONS
627
+ ----------------------------------------------------------- */
628
+ const PREFIX = "$c";
629
+
630
+ const configure =
631
+ (rename: (str: string) => string) =>
632
+ (project: IProject) =>
633
+ (importer: FunctionImporter): FeatureProgrammer.IConfig => {
634
+ const config: FeatureProgrammer.IConfig = {
635
+ types: {
636
+ input: (type, name) =>
637
+ ts.factory.createTypeReferenceNode(
638
+ name ??
639
+ TypeFactory.getFullName(project.checker)(type),
640
+ ),
641
+ output: (type, name) =>
642
+ ts.factory.createTypeReferenceNode(
643
+ returnType(rename)(
644
+ name ??
645
+ TypeFactory.getFullName(project.checker)(
646
+ type,
647
+ ),
648
+ ),
649
+ ),
650
+ },
651
+ prefix: PREFIX,
652
+ trace: false,
653
+ path: false,
654
+ initializer,
655
+ decoder: () => decode(project)(config)(importer),
656
+ objector: {
657
+ checker: () => IsProgrammer.decode(project)(importer),
658
+ decoder: () => decode_object(importer),
659
+ joiner: NotationJoiner.object(rename),
660
+ unionizer: decode_union_object(
661
+ IsProgrammer.decode_object(importer),
662
+ )(decode_object(importer))((exp) => exp)(
663
+ (input, expected) =>
664
+ create_throw_error(importer)(expected)(input),
665
+ ),
666
+ failure: (input, expected) =>
667
+ create_throw_error(importer)(expected)(input),
668
+ },
669
+ generator: {
670
+ arrays: () => write_array_functions(config)(importer),
671
+ tuples: () =>
672
+ write_tuple_functions(project)(config)(importer),
673
+ },
674
+ };
675
+ return config;
676
+ };
677
+
678
+ const initializer: FeatureProgrammer.IConfig["initializer"] =
679
+ ({ checker }) =>
680
+ (importer) =>
681
+ (type) => {
682
+ const collection = new MetadataCollection();
683
+ const result = MetadataFactory.analyze(checker)({
684
+ escape: false,
685
+ constant: true,
686
+ absorb: true,
687
+ })(collection)(type);
688
+ if (result.success === false)
689
+ throw TransformerError.from(`typia.misc.${importer.method}`)(
690
+ result.errors,
691
+ );
692
+ return [collection, result.data];
693
+ };
694
+
695
+ const create_throw_error =
696
+ (importer: FunctionImporter) =>
697
+ (expected: string) =>
698
+ (value: ts.Expression) =>
699
+ ts.factory.createExpressionStatement(
700
+ ts.factory.createCallExpression(
701
+ importer.use("throws"),
702
+ [],
703
+ [
704
+ ts.factory.createObjectLiteralExpression(
705
+ [
706
+ ts.factory.createPropertyAssignment(
707
+ "expected",
708
+ ts.factory.createStringLiteral(expected),
709
+ ),
710
+ ts.factory.createPropertyAssignment(
711
+ "value",
712
+ value,
713
+ ),
714
+ ],
715
+ true,
716
+ ),
717
+ ],
718
+ ),
719
+ );
720
+ }