typia 3.7.5 → 3.8.0-dev.20230415

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