typia 4.2.2 → 4.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 (250) hide show
  1. package/lib/factories/MetadataCollection.js +1 -0
  2. package/lib/factories/MetadataCollection.js.map +1 -1
  3. package/lib/factories/MetadataFactory.js +27 -0
  4. package/lib/factories/MetadataFactory.js.map +1 -1
  5. package/lib/factories/internal/metadata/explore_metadata.js +3 -7
  6. package/lib/factories/internal/metadata/explore_metadata.js.map +1 -1
  7. package/lib/factories/internal/metadata/iterate_metadata_intersection.js +1 -0
  8. package/lib/factories/internal/metadata/iterate_metadata_intersection.js.map +1 -1
  9. package/lib/programmers/CheckerProgrammer.js +0 -5
  10. package/lib/programmers/CheckerProgrammer.js.map +1 -1
  11. package/lib/programmers/StringifyProgrammer.js +3 -0
  12. package/lib/programmers/StringifyProgrammer.js.map +1 -1
  13. package/lib/transformers/features/miscellaneous/ApplicationTransformer.js +3 -0
  14. package/lib/transformers/features/miscellaneous/ApplicationTransformer.js.map +1 -1
  15. package/package.json +1 -1
  16. package/src/CustomValidatorMap.ts +126 -126
  17. package/src/IRandomGenerator.ts +34 -34
  18. package/src/IValidation.ts +21 -21
  19. package/src/Primitive.ts +131 -131
  20. package/src/TypeGuardError.ts +36 -36
  21. package/src/executable/TypiaGenerateWizard.ts +85 -85
  22. package/src/executable/TypiaSetupWizard.ts +153 -153
  23. package/src/executable/setup/ArgumentParser.ts +45 -45
  24. package/src/executable/setup/CommandExecutor.ts +8 -8
  25. package/src/executable/setup/FileRetriever.ts +22 -22
  26. package/src/executable/setup/PackageManager.ts +71 -71
  27. package/src/executable/setup/PluginConfigurator.ts +70 -70
  28. package/src/executable/typia.ts +52 -52
  29. package/src/factories/CommentFactory.ts +84 -84
  30. package/src/factories/ExpressionFactory.ts +70 -70
  31. package/src/factories/IdentifierFactory.ts +59 -59
  32. package/src/factories/LiteralFactory.ts +39 -39
  33. package/src/factories/MetadataCollection.ts +269 -264
  34. package/src/factories/MetadataFactory.ts +34 -30
  35. package/src/factories/MetadataTagFactory.ts +355 -355
  36. package/src/factories/StatementFactory.ts +24 -24
  37. package/src/factories/TemplateFactory.ts +58 -58
  38. package/src/factories/TypeFactory.ts +124 -124
  39. package/src/factories/ValueFactory.ts +12 -12
  40. package/src/factories/internal/metadata/MetadataHelper.ts +12 -12
  41. package/src/factories/internal/metadata/emend_metadata_atomics.ts +33 -33
  42. package/src/factories/internal/metadata/emplace_metadata_alias.ts +40 -40
  43. package/src/factories/internal/metadata/emplace_metadata_array.ts +34 -34
  44. package/src/factories/internal/metadata/emplace_metadata_object.ts +136 -136
  45. package/src/factories/internal/metadata/emplace_metadata_tuple.ts +50 -50
  46. package/src/factories/internal/metadata/explore_metadata.ts +38 -40
  47. package/src/factories/internal/metadata/iterate_metadata.ts +81 -81
  48. package/src/factories/internal/metadata/iterate_metadata_alias.ts +30 -30
  49. package/src/factories/internal/metadata/iterate_metadata_array.ts +24 -24
  50. package/src/factories/internal/metadata/iterate_metadata_atomic.ts +59 -59
  51. package/src/factories/internal/metadata/iterate_metadata_coalesce.ts +33 -33
  52. package/src/factories/internal/metadata/iterate_metadata_collection.ts +133 -133
  53. package/src/factories/internal/metadata/iterate_metadata_constant.ts +58 -58
  54. package/src/factories/internal/metadata/iterate_metadata_intersection.ts +84 -83
  55. package/src/factories/internal/metadata/iterate_metadata_map.ts +41 -41
  56. package/src/factories/internal/metadata/iterate_metadata_native.ts +219 -219
  57. package/src/factories/internal/metadata/iterate_metadata_object.ts +43 -43
  58. package/src/factories/internal/metadata/iterate_metadata_resolve.ts +49 -49
  59. package/src/factories/internal/metadata/iterate_metadata_set.ts +33 -33
  60. package/src/factories/internal/metadata/iterate_metadata_sort.ts +69 -69
  61. package/src/factories/internal/metadata/iterate_metadata_tag.ts +31 -31
  62. package/src/factories/internal/metadata/iterate_metadata_template.ts +38 -38
  63. package/src/factories/internal/metadata/iterate_metadata_tuple.ts +24 -24
  64. package/src/factories/internal/metadata/iterate_metadata_union.ts +24 -24
  65. package/src/functional/$any.ts +2 -2
  66. package/src/functional/$dictionary.ts +25 -25
  67. package/src/functional/$every.ts +11 -11
  68. package/src/functional/$guard.ts +35 -35
  69. package/src/functional/$is_between.ts +2 -2
  70. package/src/functional/$is_custom.ts +14 -14
  71. package/src/functional/$is_date.ts +3 -3
  72. package/src/functional/$is_datetime.ts +2 -2
  73. package/src/functional/$is_email.ts +4 -4
  74. package/src/functional/$is_ipv4.ts +4 -4
  75. package/src/functional/$is_ipv6.ts +4 -4
  76. package/src/functional/$is_url.ts +4 -4
  77. package/src/functional/$is_uuid.ts +4 -4
  78. package/src/functional/$join.ts +46 -46
  79. package/src/functional/$number.ts +12 -12
  80. package/src/functional/$report.ts +15 -15
  81. package/src/functional/$rest.ts +3 -3
  82. package/src/functional/$string.ts +50 -50
  83. package/src/functional/$tail.ts +5 -5
  84. package/src/functional/Namespace.ts +127 -127
  85. package/src/index.ts +4 -4
  86. package/src/metadata/ICommentTag.ts +4 -4
  87. package/src/metadata/IJsDocTagInfo.ts +10 -10
  88. package/src/metadata/IMetadata.ts +28 -28
  89. package/src/metadata/IMetadataAlias.ts +14 -14
  90. package/src/metadata/IMetadataApplication.ts +7 -7
  91. package/src/metadata/IMetadataArray.ts +10 -10
  92. package/src/metadata/IMetadataCollection.ts +11 -11
  93. package/src/metadata/IMetadataConstant.ts +16 -16
  94. package/src/metadata/IMetadataDictionary.ts +14 -14
  95. package/src/metadata/IMetadataEntry.ts +6 -6
  96. package/src/metadata/IMetadataObject.ts +18 -18
  97. package/src/metadata/IMetadataProperty.ts +11 -11
  98. package/src/metadata/IMetadataResolved.ts +6 -6
  99. package/src/metadata/IMetadataTag.ts +105 -105
  100. package/src/metadata/IMetadataTuple.ts +10 -10
  101. package/src/metadata/Metadata.ts +607 -607
  102. package/src/metadata/MetadataAlias.ts +66 -66
  103. package/src/metadata/MetadataArray.ts +55 -55
  104. package/src/metadata/MetadataConstant.ts +3 -3
  105. package/src/metadata/MetadataObject.ts +129 -129
  106. package/src/metadata/MetadataProperty.ts +64 -64
  107. package/src/metadata/MetadataResolved.ts +51 -51
  108. package/src/metadata/MetadataTuple.ts +53 -53
  109. package/src/module.ts +2038 -2038
  110. package/src/programmers/ApplicationProgrammer.ts +47 -47
  111. package/src/programmers/AssertCloneProgrammer.ts +71 -71
  112. package/src/programmers/AssertParseProgrammer.ts +66 -66
  113. package/src/programmers/AssertProgrammer.ts +279 -279
  114. package/src/programmers/AssertPruneProgrammer.ts +68 -68
  115. package/src/programmers/AssertStringifyProgrammer.ts +66 -66
  116. package/src/programmers/CheckerProgrammer.ts +1173 -1182
  117. package/src/programmers/CloneProgrammer.ts +587 -587
  118. package/src/programmers/FeatureProgrammer.ts +495 -495
  119. package/src/programmers/IsCloneProgrammer.ts +78 -78
  120. package/src/programmers/IsParseProgrammer.ts +72 -72
  121. package/src/programmers/IsProgrammer.ts +239 -239
  122. package/src/programmers/IsPruneProgrammer.ts +73 -73
  123. package/src/programmers/IsStringifyProgrammer.ts +76 -76
  124. package/src/programmers/LiteralsProgrammer.ts +63 -63
  125. package/src/programmers/PruneProgrammer.ts +542 -542
  126. package/src/programmers/RandomProgrammer.ts +579 -579
  127. package/src/programmers/StringifyProgrammer.ts +986 -978
  128. package/src/programmers/TypiaProgrammer.ts +129 -129
  129. package/src/programmers/ValidateCloneProgrammer.ts +85 -85
  130. package/src/programmers/ValidateParseProgrammer.ts +70 -70
  131. package/src/programmers/ValidateProgrammer.ts +305 -305
  132. package/src/programmers/ValidatePruneProgrammer.ts +78 -78
  133. package/src/programmers/ValidateStringifyProgrammer.ts +84 -84
  134. package/src/programmers/helpers/AtomicPredicator.ts +31 -31
  135. package/src/programmers/helpers/CloneJoiner.ts +131 -131
  136. package/src/programmers/helpers/FunctionImporeter.ts +78 -78
  137. package/src/programmers/helpers/ICheckEntry.ts +12 -12
  138. package/src/programmers/helpers/IExpressionEntry.ts +12 -12
  139. package/src/programmers/helpers/OptionPredicator.ts +15 -15
  140. package/src/programmers/helpers/PruneJoiner.ts +143 -143
  141. package/src/programmers/helpers/RandomJoiner.ts +173 -173
  142. package/src/programmers/helpers/RandomRanger.ts +216 -216
  143. package/src/programmers/helpers/StringifyJoinder.ts +113 -113
  144. package/src/programmers/helpers/StringifyPredicator.ts +13 -13
  145. package/src/programmers/helpers/UnionExplorer.ts +301 -301
  146. package/src/programmers/helpers/UnionPredicator.ts +81 -81
  147. package/src/programmers/helpers/disable_function_importer_declare.ts +26 -26
  148. package/src/programmers/internal/JSON_SCHEMA_PREFIX.ts +1 -1
  149. package/src/programmers/internal/application_alias.ts +66 -66
  150. package/src/programmers/internal/application_array.ts +30 -30
  151. package/src/programmers/internal/application_boolean.ts +15 -15
  152. package/src/programmers/internal/application_constant.ts +26 -26
  153. package/src/programmers/internal/application_default.ts +17 -17
  154. package/src/programmers/internal/application_default_string.ts +33 -33
  155. package/src/programmers/internal/application_native.ts +39 -39
  156. package/src/programmers/internal/application_number.ts +74 -74
  157. package/src/programmers/internal/application_object.ts +165 -165
  158. package/src/programmers/internal/application_resolved.ts +55 -55
  159. package/src/programmers/internal/application_schema.ts +157 -157
  160. package/src/programmers/internal/application_string.ts +44 -44
  161. package/src/programmers/internal/application_templates.ts +25 -25
  162. package/src/programmers/internal/application_tuple.ts +57 -57
  163. package/src/programmers/internal/check_array.ts +30 -30
  164. package/src/programmers/internal/check_array_length.ts +35 -35
  165. package/src/programmers/internal/check_bigint.ts +82 -82
  166. package/src/programmers/internal/check_custom.ts +31 -31
  167. package/src/programmers/internal/check_dynamic_properties.ts +195 -195
  168. package/src/programmers/internal/check_everything.ts +28 -28
  169. package/src/programmers/internal/check_native.ts +21 -21
  170. package/src/programmers/internal/check_number.ts +177 -177
  171. package/src/programmers/internal/check_object.ts +55 -55
  172. package/src/programmers/internal/check_string.ts +25 -25
  173. package/src/programmers/internal/check_string_tags.ts +67 -67
  174. package/src/programmers/internal/check_template.ts +56 -56
  175. package/src/programmers/internal/check_union_array_like.ts +329 -329
  176. package/src/programmers/internal/decode_union_object.ts +73 -73
  177. package/src/programmers/internal/feature_object_entries.ts +63 -63
  178. package/src/programmers/internal/get_comment_tags.ts +23 -23
  179. package/src/programmers/internal/metadata_to_pattern.ts +34 -34
  180. package/src/programmers/internal/prune_object_properties.ts +60 -60
  181. package/src/programmers/internal/random_custom.ts +29 -29
  182. package/src/programmers/internal/stringify_dynamic_properties.ts +171 -171
  183. package/src/programmers/internal/stringify_native.ts +7 -7
  184. package/src/programmers/internal/stringify_regular_properties.ts +83 -83
  185. package/src/programmers/internal/template_to_pattern.ts +15 -15
  186. package/src/programmers/internal/wrap_metadata_rest_tuple.ts +16 -16
  187. package/src/schemas/IJsonApplication.ts +8 -8
  188. package/src/schemas/IJsonComponents.ts +33 -33
  189. package/src/schemas/IJsonSchema.ts +133 -133
  190. package/src/transform.ts +27 -27
  191. package/src/transformers/CallExpressionTransformer.ts +179 -179
  192. package/src/transformers/FileTransformer.ts +47 -47
  193. package/src/transformers/IProject.ts +11 -11
  194. package/src/transformers/ITransformOptions.ts +62 -62
  195. package/src/transformers/ImportTransformer.ts +66 -66
  196. package/src/transformers/NodeTransformer.ts +13 -13
  197. package/src/transformers/features/miscellaneous/ApplicationTransformer.ts +112 -104
  198. package/src/transformers/features/miscellaneous/AssertCloneTransformer.ts +9 -9
  199. package/src/transformers/features/miscellaneous/AssertPruneTransformer.ts +9 -9
  200. package/src/transformers/features/miscellaneous/CloneTransformer.ts +9 -9
  201. package/src/transformers/features/miscellaneous/CreateAssertCloneTransformer.ts +9 -9
  202. package/src/transformers/features/miscellaneous/CreateAssertPruneTransformer.ts +9 -9
  203. package/src/transformers/features/miscellaneous/CreateCloneTransformer.ts +9 -9
  204. package/src/transformers/features/miscellaneous/CreateIsCloneTransformer.ts +9 -9
  205. package/src/transformers/features/miscellaneous/CreateIsPruneTransformer.ts +9 -9
  206. package/src/transformers/features/miscellaneous/CreatePruneTransformer.ts +9 -9
  207. package/src/transformers/features/miscellaneous/CreateRandomTransformer.ts +39 -39
  208. package/src/transformers/features/miscellaneous/CreateValidateCloneTransformer.ts +9 -9
  209. package/src/transformers/features/miscellaneous/CreateValidatePruneTransformer.ts +9 -9
  210. package/src/transformers/features/miscellaneous/IsCloneTransformer.ts +9 -9
  211. package/src/transformers/features/miscellaneous/IsPruneTransformer.ts +9 -9
  212. package/src/transformers/features/miscellaneous/LiteralsTransformer.ts +28 -28
  213. package/src/transformers/features/miscellaneous/MetadataTransformer.ts +53 -53
  214. package/src/transformers/features/miscellaneous/PruneTransformer.ts +9 -9
  215. package/src/transformers/features/miscellaneous/RandomTransformer.ts +42 -42
  216. package/src/transformers/features/miscellaneous/ValidateCloneTransformer.ts +9 -9
  217. package/src/transformers/features/miscellaneous/ValidatePruneTransformer.ts +9 -9
  218. package/src/transformers/features/parsers/AssertParseTransformer.ts +9 -9
  219. package/src/transformers/features/parsers/CreateAssertParseTransformer.ts +9 -9
  220. package/src/transformers/features/parsers/CreateIsParseTransformer.ts +9 -9
  221. package/src/transformers/features/parsers/CreateValidateParseTransformer.ts +9 -9
  222. package/src/transformers/features/parsers/IsParseTransformer.ts +9 -9
  223. package/src/transformers/features/parsers/ValidateParseTransformer.ts +9 -9
  224. package/src/transformers/features/stringifiers/AssertStringifyTransformer.ts +10 -10
  225. package/src/transformers/features/stringifiers/CreateAssertStringifyTransformer.ts +12 -12
  226. package/src/transformers/features/stringifiers/CreateIsStringifyTransformer.ts +9 -9
  227. package/src/transformers/features/stringifiers/CreateStringifyTransformer.ts +9 -9
  228. package/src/transformers/features/stringifiers/CreateValidateStringifyProgrammer.ts +12 -12
  229. package/src/transformers/features/stringifiers/IsStringifyTransformer.ts +9 -9
  230. package/src/transformers/features/stringifiers/StringifyTransformer.ts +9 -9
  231. package/src/transformers/features/stringifiers/ValidateStringifyTransformer.ts +10 -10
  232. package/src/transformers/features/validators/AssertTransformer.ts +11 -11
  233. package/src/transformers/features/validators/CreateAssertTransformer.ts +13 -13
  234. package/src/transformers/features/validators/CreateIsTransformer.ts +11 -11
  235. package/src/transformers/features/validators/CreateValidateTransformer.ts +13 -13
  236. package/src/transformers/features/validators/IsTransformer.ts +11 -11
  237. package/src/transformers/features/validators/ValidateTransformer.ts +11 -11
  238. package/src/transformers/internal/GenericTransformer.ts +97 -97
  239. package/src/typings/Atomic.ts +17 -17
  240. package/src/typings/ClassProperties.ts +5 -5
  241. package/src/typings/Customizable.ts +5 -5
  242. package/src/typings/OmitNever.ts +3 -3
  243. package/src/typings/SpecialFields.ts +3 -3
  244. package/src/typings/Writable.ts +11 -11
  245. package/src/utils/ArrayUtil.ts +45 -45
  246. package/src/utils/Escaper.ts +46 -46
  247. package/src/utils/MapUtil.ts +12 -12
  248. package/src/utils/PatternUtil.ts +33 -33
  249. package/src/utils/RandomGenerator.ts +81 -81
  250. package/src/utils/Singleton.ts +17 -17
