typia 3.6.8 → 3.6.10

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