typia 5.0.0-dev.20230823 → 5.0.0-dev.2023084

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