@@ -1,978 +1,986 @@
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
- import { ValueFactory } from "../factories/ValueFactory";
10
-
11
- import { IJsDocTagInfo } from "../metadata/IJsDocTagInfo";
12
- import { IMetadataTag } from "../metadata/IMetadataTag";
13
- import { Metadata } from "../metadata/Metadata";
14
- import { MetadataArray } from "../metadata/MetadataArray";
15
- import { MetadataObject } from "../metadata/MetadataObject";
16
- import { MetadataTuple } from "../metadata/MetadataTuple";
17
-
18
- import { IProject } from "../transformers/IProject";
19
-
20
- import { Atomic } from "../typings/Atomic";
21
-
22
- import { ArrayUtil } from "../utils/ArrayUtil";
23
-
24
- import { FeatureProgrammer } from "./FeatureProgrammer";
25
- import { IsProgrammer } from "./IsProgrammer";
26
- import { AtomicPredicator } from "./helpers/AtomicPredicator";
27
- import { FunctionImporter } from "./helpers/FunctionImporeter";
28
- import { IExpressionEntry } from "./helpers/IExpressionEntry";
29
- import { OptionPredicator } from "./helpers/OptionPredicator";
30
- import { StringifyJoiner } from "./helpers/StringifyJoinder";
31
- import { StringifyPredicator } from "./helpers/StringifyPredicator";
32
- import { UnionExplorer } from "./helpers/UnionExplorer";
33
- import { check_native } from "./internal/check_native";
34
- import { decode_union_object } from "./internal/decode_union_object";
35
- import { feature_object_entries } from "./internal/feature_object_entries";
36
- import { wrap_metadata_rest_tuple } from "./internal/wrap_metadata_rest_tuple";
37
-
38
- export namespace StringifyProgrammer {
39
- /* -----------------------------------------------------------
40
- WRITER
41
- ----------------------------------------------------------- */
42
-
43
- export const write =
44
- (project: IProject) => (modulo: ts.LeftHandSideExpression) => {
45
- const importer: FunctionImporter = new FunctionImporter();
46
- const config: FeatureProgrammer.IConfig =
47
- configure(project)(importer);
48
-
49
- return FeatureProgrammer.write(project)({
50
- ...config,
51
- addition: (collection) => [
52
- ...IsProgrammer.write_function_statements(project)(
53
- importer,
54
- )(collection),
55
- ...importer.declare(modulo),
56
- ],
57
- })(importer);
58
- };
59
-
60
- const write_array_functions =
61
- (config: FeatureProgrammer.IConfig) =>
62
- (importer: FunctionImporter) =>
63
- (collection: MetadataCollection): ts.VariableStatement[] =>
64
- collection
65
- .arrays()
66
- .filter((a) => a.recursive)
67
- .map((array, i) =>
68
- StatementFactory.constant(
69
- `${config.prefix}a${i}`,
70
- ts.factory.createArrowFunction(
71
- undefined,
72
- undefined,
73
- FeatureProgrammer.parameterDeclarations(config)(
74
- TypeFactory.keyword("any"),
75
- )(ts.factory.createIdentifier("input")),
76
- TypeFactory.keyword("any"),
77
- undefined,
78
- decode_array_inline(config)(importer)(
79
- ts.factory.createIdentifier("input"),
80
- array,
81
- {
82
- tracable: config.trace,
83
- source: "function",
84
- from: "array",
85
- postfix: "",
86
- },
87
- ),
88
- ),
89
- ),
90
- );
91
-
92
- const write_tuple_functions =
93
- (project: IProject) =>
94
- (config: FeatureProgrammer.IConfig) =>
95
- (importer: FunctionImporter) =>
96
- (collection: MetadataCollection): ts.VariableStatement[] =>
97
- collection
98
- .tuples()
99
- .filter((t) => t.recursive)
100
- .map((tuple, i) =>
101
- StatementFactory.constant(
102
- `${config.prefix}t${i}`,
103
- ts.factory.createArrowFunction(
104
- undefined,
105
- undefined,
106
- FeatureProgrammer.parameterDeclarations(config)(
107
- TypeFactory.keyword("any"),
108
- )(ts.factory.createIdentifier("input")),
109
- TypeFactory.keyword("any"),
110
- undefined,
111
- decode_tuple_inline(project)(config)(importer)(
112
- ts.factory.createIdentifier("input"),
113
- tuple,
114
- {
115
- tracable: config.trace,
116
- source: "function",
117
- from: "array",
118
- postfix: "",
119
- },
120
- ),
121
- ),
122
- ),
123
- );
124
-
125
- /* -----------------------------------------------------------
126
- DECODERS
127
- ----------------------------------------------------------- */
128
- const decode =
129
- (project: IProject) =>
130
- (config: FeatureProgrammer.IConfig) =>
131
- (importer: FunctionImporter) =>
132
- (
133
- input: ts.Expression,
134
- meta: Metadata,
135
- explore: FeatureProgrammer.IExplore,
136
- ): ts.Expression => {
137
- // ANY TYPE
138
- if (meta.any === true)
139
- return wrap_required(
140
- input,
141
- meta,
142
- explore,
143
- )(
144
- wrap_functional(
145
- input,
146
- meta,
147
- explore,
148
- )(
149
- ts.factory.createCallExpression(
150
- ts.factory.createIdentifier("JSON.stringify"),
151
- undefined,
152
- [input],
153
- ),
154
- ),
155
- );
156
-
157
- // ONLY NULL OR UNDEFINED
158
- const size: number = meta.size();
159
- if (
160
- size === 0 &&
161
- (meta.isRequired() === false || meta.nullable === true)
162
- ) {
163
- if (meta.isRequired() === false && meta.nullable === true)
164
- return explore.from === "array"
165
- ? ts.factory.createStringLiteral("null")
166
- : ts.factory.createConditionalExpression(
167
- ts.factory.createStrictEquality(
168
- ts.factory.createNull(),
169
- input,
170
- ),
171
- undefined,
172
- ts.factory.createStringLiteral("null"),
173
- undefined,
174
- ts.factory.createIdentifier("undefined"),
175
- );
176
- else if (meta.isRequired() === false)
177
- return explore.from === "array"
178
- ? ts.factory.createStringLiteral("null")
179
- : ts.factory.createIdentifier("undefined");
180
- else return ts.factory.createStringLiteral("null");
181
- }
182
-
183
- //----
184
- // LIST UP UNION TYPES
185
- //----
186
- const unions: IUnion[] = [];
187
-
188
- // toJSON() METHOD
189
- if (meta.resolved !== null)
190
- unions.push({
191
- type: "resolved",
192
- is: () => IsProgrammer.decode_to_json(false)(input),
193
- value: () =>
194
- decode_to_json(project)(config)(importer)(
195
- input,
196
- meta.resolved!.returns,
197
- explore,
198
- ),
199
- });
200
- else if (meta.functional === true)
201
- unions.push({
202
- type: "functional",
203
- is: () => IsProgrammer.decode_functional(input),
204
- value: () => decode_functional(explore),
205
- });
206
-
207
- // TEMPLATES
208
- if (
209
- meta.templates.length ||
210
- ArrayUtil.has(meta.constants, (c) => c.type === "string")
211
- )
212
- if (AtomicPredicator.template(meta)) {
213
- const partial = Metadata.initialize();
214
- partial.atomics.push("string"),
215
- unions.push({
216
- type: "template literal",
217
- is: () =>
218
- IsProgrammer.decode(project)(importer)(
219
- input,
220
- partial,
221
- explore,
222
- [],
223
- [],
224
- ),
225
- value: () =>
226
- decode_atomic(project)(importer)(
227
- input,
228
- "string",
229
- explore,
230
- ),
231
- });
232
- }
233
-
234
- // CONSTANTS
235
- for (const constant of meta.constants)
236
- if (AtomicPredicator.constant(meta)(constant.type) === false)
237
- continue;
238
- else if (constant.type !== "string")
239
- unions.push({
240
- type: "atomic",
241
- is: () =>
242
- IsProgrammer.decode(project)(importer)(
243
- input,
244
- (() => {
245
- const partial = Metadata.initialize();
246
- partial.atomics.push(constant.type);
247
- return partial;
248
- })(),
249
- explore,
250
- [],
251
- [],
252
- ),
253
- value: () =>
254
- decode_atomic(project)(importer)(
255
- input,
256
- constant.type,
257
- explore,
258
- ),
259
- });
260
- else if (meta.templates.length === 0)
261
- unions.push({
262
- type: "const string",
263
- is: () =>
264
- IsProgrammer.decode(project)(importer)(
265
- input,
266
- (() => {
267
- const partial = Metadata.initialize();
268
- partial.atomics.push("string");
269
- return partial;
270
- })(),
271
- explore,
272
- [],
273
- [],
274
- ),
275
- value: () =>
276
- decode_constant_string(project)(importer)(
277
- input,
278
- [...constant.values] as string[],
279
- explore,
280
- ),
281
- });
282
-
283
- /// ATOMICS
284
- for (const type of meta.atomics)
285
- if (AtomicPredicator.atomic(meta)(type))
286
- unions.push({
287
- type: "atomic",
288
- is: () =>
289
- IsProgrammer.decode(project)(importer)(
290
- input,
291
- (() => {
292
- const partial = Metadata.initialize();
293
- partial.atomics.push(type);
294
- return partial;
295
- })(),
296
- explore,
297
- [],
298
- [],
299
- ),
300
- value: () =>
301
- decode_atomic(project)(importer)(
302
- input,
303
- type,
304
- explore,
305
- ),
306
- });
307
-
308
- // TUPLES
309
- for (const tuple of meta.tuples) {
310
- for (const child of tuple.elements)
311
- if (StringifyPredicator.undefindable(meta))
312
- throw new Error(
313
- `Error on typia.stringify(): tuple cannot contain undefined value - (${child.getName()}).`,
314
- );
315
- unions.push({
316
- type: "tuple",
317
- is: () =>
318
- IsProgrammer.decode(project)(importer)(
319
- input,
320
- (() => {
321
- const partial = Metadata.initialize();
322
- partial.tuples.push(tuple);
323
- return partial;
324
- })(),
325
- explore,
326
- [],
327
- [],
328
- ),
329
- value: () =>
330
- decode_tuple(project)(config)(importer)(
331
- input,
332
- tuple,
333
- explore,
334
- ),
335
- });
336
- }
337
-
338
- // ARRAYS
339
- if (meta.arrays.length) {
340
- for (const child of meta.arrays)
341
- if (StringifyPredicator.undefindable(child.value))
342
- throw new Error(
343
- `Error on typia.stringify(): array cannot contain undefined value (${child.value.getName()}).`,
344
- );
345
- const value: () => ts.Expression =
346
- meta.arrays.length === 1
347
- ? () =>
348
- decode_array(config)(importer)(
349
- input,
350
- meta.arrays[0]!,
351
- {
352
- ...explore,
353
- from: "array",
354
- },
355
- )
356
- : meta.arrays.some((elem) => elem.value.any)
357
- ? () =>
358
- ts.factory.createCallExpression(
359
- ts.factory.createIdentifier("JSON.stringify"),
360
- undefined,
361
- [input],
362
- )
363
- : () =>
364
- explore_arrays(project)(config)(importer)(
365
- input,
366
- meta.arrays,
367
- {
368
- ...explore,
369
- from: "array",
370
- },
371
- );
372
-
373
- unions.push({
374
- type: "array",
375
- is: () => ExpressionFactory.isArray(input),
376
- value,
377
- });
378
- }
379
-
380
- // BUILT-IN CLASSES
381
- if (meta.natives.length)
382
- for (const native of meta.natives)
383
- unions.push({
384
- type: "object",
385
- is: () => check_native(native)(input),
386
- value: () =>
387
- AtomicPredicator.native(native)
388
- ? decode_atomic(project)(importer)(
389
- input,
390
- native.toLowerCase() as Atomic.Literal,
391
- explore,
392
- )
393
- : ts.factory.createStringLiteral("{}"),
394
- });
395
-
396
- // SETS
397
- if (meta.sets.length)
398
- unions.push({
399
- type: "object",
400
- is: () => ExpressionFactory.isInstanceOf("Set")(input),
401
- value: () => ts.factory.createStringLiteral("{}"),
402
- });
403
-
404
- // MAPS
405
- if (meta.maps.length)
406
- unions.push({
407
- type: "object",
408
- is: () => ExpressionFactory.isInstanceOf("Map")(input),
409
- value: () => ts.factory.createStringLiteral("{}"),
410
- });
411
-
412
- // OBJECTS
413
- if (meta.objects.length)
414
- unions.push({
415
- type: "object",
416
- is: () =>
417
- ExpressionFactory.isObject({
418
- checkNull: true,
419
- checkArray: meta.objects.some((obj) =>
420
- obj.properties.every(
421
- (prop) =>
422
- !prop.key.isSoleLiteral() ||
423
- !prop.value.isRequired(),
424
- ),
425
- ),
426
- })(input),
427
- value: () =>
428
- meta.isParentResolved() === false &&
429
- meta.objects.length === 1 &&
430
- meta.objects[0]!._Is_simple(
431
- explore.from === "top" ? 0 : 1,
432
- )
433
- ? (() => {
434
- const obj: MetadataObject = meta.objects[0]!;
435
- const entries: IExpressionEntry<ts.Expression>[] =
436
- feature_object_entries({
437
- decoder: () =>
438
- decode(project)(config)(importer),
439
- trace: false,
440
- path: false,
441
- })(importer)(obj)(
442
- ts.factory.createAsExpression(
443
- input,
444
- TypeFactory.keyword("any"),
445
- ),
446
- );
447
- return StringifyJoiner.object(importer)(
448
- ts.factory.createAsExpression(
449
- input,
450
- TypeFactory.keyword("any"),
451
- ),
452
- entries,
453
- );
454
- })()
455
- : explore_objects(config)(importer)(input, meta, {
456
- ...explore,
457
- from: "object",
458
- }),
459
- });
460
-
461
- //----
462
- // RETURNS
463
- //----
464
- // CHECK NULL AND UNDEFINED
465
- const wrapper = (output: ts.Expression) =>
466
- wrap_required(
467
- input,
468
- meta,
469
- explore,
470
- )(wrap_nullable(input, meta)(output));
471
-
472
- // DIRECT RETURN
473
- if (unions.length === 0)
474
- return ts.factory.createCallExpression(
475
- ts.factory.createIdentifier("JSON.stringify"),
476
- undefined,
477
- [input],
478
- );
479
- else if (unions.length === 1) return wrapper(unions[0]!.value());
480
-
481
- // RETURN WITH TYPE CHECKING
482
- return wrapper(
483
- ts.factory.createCallExpression(
484
- ts.factory.createArrowFunction(
485
- undefined,
486
- undefined,
487
- [],
488
- undefined,
489
- undefined,
490
- iterate(importer, input, unions, meta.getName()),
491
- ),
492
- undefined,
493
- undefined,
494
- ),
495
- );
496
- };
497
-
498
- const decode_object = (importer: FunctionImporter) =>
499
- FeatureProgrammer.decode_object({
500
- trace: false,
501
- path: false,
502
- prefix: PREFIX,
503
- })(importer);
504
-
505
- const decode_array =
506
- (config: FeatureProgrammer.IConfig) =>
507
- (importer: FunctionImporter) =>
508
- (
509
- input: ts.Expression,
510
- array: MetadataArray,
511
- explore: FeatureProgrammer.IExplore,
512
- ) =>
513
- array.recursive
514
- ? ts.factory.createCallExpression(
515
- ts.factory.createIdentifier(
516
- importer.useLocal(`${config.prefix}a${array.index}`),
517
- ),
518
- undefined,
519
- FeatureProgrammer.argumentsArray(config)({
520
- ...explore,
521
- source: "function",
522
- from: "array",
523
- })(input),
524
- )
525
- : decode_array_inline(config)(importer)(input, array, explore);
526
-
527
- const decode_array_inline =
528
- (config: FeatureProgrammer.IConfig) =>
529
- (importer: FunctionImporter) =>
530
- (
531
- input: ts.Expression,
532
- array: MetadataArray,
533
- explore: FeatureProgrammer.IExplore,
534
- ) =>
535
- FeatureProgrammer.decode_array(config)(importer)(
536
- StringifyJoiner.array,
537
- )(input, array, explore, [], []);
538
-
539
- const decode_tuple =
540
- (project: IProject) =>
541
- (config: FeatureProgrammer.IConfig) =>
542
- (importer: FunctionImporter) =>
543
- (
544
- input: ts.Expression,
545
- tuple: MetadataTuple,
546
- explore: FeatureProgrammer.IExplore,
547
- ): ts.Expression =>
548
- tuple.recursive
549
- ? ts.factory.createCallExpression(
550
- ts.factory.createIdentifier(
551
- importer.useLocal(`${config.prefix}t${tuple.index}`),
552
- ),
553
- undefined,
554
- FeatureProgrammer.argumentsArray(config)({
555
- ...explore,
556
- source: "function",
557
- })(input),
558
- )
559
- : decode_tuple_inline(project)(config)(importer)(
560
- input,
561
- tuple,
562
- explore,
563
- );
564
-
565
- const decode_tuple_inline =
566
- (project: IProject) =>
567
- (config: FeatureProgrammer.IConfig) =>
568
- (importer: FunctionImporter) =>
569
- (
570
- input: ts.Expression,
571
- tuple: MetadataTuple,
572
- explore: FeatureProgrammer.IExplore,
573
- ): ts.Expression => {
574
- const children: ts.Expression[] = tuple.elements
575
- .filter((elem) => elem.rest === null)
576
- .map((elem, index) =>
577
- decode(project)(config)(importer)(
578
- ts.factory.createElementAccessExpression(input, index),
579
- elem,
580
- {
581
- ...explore,
582
- from: "array",
583
- postfix: explore.postfix.length
584
- ? `${explore.postfix.slice(0, -1)}[${index}]"`
585
- : `"[${index}]"`,
586
- },
587
- ),
588
- );
589
- const rest = (() => {
590
- if (tuple.elements.length === 0) return null;
591
- const last = tuple.elements.at(-1)!;
592
- if (last.rest === null) return null;
593
-
594
- const code = decode(project)(config)(importer)(
595
- ts.factory.createCallExpression(
596
- IdentifierFactory.access(input)("slice"),
597
- undefined,
598
- [
599
- ts.factory.createNumericLiteral(
600
- tuple.elements.length - 1,
601
- ),
602
- ],
603
- ),
604
- wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
605
- {
606
- ...explore,
607
- start: tuple.elements.length - 1,
608
- },
609
- );
610
- return ts.factory.createCallExpression(
611
- importer.use("rest"),
612
- undefined,
613
- [code],
614
- );
615
- })();
616
- return StringifyJoiner.tuple(children, rest);
617
- };
618
-
619
- const decode_atomic =
620
- (project: IProject) =>
621
- (importer: FunctionImporter) =>
622
- (
623
- input: ts.Expression,
624
- type: string,
625
- explore: FeatureProgrammer.IExplore,
626
- ) => {
627
- if (type === "string")
628
- return ts.factory.createCallExpression(
629
- importer.use("string"),
630
- undefined,
631
- [input],
632
- );
633
- else if (
634
- type === "number" &&
635
- OptionPredicator.numeric(project.options)
636
- )
637
- input = ts.factory.createCallExpression(
638
- importer.use("number"),
639
- undefined,
640
- [input],
641
- );
642
-
643
- return explore.from !== "top"
644
- ? input
645
- : ts.factory.createCallExpression(
646
- IdentifierFactory.access(input)("toString"),
647
- undefined,
648
- undefined,
649
- );
650
- };
651
-
652
- const decode_constant_string =
653
- (project: IProject) =>
654
- (importer: FunctionImporter) =>
655
- (
656
- input: ts.Expression,
657
- values: string[],
658
- explore: FeatureProgrammer.IExplore,
659
- ): ts.Expression => {
660
- if (values.every((v) => !StringifyPredicator.require_escape(v)))
661
- return [
662
- ts.factory.createStringLiteral('"'),
663
- input,
664
- ts.factory.createStringLiteral('"'),
665
- ].reduce((x, y) => ts.factory.createAdd(x, y));
666
- else
667
- return decode_atomic(project)(importer)(
668
- input,
669
- "string",
670
- explore,
671
- );
672
- };
673
-
674
- const decode_to_json =
675
- (project: IProject) =>
676
- (config: FeatureProgrammer.IConfig) =>
677
- (importer: FunctionImporter) =>
678
- (
679
- input: ts.Expression,
680
- resolved: Metadata,
681
- explore: FeatureProgrammer.IExplore,
682
- ): ts.Expression => {
683
- return decode(project)(config)(importer)(
684
- ts.factory.createCallExpression(
685
- IdentifierFactory.access(input)("toJSON"),
686
- undefined,
687
- [],
688
- ),
689
- resolved,
690
- explore,
691
- );
692
- };
693
-
694
- const decode_functional = (explore: FeatureProgrammer.IExplore) =>
695
- explore.from === "array"
696
- ? ts.factory.createStringLiteral("null")
697
- : ts.factory.createIdentifier("undefined");
698
-
699
- /* -----------------------------------------------------------
700
- EXPLORERS
701
- ----------------------------------------------------------- */
702
- const explore_objects =
703
- (config: FeatureProgrammer.IConfig) =>
704
- (importer: FunctionImporter) =>
705
- (
706
- input: ts.Expression,
707
- meta: Metadata,
708
- explore: FeatureProgrammer.IExplore,
709
- ) =>
710
- meta.objects.length === 1
711
- ? decode_object(importer)(input, meta.objects[0]!, explore)
712
- : ts.factory.createCallExpression(
713
- ts.factory.createIdentifier(
714
- importer.useLocal(`${PREFIX}u${meta.union_index!}`),
715
- ),
716
- undefined,
717
- FeatureProgrammer.argumentsArray(config)(explore)(input),
718
- );
719
-
720
- const explore_arrays =
721
- (project: IProject) =>
722
- (config: FeatureProgrammer.IConfig) =>
723
- (importer: FunctionImporter) =>
724
- (
725
- input: ts.Expression,
726
- elements: MetadataArray[],
727
- explore: FeatureProgrammer.IExplore,
728
- ): ts.Expression =>
729
- explore_array_like_union_types(config)(importer)(
730
- UnionExplorer.array({
731
- checker: IsProgrammer.decode(project)(importer),
732
- decoder: decode_array(config)(importer),
733
- empty: ts.factory.createStringLiteral("[]"),
734
- success: ts.factory.createTrue(),
735
- failure: (input, expected) =>
736
- create_throw_error(importer)(expected)(input),
737
- }),
738
- )(input, elements, explore);
739
-
740
- const explore_array_like_union_types =
741
- (config: FeatureProgrammer.IConfig) =>
742
- (importer: FunctionImporter) =>
743
- <T extends MetadataArray | MetadataTuple>(
744
- factory: (
745
- parameters: ts.ParameterDeclaration[],
746
- ) => (
747
- input: ts.Expression,
748
- elements: T[],
749
- explore: FeatureProgrammer.IExplore,
750
- tags: IMetadataTag[],
751
- jsDocTags: IJsDocTagInfo[],
752
- ) => ts.ArrowFunction,
753
- ) =>
754
- (
755
- input: ts.Expression,
756
- elements: T[],
757
- explore: FeatureProgrammer.IExplore,
758
- ): ts.Expression => {
759
- const arrow =
760
- (parameters: ts.ParameterDeclaration[]) =>
761
- (explore: FeatureProgrammer.IExplore) =>
762
- (input: ts.Expression): ts.ArrowFunction =>
763
- factory(parameters)(input, elements, explore, [], []);
764
- if (elements.every((e) => e.recursive === false))
765
- ts.factory.createCallExpression(
766
- arrow([])(explore)(input),
767
- undefined,
768
- [],
769
- );
770
-
771
- explore = {
772
- ...explore,
773
- source: "function",
774
- from: "array",
775
- };
776
- return ts.factory.createCallExpression(
777
- ts.factory.createIdentifier(
778
- importer.emplaceUnion(
779
- config.prefix,
780
- elements.map((e) => e.name).join(" | "),
781
- () =>
782
- arrow(
783
- FeatureProgrammer.parameterDeclarations(config)(
784
- TypeFactory.keyword("any"),
785
- )(ts.factory.createIdentifier("input")),
786
- )({
787
- ...explore,
788
- postfix: "",
789
- })(ts.factory.createIdentifier("input")),
790
- ),
791
- ),
792
- undefined,
793
- FeatureProgrammer.argumentsArray(config)(explore)(input),
794
- );
795
- };
796
-
797
- /* -----------------------------------------------------------
798
- RETURN SCRIPTS
799
- ----------------------------------------------------------- */
800
- const wrap_required = (
801
- input: ts.Expression,
802
- meta: Metadata,
803
- explore: FeatureProgrammer.IExplore,
804
- ): ((expression: ts.Expression) => ts.Expression) => {
805
- if (meta.isRequired() === true && meta.any === false)
806
- return (expression) => expression;
807
- return (expression) =>
808
- ts.factory.createConditionalExpression(
809
- ts.factory.createStrictInequality(
810
- ts.factory.createIdentifier("undefined"),
811
- input,
812
- ),
813
- undefined,
814
- expression,
815
- undefined,
816
- explore.from === "array"
817
- ? ts.factory.createStringLiteral("null")
818
- : ts.factory.createIdentifier("undefined"),
819
- );
820
- };
821
-
822
- const wrap_nullable = (
823
- input: ts.Expression,
824
- meta: Metadata,
825
- ): ((expression: ts.Expression) => ts.Expression) => {
826
- if (meta.nullable === false) return (expression) => expression;
827
- return (expression) =>
828
- ts.factory.createConditionalExpression(
829
- ts.factory.createStrictInequality(
830
- ts.factory.createNull(),
831
- input,
832
- ),
833
- undefined,
834
- expression,
835
- undefined,
836
- ts.factory.createStringLiteral("null"),
837
- );
838
- };
839
-
840
- const wrap_functional = (
841
- input: ts.Expression,
842
- meta: Metadata,
843
- explore: FeatureProgrammer.IExplore,
844
- ): ((expression: ts.Expression) => ts.Expression) => {
845
- if (meta.functional === false) return (expression) => expression;
846
- return (expression) =>
847
- ts.factory.createConditionalExpression(
848
- ts.factory.createStrictInequality(
849
- ts.factory.createStringLiteral("function"),
850
- ValueFactory.TYPEOF(input),
851
- ),
852
- undefined,
853
- expression,
854
- undefined,
855
- decode_functional(explore),
856
- );
857
- };
858
-
859
- const iterate = (
860
- importer: FunctionImporter,
861
- input: ts.Expression,
862
- unions: IUnion[],
863
- expected: string,
864
- ) =>
865
- ts.factory.createBlock(
866
- [
867
- ...unions.map((u) =>
868
- ts.factory.createIfStatement(
869
- u.is(),
870
- ts.factory.createReturnStatement(u.value()),
871
- ),
872
- ),
873
- create_throw_error(importer)(expected)(input),
874
- ],
875
- true,
876
- );
877
-
878
- /* -----------------------------------------------------------
879
- CONFIGURATIONS
880
- ----------------------------------------------------------- */
881
- const PREFIX = "$s";
882
-
883
- const configure =
884
- (project: IProject) =>
885
- (importer: FunctionImporter): FeatureProgrammer.IConfig => {
886
- const config: FeatureProgrammer.IConfig = {
887
- types: {
888
- input: (type, name) =>
889
- ts.factory.createTypeReferenceNode(
890
- name ??
891
- TypeFactory.getFullName(project.checker)(type),
892
- ),
893
- output: () => TypeFactory.keyword("string"),
894
- },
895
- prefix: PREFIX,
896
- trace: false,
897
- path: false,
898
- initializer,
899
- decoder: () => decode(project)(config)(importer),
900
- objector: {
901
- checker: () => (input, meta, explore) =>
902
- IsProgrammer.decode(project)(importer)(
903
- input,
904
- meta,
905
- explore,
906
- [],
907
- [],
908
- ),
909
- decoder: () => decode_object(importer),
910
- joiner: StringifyJoiner.object(importer),
911
- unionizer: decode_union_object(
912
- IsProgrammer.decode_object(importer),
913
- )(decode_object(importer))((exp) => exp)(
914
- (value, expected) =>
915
- create_throw_error(importer)(expected)(value),
916
- ),
917
- failure: (input, expected) =>
918
- create_throw_error(importer)(expected)(input),
919
- },
920
- generator: {
921
- arrays: () => write_array_functions(config)(importer),
922
- tuples: () =>
923
- write_tuple_functions(project)(config)(importer),
924
- },
925
- };
926
- return config;
927
- };
928
-
929
- const initializer: FeatureProgrammer.IConfig["initializer"] =
930
- ({ checker }) =>
931
- (type) => {
932
- const collection: MetadataCollection = new MetadataCollection();
933
- const meta: Metadata = MetadataFactory.analyze(checker)({
934
- resolve: true,
935
- constant: true,
936
- absorb: true,
937
- validate: (meta) => {
938
- if (meta.atomics.find((str) => str === "bigint"))
939
- throw new Error(NO_BIGINT);
940
- },
941
- })(collection)(type);
942
- return [collection, meta];
943
- };
944
-
945
- const create_throw_error =
946
- (importer: FunctionImporter) =>
947
- (expected: string) =>
948
- (value: ts.Expression) =>
949
- ts.factory.createExpressionStatement(
950
- ts.factory.createCallExpression(
951
- importer.use("throws"),
952
- [],
953
- [
954
- ts.factory.createObjectLiteralExpression(
955
- [
956
- ts.factory.createPropertyAssignment(
957
- "expected",
958
- ts.factory.createStringLiteral(expected),
959
- ),
960
- ts.factory.createPropertyAssignment(
961
- "value",
962
- value,
963
- ),
964
- ],
965
- true,
966
- ),
967
- ],
968
- ),
969
- );
970
- }
971
-
972
- interface IUnion {
973
- type: string;
974
- is: () => ts.Expression;
975
- value: () => ts.Expression;
976
- }
977
-
978
- const NO_BIGINT = "Error on typia.stringify(): does not allow bigint type.";
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
+ import { ValueFactory } from "../factories/ValueFactory";
10
+
11
+ import { IJsDocTagInfo } from "../metadata/IJsDocTagInfo";
12
+ import { IMetadataTag } from "../metadata/IMetadataTag";
13
+ import { Metadata } from "../metadata/Metadata";
14
+ import { MetadataArray } from "../metadata/MetadataArray";
15
+ import { MetadataObject } from "../metadata/MetadataObject";
16
+ import { MetadataTuple } from "../metadata/MetadataTuple";
17
+
18
+ import { IProject } from "../transformers/IProject";
19
+
20
+ import { Atomic } from "../typings/Atomic";
21
+
22
+ import { ArrayUtil } from "../utils/ArrayUtil";
23
+
24
+ import { FeatureProgrammer } from "./FeatureProgrammer";
25
+ import { IsProgrammer } from "./IsProgrammer";
26
+ import { AtomicPredicator } from "./helpers/AtomicPredicator";
27
+ import { FunctionImporter } from "./helpers/FunctionImporeter";
28
+ import { IExpressionEntry } from "./helpers/IExpressionEntry";
29
+ import { OptionPredicator } from "./helpers/OptionPredicator";
30
+ import { StringifyJoiner } from "./helpers/StringifyJoinder";
31
+ import { StringifyPredicator } from "./helpers/StringifyPredicator";
32
+ import { UnionExplorer } from "./helpers/UnionExplorer";
33
+ import { check_native } from "./internal/check_native";
34
+ import { decode_union_object } from "./internal/decode_union_object";
35
+ import { feature_object_entries } from "./internal/feature_object_entries";
36
+ import { wrap_metadata_rest_tuple } from "./internal/wrap_metadata_rest_tuple";
37
+
38
+ export namespace StringifyProgrammer {
39
+ /* -----------------------------------------------------------
40
+ WRITER
41
+ ----------------------------------------------------------- */
42
+
43
+ export const write =
44
+ (project: IProject) => (modulo: ts.LeftHandSideExpression) => {
45
+ const importer: FunctionImporter = new FunctionImporter();
46
+ const config: FeatureProgrammer.IConfig =
47
+ configure(project)(importer);
48
+
49
+ return FeatureProgrammer.write(project)({
50
+ ...config,
51
+ addition: (collection) => [
52
+ ...IsProgrammer.write_function_statements(project)(
53
+ importer,
54
+ )(collection),
55
+ ...importer.declare(modulo),
56
+ ],
57
+ })(importer);
58
+ };
59
+
60
+ const write_array_functions =
61
+ (config: FeatureProgrammer.IConfig) =>
62
+ (importer: FunctionImporter) =>
63
+ (collection: MetadataCollection): ts.VariableStatement[] =>
64
+ collection
65
+ .arrays()
66
+ .filter((a) => a.recursive)
67
+ .map((array, i) =>
68
+ StatementFactory.constant(
69
+ `${config.prefix}a${i}`,
70
+ ts.factory.createArrowFunction(
71
+ undefined,
72
+ undefined,
73
+ FeatureProgrammer.parameterDeclarations(config)(
74
+ TypeFactory.keyword("any"),
75
+ )(ts.factory.createIdentifier("input")),
76
+ TypeFactory.keyword("any"),
77
+ undefined,
78
+ decode_array_inline(config)(importer)(
79
+ ts.factory.createIdentifier("input"),
80
+ array,
81
+ {
82
+ tracable: config.trace,
83
+ source: "function",
84
+ from: "array",
85
+ postfix: "",
86
+ },
87
+ ),
88
+ ),
89
+ ),
90
+ );
91
+
92
+ const write_tuple_functions =
93
+ (project: IProject) =>
94
+ (config: FeatureProgrammer.IConfig) =>
95
+ (importer: FunctionImporter) =>
96
+ (collection: MetadataCollection): ts.VariableStatement[] =>
97
+ collection
98
+ .tuples()
99
+ .filter((t) => t.recursive)
100
+ .map((tuple, i) =>
101
+ StatementFactory.constant(
102
+ `${config.prefix}t${i}`,
103
+ ts.factory.createArrowFunction(
104
+ undefined,
105
+ undefined,
106
+ FeatureProgrammer.parameterDeclarations(config)(
107
+ TypeFactory.keyword("any"),
108
+ )(ts.factory.createIdentifier("input")),
109
+ TypeFactory.keyword("any"),
110
+ undefined,
111
+ decode_tuple_inline(project)(config)(importer)(
112
+ ts.factory.createIdentifier("input"),
113
+ tuple,
114
+ {
115
+ tracable: config.trace,
116
+ source: "function",
117
+ from: "array",
118
+ postfix: "",
119
+ },
120
+ ),
121
+ ),
122
+ ),
123
+ );
124
+
125
+ /* -----------------------------------------------------------
126
+ DECODERS
127
+ ----------------------------------------------------------- */
128
+ const decode =
129
+ (project: IProject) =>
130
+ (config: FeatureProgrammer.IConfig) =>
131
+ (importer: FunctionImporter) =>
132
+ (
133
+ input: ts.Expression,
134
+ meta: Metadata,
135
+ explore: FeatureProgrammer.IExplore,
136
+ ): ts.Expression => {
137
+ // ANY TYPE
138
+ if (meta.any === true)
139
+ return wrap_required(
140
+ input,
141
+ meta,
142
+ explore,
143
+ )(
144
+ wrap_functional(
145
+ input,
146
+ meta,
147
+ explore,
148
+ )(
149
+ ts.factory.createCallExpression(
150
+ ts.factory.createIdentifier("JSON.stringify"),
151
+ undefined,
152
+ [input],
153
+ ),
154
+ ),
155
+ );
156
+
157
+ // ONLY NULL OR UNDEFINED
158
+ const size: number = meta.size();
159
+ if (
160
+ size === 0 &&
161
+ (meta.isRequired() === false || meta.nullable === true)
162
+ ) {
163
+ if (meta.isRequired() === false && meta.nullable === true)
164
+ return explore.from === "array"
165
+ ? ts.factory.createStringLiteral("null")
166
+ : ts.factory.createConditionalExpression(
167
+ ts.factory.createStrictEquality(
168
+ ts.factory.createNull(),
169
+ input,
170
+ ),
171
+ undefined,
172
+ ts.factory.createStringLiteral("null"),
173
+ undefined,
174
+ ts.factory.createIdentifier("undefined"),
175
+ );
176
+ else if (meta.isRequired() === false)
177
+ return explore.from === "array"
178
+ ? ts.factory.createStringLiteral("null")
179
+ : ts.factory.createIdentifier("undefined");
180
+ else return ts.factory.createStringLiteral("null");
181
+ }
182
+
183
+ //----
184
+ // LIST UP UNION TYPES
185
+ //----
186
+ const unions: IUnion[] = [];
187
+
188
+ // toJSON() METHOD
189
+ if (meta.resolved !== null)
190
+ unions.push({
191
+ type: "resolved",
192
+ is: () => IsProgrammer.decode_to_json(false)(input),
193
+ value: () =>
194
+ decode_to_json(project)(config)(importer)(
195
+ input,
196
+ meta.resolved!.returns,
197
+ explore,
198
+ ),
199
+ });
200
+ else if (meta.functional === true)
201
+ unions.push({
202
+ type: "functional",
203
+ is: () => IsProgrammer.decode_functional(input),
204
+ value: () => decode_functional(explore),
205
+ });
206
+
207
+ // TEMPLATES
208
+ if (
209
+ meta.templates.length ||
210
+ ArrayUtil.has(meta.constants, (c) => c.type === "string")
211
+ )
212
+ if (AtomicPredicator.template(meta)) {
213
+ const partial = Metadata.initialize();
214
+ partial.atomics.push("string"),
215
+ unions.push({
216
+ type: "template literal",
217
+ is: () =>
218
+ IsProgrammer.decode(project)(importer)(
219
+ input,
220
+ partial,
221
+ explore,
222
+ [],
223
+ [],
224
+ ),
225
+ value: () =>
226
+ decode_atomic(project)(importer)(
227
+ input,
228
+ "string",
229
+ explore,
230
+ ),
231
+ });
232
+ }
233
+
234
+ // CONSTANTS
235
+ for (const constant of meta.constants)
236
+ if (AtomicPredicator.constant(meta)(constant.type) === false)
237
+ continue;
238
+ else if (constant.type !== "string")
239
+ unions.push({
240
+ type: "atomic",
241
+ is: () =>
242
+ IsProgrammer.decode(project)(importer)(
243
+ input,
244
+ (() => {
245
+ const partial = Metadata.initialize();
246
+ partial.atomics.push(constant.type);
247
+ return partial;
248
+ })(),
249
+ explore,
250
+ [],
251
+ [],
252
+ ),
253
+ value: () =>
254
+ decode_atomic(project)(importer)(
255
+ input,
256
+ constant.type,
257
+ explore,
258
+ ),
259
+ });
260
+ else if (meta.templates.length === 0)
261
+ unions.push({
262
+ type: "const string",
263
+ is: () =>
264
+ IsProgrammer.decode(project)(importer)(
265
+ input,
266
+ (() => {
267
+ const partial = Metadata.initialize();
268
+ partial.atomics.push("string");
269
+ return partial;
270
+ })(),
271
+ explore,
272
+ [],
273
+ [],
274
+ ),
275
+ value: () =>
276
+ decode_constant_string(project)(importer)(
277
+ input,
278
+ [...constant.values] as string[],
279
+ explore,
280
+ ),
281
+ });
282
+
283
+ /// ATOMICS
284
+ for (const type of meta.atomics)
285
+ if (AtomicPredicator.atomic(meta)(type))
286
+ unions.push({
287
+ type: "atomic",
288
+ is: () =>
289
+ IsProgrammer.decode(project)(importer)(
290
+ input,
291
+ (() => {
292
+ const partial = Metadata.initialize();
293
+ partial.atomics.push(type);
294
+ return partial;
295
+ })(),
296
+ explore,
297
+ [],
298
+ [],
299
+ ),
300
+ value: () =>
301
+ decode_atomic(project)(importer)(
302
+ input,
303
+ type,
304
+ explore,
305
+ ),
306
+ });
307
+
308
+ // TUPLES
309
+ for (const tuple of meta.tuples) {
310
+ for (const child of tuple.elements)
311
+ if (StringifyPredicator.undefindable(meta))
312
+ throw new Error(
313
+ `Error on typia.stringify(): tuple cannot contain undefined value - (${child.getName()}).`,
314
+ );
315
+ unions.push({
316
+ type: "tuple",
317
+ is: () =>
318
+ IsProgrammer.decode(project)(importer)(
319
+ input,
320
+ (() => {
321
+ const partial = Metadata.initialize();
322
+ partial.tuples.push(tuple);
323
+ return partial;
324
+ })(),
325
+ explore,
326
+ [],
327
+ [],
328
+ ),
329
+ value: () =>
330
+ decode_tuple(project)(config)(importer)(
331
+ input,
332
+ tuple,
333
+ explore,
334
+ ),
335
+ });
336
+ }
337
+
338
+ // ARRAYS
339
+ if (meta.arrays.length) {
340
+ for (const child of meta.arrays)
341
+ if (StringifyPredicator.undefindable(child.value))
342
+ throw new Error(
343
+ `Error on typia.stringify(): array cannot contain undefined value (${child.value.getName()}).`,
344
+ );
345
+ const value: () => ts.Expression =
346
+ meta.arrays.length === 1
347
+ ? () =>
348
+ decode_array(config)(importer)(
349
+ input,
350
+ meta.arrays[0]!,
351
+ {
352
+ ...explore,
353
+ from: "array",
354
+ },
355
+ )
356
+ : meta.arrays.some((elem) => elem.value.any)
357
+ ? () =>
358
+ ts.factory.createCallExpression(
359
+ ts.factory.createIdentifier("JSON.stringify"),
360
+ undefined,
361
+ [input],
362
+ )
363
+ : () =>
364
+ explore_arrays(project)(config)(importer)(
365
+ input,
366
+ meta.arrays,
367
+ {
368
+ ...explore,
369
+ from: "array",
370
+ },
371
+ );
372
+
373
+ unions.push({
374
+ type: "array",
375
+ is: () => ExpressionFactory.isArray(input),
376
+ value,
377
+ });
378
+ }
379
+
380
+ // BUILT-IN CLASSES
381
+ if (meta.natives.length)
382
+ for (const native of meta.natives)
383
+ unions.push({
384
+ type: "object",
385
+ is: () => check_native(native)(input),
386
+ value: () =>
387
+ AtomicPredicator.native(native)
388
+ ? decode_atomic(project)(importer)(
389
+ input,
390
+ native.toLowerCase() as Atomic.Literal,
391
+ explore,
392
+ )
393
+ : ts.factory.createStringLiteral("{}"),
394
+ });
395
+
396
+ // SETS
397
+ if (meta.sets.length)
398
+ unions.push({
399
+ type: "object",
400
+ is: () => ExpressionFactory.isInstanceOf("Set")(input),
401
+ value: () => ts.factory.createStringLiteral("{}"),
402
+ });
403
+
404
+ // MAPS
405
+ if (meta.maps.length)
406
+ unions.push({
407
+ type: "object",
408
+ is: () => ExpressionFactory.isInstanceOf("Map")(input),
409
+ value: () => ts.factory.createStringLiteral("{}"),
410
+ });
411
+
412
+ // OBJECTS
413
+ if (meta.objects.length)
414
+ unions.push({
415
+ type: "object",
416
+ is: () =>
417
+ ExpressionFactory.isObject({
418
+ checkNull: true,
419
+ checkArray: meta.objects.some((obj) =>
420
+ obj.properties.every(
421
+ (prop) =>
422
+ !prop.key.isSoleLiteral() ||
423
+ !prop.value.isRequired(),
424
+ ),
425
+ ),
426
+ })(input),
427
+ value: () =>
428
+ meta.isParentResolved() === false &&
429
+ meta.objects.length === 1 &&
430
+ meta.objects[0]!._Is_simple(
431
+ explore.from === "top" ? 0 : 1,
432
+ )
433
+ ? (() => {
434
+ const obj: MetadataObject = meta.objects[0]!;
435
+ const entries: IExpressionEntry<ts.Expression>[] =
436
+ feature_object_entries({
437
+ decoder: () =>
438
+ decode(project)(config)(importer),
439
+ trace: false,
440
+ path: false,
441
+ })(importer)(obj)(
442
+ ts.factory.createAsExpression(
443
+ input,
444
+ TypeFactory.keyword("any"),
445
+ ),
446
+ );
447
+ return StringifyJoiner.object(importer)(
448
+ ts.factory.createAsExpression(
449
+ input,
450
+ TypeFactory.keyword("any"),
451
+ ),
452
+ entries,
453
+ );
454
+ })()
455
+ : explore_objects(config)(importer)(input, meta, {
456
+ ...explore,
457
+ from: "object",
458
+ }),
459
+ });
460
+
461
+ //----
462
+ // RETURNS
463
+ //----
464
+ // CHECK NULL AND UNDEFINED
465
+ const wrapper = (output: ts.Expression) =>
466
+ wrap_required(
467
+ input,
468
+ meta,
469
+ explore,
470
+ )(wrap_nullable(input, meta)(output));
471
+
472
+ // DIRECT RETURN
473
+ if (unions.length === 0)
474
+ return ts.factory.createCallExpression(
475
+ ts.factory.createIdentifier("JSON.stringify"),
476
+ undefined,
477
+ [input],
478
+ );
479
+ else if (unions.length === 1) return wrapper(unions[0]!.value());
480
+
481
+ // RETURN WITH TYPE CHECKING
482
+ return wrapper(
483
+ ts.factory.createCallExpression(
484
+ ts.factory.createArrowFunction(
485
+ undefined,
486
+ undefined,
487
+ [],
488
+ undefined,
489
+ undefined,
490
+ iterate(importer, input, unions, meta.getName()),
491
+ ),
492
+ undefined,
493
+ undefined,
494
+ ),
495
+ );
496
+ };
497
+
498
+ const decode_object = (importer: FunctionImporter) =>
499
+ FeatureProgrammer.decode_object({
500
+ trace: false,
501
+ path: false,
502
+ prefix: PREFIX,
503
+ })(importer);
504
+
505
+ const decode_array =
506
+ (config: FeatureProgrammer.IConfig) =>
507
+ (importer: FunctionImporter) =>
508
+ (
509
+ input: ts.Expression,
510
+ array: MetadataArray,
511
+ explore: FeatureProgrammer.IExplore,
512
+ ) =>
513
+ array.recursive
514
+ ? ts.factory.createCallExpression(
515
+ ts.factory.createIdentifier(
516
+ importer.useLocal(`${config.prefix}a${array.index}`),
517
+ ),
518
+ undefined,
519
+ FeatureProgrammer.argumentsArray(config)({
520
+ ...explore,
521
+ source: "function",
522
+ from: "array",
523
+ })(input),
524
+ )
525
+ : decode_array_inline(config)(importer)(input, array, explore);
526
+
527
+ const decode_array_inline =
528
+ (config: FeatureProgrammer.IConfig) =>
529
+ (importer: FunctionImporter) =>
530
+ (
531
+ input: ts.Expression,
532
+ array: MetadataArray,
533
+ explore: FeatureProgrammer.IExplore,
534
+ ) =>
535
+ FeatureProgrammer.decode_array(config)(importer)(
536
+ StringifyJoiner.array,
537
+ )(input, array, explore, [], []);
538
+
539
+ const decode_tuple =
540
+ (project: IProject) =>
541
+ (config: FeatureProgrammer.IConfig) =>
542
+ (importer: FunctionImporter) =>
543
+ (
544
+ input: ts.Expression,
545
+ tuple: MetadataTuple,
546
+ explore: FeatureProgrammer.IExplore,
547
+ ): ts.Expression =>
548
+ tuple.recursive
549
+ ? ts.factory.createCallExpression(
550
+ ts.factory.createIdentifier(
551
+ importer.useLocal(`${config.prefix}t${tuple.index}`),
552
+ ),
553
+ undefined,
554
+ FeatureProgrammer.argumentsArray(config)({
555
+ ...explore,
556
+ source: "function",
557
+ })(input),
558
+ )
559
+ : decode_tuple_inline(project)(config)(importer)(
560
+ input,
561
+ tuple,
562
+ explore,
563
+ );
564
+
565
+ const decode_tuple_inline =
566
+ (project: IProject) =>
567
+ (config: FeatureProgrammer.IConfig) =>
568
+ (importer: FunctionImporter) =>
569
+ (
570
+ input: ts.Expression,
571
+ tuple: MetadataTuple,
572
+ explore: FeatureProgrammer.IExplore,
573
+ ): ts.Expression => {
574
+ const children: ts.Expression[] = tuple.elements
575
+ .filter((elem) => elem.rest === null)
576
+ .map((elem, index) =>
577
+ decode(project)(config)(importer)(
578
+ ts.factory.createElementAccessExpression(input, index),
579
+ elem,
580
+ {
581
+ ...explore,
582
+ from: "array",
583
+ postfix: explore.postfix.length
584
+ ? `${explore.postfix.slice(0, -1)}[${index}]"`
585
+ : `"[${index}]"`,
586
+ },
587
+ ),
588
+ );
589
+ const rest = (() => {
590
+ if (tuple.elements.length === 0) return null;
591
+ const last = tuple.elements.at(-1)!;
592
+ if (last.rest === null) return null;
593
+
594
+ const code = decode(project)(config)(importer)(
595
+ ts.factory.createCallExpression(
596
+ IdentifierFactory.access(input)("slice"),
597
+ undefined,
598
+ [
599
+ ts.factory.createNumericLiteral(
600
+ tuple.elements.length - 1,
601
+ ),
602
+ ],
603
+ ),
604
+ wrap_metadata_rest_tuple(tuple.elements.at(-1)!.rest!),
605
+ {
606
+ ...explore,
607
+ start: tuple.elements.length - 1,
608
+ },
609
+ );
610
+ return ts.factory.createCallExpression(
611
+ importer.use("rest"),
612
+ undefined,
613
+ [code],
614
+ );
615
+ })();
616
+ return StringifyJoiner.tuple(children, rest);
617
+ };
618
+
619
+ const decode_atomic =
620
+ (project: IProject) =>
621
+ (importer: FunctionImporter) =>
622
+ (
623
+ input: ts.Expression,
624
+ type: string,
625
+ explore: FeatureProgrammer.IExplore,
626
+ ) => {
627
+ if (type === "string")
628
+ return ts.factory.createCallExpression(
629
+ importer.use("string"),
630
+ undefined,
631
+ [input],
632
+ );
633
+ else if (
634
+ type === "number" &&
635
+ OptionPredicator.numeric(project.options)
636
+ )
637
+ input = ts.factory.createCallExpression(
638
+ importer.use("number"),
639
+ undefined,
640
+ [input],
641
+ );
642
+
643
+ return explore.from !== "top"
644
+ ? input
645
+ : ts.factory.createCallExpression(
646
+ IdentifierFactory.access(input)("toString"),
647
+ undefined,
648
+ undefined,
649
+ );
650
+ };
651
+
652
+ const decode_constant_string =
653
+ (project: IProject) =>
654
+ (importer: FunctionImporter) =>
655
+ (
656
+ input: ts.Expression,
657
+ values: string[],
658
+ explore: FeatureProgrammer.IExplore,
659
+ ): ts.Expression => {
660
+ if (values.every((v) => !StringifyPredicator.require_escape(v)))
661
+ return [
662
+ ts.factory.createStringLiteral('"'),
663
+ input,
664
+ ts.factory.createStringLiteral('"'),
665
+ ].reduce((x, y) => ts.factory.createAdd(x, y));
666
+ else
667
+ return decode_atomic(project)(importer)(
668
+ input,
669
+ "string",
670
+ explore,
671
+ );
672
+ };
673
+
674
+ const decode_to_json =
675
+ (project: IProject) =>
676
+ (config: FeatureProgrammer.IConfig) =>
677
+ (importer: FunctionImporter) =>
678
+ (
679
+ input: ts.Expression,
680
+ resolved: Metadata,
681
+ explore: FeatureProgrammer.IExplore,
682
+ ): ts.Expression => {
683
+ return decode(project)(config)(importer)(
684
+ ts.factory.createCallExpression(
685
+ IdentifierFactory.access(input)("toJSON"),
686
+ undefined,
687
+ [],
688
+ ),
689
+ resolved,
690
+ explore,
691
+ );
692
+ };
693
+
694
+ const decode_functional = (explore: FeatureProgrammer.IExplore) =>
695
+ explore.from === "array"
696
+ ? ts.factory.createStringLiteral("null")
697
+ : ts.factory.createIdentifier("undefined");
698
+
699
+ /* -----------------------------------------------------------
700
+ EXPLORERS
701
+ ----------------------------------------------------------- */
702
+ const explore_objects =
703
+ (config: FeatureProgrammer.IConfig) =>
704
+ (importer: FunctionImporter) =>
705
+ (
706
+ input: ts.Expression,
707
+ meta: Metadata,
708
+ explore: FeatureProgrammer.IExplore,
709
+ ) =>
710
+ meta.objects.length === 1
711
+ ? decode_object(importer)(input, meta.objects[0]!, explore)
712
+ : ts.factory.createCallExpression(
713
+ ts.factory.createIdentifier(
714
+ importer.useLocal(`${PREFIX}u${meta.union_index!}`),
715
+ ),
716
+ undefined,
717
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
718
+ );
719
+
720
+ const explore_arrays =
721
+ (project: IProject) =>
722
+ (config: FeatureProgrammer.IConfig) =>
723
+ (importer: FunctionImporter) =>
724
+ (
725
+ input: ts.Expression,
726
+ elements: MetadataArray[],
727
+ explore: FeatureProgrammer.IExplore,
728
+ ): ts.Expression =>
729
+ explore_array_like_union_types(config)(importer)(
730
+ UnionExplorer.array({
731
+ checker: IsProgrammer.decode(project)(importer),
732
+ decoder: decode_array(config)(importer),
733
+ empty: ts.factory.createStringLiteral("[]"),
734
+ success: ts.factory.createTrue(),
735
+ failure: (input, expected) =>
736
+ create_throw_error(importer)(expected)(input),
737
+ }),
738
+ )(input, elements, explore);
739
+
740
+ const explore_array_like_union_types =
741
+ (config: FeatureProgrammer.IConfig) =>
742
+ (importer: FunctionImporter) =>
743
+ <T extends MetadataArray | MetadataTuple>(
744
+ factory: (
745
+ parameters: ts.ParameterDeclaration[],
746
+ ) => (
747
+ input: ts.Expression,
748
+ elements: T[],
749
+ explore: FeatureProgrammer.IExplore,
750
+ tags: IMetadataTag[],
751
+ jsDocTags: IJsDocTagInfo[],
752
+ ) => ts.ArrowFunction,
753
+ ) =>
754
+ (
755
+ input: ts.Expression,
756
+ elements: T[],
757
+ explore: FeatureProgrammer.IExplore,
758
+ ): ts.Expression => {
759
+ const arrow =
760
+ (parameters: ts.ParameterDeclaration[]) =>
761
+ (explore: FeatureProgrammer.IExplore) =>
762
+ (input: ts.Expression): ts.ArrowFunction =>
763
+ factory(parameters)(input, elements, explore, [], []);
764
+ if (elements.every((e) => e.recursive === false))
765
+ ts.factory.createCallExpression(
766
+ arrow([])(explore)(input),
767
+ undefined,
768
+ [],
769
+ );
770
+
771
+ explore = {
772
+ ...explore,
773
+ source: "function",
774
+ from: "array",
775
+ };
776
+ return ts.factory.createCallExpression(
777
+ ts.factory.createIdentifier(
778
+ importer.emplaceUnion(
779
+ config.prefix,
780
+ elements.map((e) => e.name).join(" | "),
781
+ () =>
782
+ arrow(
783
+ FeatureProgrammer.parameterDeclarations(config)(
784
+ TypeFactory.keyword("any"),
785
+ )(ts.factory.createIdentifier("input")),
786
+ )({
787
+ ...explore,
788
+ postfix: "",
789
+ })(ts.factory.createIdentifier("input")),
790
+ ),
791
+ ),
792
+ undefined,
793
+ FeatureProgrammer.argumentsArray(config)(explore)(input),
794
+ );
795
+ };
796
+
797
+ /* -----------------------------------------------------------
798
+ RETURN SCRIPTS
799
+ ----------------------------------------------------------- */
800
+ const wrap_required = (
801
+ input: ts.Expression,
802
+ meta: Metadata,
803
+ explore: FeatureProgrammer.IExplore,
804
+ ): ((expression: ts.Expression) => ts.Expression) => {
805
+ if (meta.isRequired() === true && meta.any === false)
806
+ return (expression) => expression;
807
+ return (expression) =>
808
+ ts.factory.createConditionalExpression(
809
+ ts.factory.createStrictInequality(
810
+ ts.factory.createIdentifier("undefined"),
811
+ input,
812
+ ),
813
+ undefined,
814
+ expression,
815
+ undefined,
816
+ explore.from === "array"
817
+ ? ts.factory.createStringLiteral("null")
818
+ : ts.factory.createIdentifier("undefined"),
819
+ );
820
+ };
821
+
822
+ const wrap_nullable = (
823
+ input: ts.Expression,
824
+ meta: Metadata,
825
+ ): ((expression: ts.Expression) => ts.Expression) => {
826
+ if (meta.nullable === false) return (expression) => expression;
827
+ return (expression) =>
828
+ ts.factory.createConditionalExpression(
829
+ ts.factory.createStrictInequality(
830
+ ts.factory.createNull(),
831
+ input,
832
+ ),
833
+ undefined,
834
+ expression,
835
+ undefined,
836
+ ts.factory.createStringLiteral("null"),
837
+ );
838
+ };
839
+
840
+ const wrap_functional = (
841
+ input: ts.Expression,
842
+ meta: Metadata,
843
+ explore: FeatureProgrammer.IExplore,
844
+ ): ((expression: ts.Expression) => ts.Expression) => {
845
+ if (meta.functional === false) return (expression) => expression;
846
+ return (expression) =>
847
+ ts.factory.createConditionalExpression(
848
+ ts.factory.createStrictInequality(
849
+ ts.factory.createStringLiteral("function"),
850
+ ValueFactory.TYPEOF(input),
851
+ ),
852
+ undefined,
853
+ expression,
854
+ undefined,
855
+ decode_functional(explore),
856
+ );
857
+ };
858
+
859
+ const iterate = (
860
+ importer: FunctionImporter,
861
+ input: ts.Expression,
862
+ unions: IUnion[],
863
+ expected: string,
864
+ ) =>
865
+ ts.factory.createBlock(
866
+ [
867
+ ...unions.map((u) =>
868
+ ts.factory.createIfStatement(
869
+ u.is(),
870
+ ts.factory.createReturnStatement(u.value()),
871
+ ),
872
+ ),
873
+ create_throw_error(importer)(expected)(input),
874
+ ],
875
+ true,
876
+ );
877
+
878
+ /* -----------------------------------------------------------
879
+ CONFIGURATIONS
880
+ ----------------------------------------------------------- */
881
+ const PREFIX = "$s";
882
+
883
+ const configure =
884
+ (project: IProject) =>
885
+ (importer: FunctionImporter): FeatureProgrammer.IConfig => {
886
+ const config: FeatureProgrammer.IConfig = {
887
+ types: {
888
+ input: (type, name) =>
889
+ ts.factory.createTypeReferenceNode(
890
+ name ??
891
+ TypeFactory.getFullName(project.checker)(type),
892
+ ),
893
+ output: () => TypeFactory.keyword("string"),
894
+ },
895
+ prefix: PREFIX,
896
+ trace: false,
897
+ path: false,
898
+ initializer,
899
+ decoder: () => decode(project)(config)(importer),
900
+ objector: {
901
+ checker: () => (input, meta, explore) =>
902
+ IsProgrammer.decode(project)(importer)(
903
+ input,
904
+ meta,
905
+ explore,
906
+ [],
907
+ [],
908
+ ),
909
+ decoder: () => decode_object(importer),
910
+ joiner: StringifyJoiner.object(importer),
911
+ unionizer: decode_union_object(
912
+ IsProgrammer.decode_object(importer),
913
+ )(decode_object(importer))((exp) => exp)(
914
+ (value, expected) =>
915
+ create_throw_error(importer)(expected)(value),
916
+ ),
917
+ failure: (input, expected) =>
918
+ create_throw_error(importer)(expected)(input),
919
+ },
920
+ generator: {
921
+ arrays: () => write_array_functions(config)(importer),
922
+ tuples: () =>
923
+ write_tuple_functions(project)(config)(importer),
924
+ },
925
+ };
926
+ return config;
927
+ };
928
+
929
+ const initializer: FeatureProgrammer.IConfig["initializer"] =
930
+ ({ checker }) =>
931
+ (type) => {
932
+ const collection: MetadataCollection = new MetadataCollection();
933
+ const meta: Metadata = MetadataFactory.analyze(checker)({
934
+ resolve: true,
935
+ constant: true,
936
+ absorb: true,
937
+ validate: (meta) => {
938
+ if (meta.atomics.find((str) => str === "bigint"))
939
+ throw new Error(NO_BIGINT);
940
+ else if (
941
+ meta.arrays.some(
942
+ (array) => array.value.isRequired() === false,
943
+ )
944
+ )
945
+ throw new Error(NO_UNDEFINED_IN_ARRAY);
946
+ },
947
+ })(collection)(type);
948
+ return [collection, meta];
949
+ };
950
+
951
+ const create_throw_error =
952
+ (importer: FunctionImporter) =>
953
+ (expected: string) =>
954
+ (value: ts.Expression) =>
955
+ ts.factory.createExpressionStatement(
956
+ ts.factory.createCallExpression(
957
+ importer.use("throws"),
958
+ [],
959
+ [
960
+ ts.factory.createObjectLiteralExpression(
961
+ [
962
+ ts.factory.createPropertyAssignment(
963
+ "expected",
964
+ ts.factory.createStringLiteral(expected),
965
+ ),
966
+ ts.factory.createPropertyAssignment(
967
+ "value",
968
+ value,
969
+ ),
970
+ ],
971
+ true,
972
+ ),
973
+ ],
974
+ ),
975
+ );
976
+ }
977
+
978
+ interface IUnion {
979
+ type: string;
980
+ is: () => ts.Expression;
981
+ value: () => ts.Expression;
982
+ }
983
+
984
+ const NO_BIGINT = "Error on typia.stringify(): does not allow bigint type.";
985
+ const NO_UNDEFINED_IN_ARRAY =
986
+ "Error on typia.stringify(): does not allow undefined type in array.";