typia 4.0.0 → 4.0.1-dev.20230604

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 (87) hide show
  1. package/lib/programmers/ApplicationProgrammer.js +1 -1
  2. package/lib/programmers/internal/application_array.js +19 -41
  3. package/lib/programmers/internal/application_array.js.map +1 -1
  4. package/lib/programmers/internal/application_definition.d.ts +1 -1
  5. package/lib/programmers/internal/application_definition.js +8 -8
  6. package/lib/programmers/internal/application_definition.js.map +1 -1
  7. package/lib/programmers/internal/application_native.js +3 -3
  8. package/lib/programmers/internal/application_object.js +4 -4
  9. package/lib/programmers/internal/application_schema.js +3 -3
  10. package/lib/programmers/internal/application_schema.js.map +1 -1
  11. package/lib/programmers/internal/application_tuple.js +8 -30
  12. package/lib/programmers/internal/application_tuple.js.map +1 -1
  13. package/lib/schemas/IJsonComponents.d.ts +1 -12
  14. package/lib/schemas/IJsonSchema.d.ts +1 -1
  15. package/package.json +1 -1
  16. package/src/IRandomGenerator.ts +34 -34
  17. package/src/factories/IdentifierFactory.ts +65 -65
  18. package/src/factories/MetadataCollection.ts +254 -254
  19. package/src/factories/MetadataFactory.ts +40 -40
  20. package/src/factories/MetadataTagFactory.ts +300 -300
  21. package/src/factories/internal/metadata/emplace_metadata_array.ts +34 -34
  22. package/src/factories/internal/metadata/emplace_metadata_definition.ts +35 -35
  23. package/src/factories/internal/metadata/emplace_metadata_object.ts +142 -142
  24. package/src/factories/internal/metadata/emplace_metadata_tuple.ts +50 -50
  25. package/src/factories/internal/metadata/explore_metadata.ts +66 -66
  26. package/src/factories/internal/metadata/iterate_metadata.ts +77 -77
  27. package/src/factories/internal/metadata/iterate_metadata_array.ts +25 -25
  28. package/src/factories/internal/metadata/iterate_metadata_collection.ts +130 -130
  29. package/src/factories/internal/metadata/iterate_metadata_definition.ts +30 -30
  30. package/src/factories/internal/metadata/iterate_metadata_object.ts +49 -49
  31. package/src/factories/internal/metadata/iterate_metadata_sort.ts +68 -68
  32. package/src/factories/internal/metadata/iterate_metadata_tuple.ts +24 -24
  33. package/src/factories/internal/metadata/iterate_metadata_union.ts +68 -68
  34. package/src/metadata/IMetadata.ts +27 -27
  35. package/src/metadata/IMetadataAlias.ts +12 -12
  36. package/src/metadata/IMetadataApplication.ts +7 -7
  37. package/src/metadata/IMetadataArray.ts +10 -10
  38. package/src/metadata/IMetadataCollection.ts +11 -11
  39. package/src/metadata/IMetadataDictionary.ts +14 -14
  40. package/src/metadata/IMetadataObject.ts +18 -18
  41. package/src/metadata/IMetadataProperty.ts +11 -11
  42. package/src/metadata/IMetadataTuple.ts +10 -10
  43. package/src/metadata/Metadata.ts +585 -585
  44. package/src/metadata/MetadataAlias.ts +61 -61
  45. package/src/metadata/MetadataArray.ts +52 -52
  46. package/src/metadata/MetadataObject.ts +114 -114
  47. package/src/metadata/MetadataProperty.ts +64 -64
  48. package/src/metadata/MetadataTuple.ts +53 -53
  49. package/src/programmers/ApplicationProgrammer.ts +55 -55
  50. package/src/programmers/AssertProgrammer.ts +291 -291
  51. package/src/programmers/CheckerProgrammer.ts +1182 -1182
  52. package/src/programmers/CloneProgrammer.ts +595 -595
  53. package/src/programmers/FeatureProgrammer.ts +495 -495
  54. package/src/programmers/IsProgrammer.ts +250 -250
  55. package/src/programmers/LiteralsProgrammer.ts +66 -66
  56. package/src/programmers/PruneProgrammer.ts +550 -550
  57. package/src/programmers/RandomProgrammer.ts +589 -589
  58. package/src/programmers/StringifyProgrammer.ts +990 -990
  59. package/src/programmers/ValidateProgrammer.ts +313 -313
  60. package/src/programmers/helpers/FunctionImporeter.ts +78 -78
  61. package/src/programmers/helpers/RandomJoiner.ts +173 -173
  62. package/src/programmers/helpers/UnionExplorer.ts +301 -301
  63. package/src/programmers/helpers/UnionPredicator.ts +81 -81
  64. package/src/programmers/helpers/disable_function_importer_declare.ts +26 -26
  65. package/src/programmers/internal/JSON_SCHEMA_PREFIX.ts +1 -1
  66. package/src/programmers/internal/application_array.ts +30 -64
  67. package/src/programmers/internal/application_boolean.ts +15 -15
  68. package/src/programmers/internal/application_constant.ts +26 -26
  69. package/src/programmers/internal/application_default.ts +17 -17
  70. package/src/programmers/internal/application_definition.ts +45 -45
  71. package/src/programmers/internal/application_native.ts +39 -39
  72. package/src/programmers/internal/application_number.ts +74 -74
  73. package/src/programmers/internal/application_object.ts +167 -167
  74. package/src/programmers/internal/application_schema.ts +156 -164
  75. package/src/programmers/internal/application_tuple.ts +47 -77
  76. package/src/programmers/internal/check_union_array_like.ts +329 -329
  77. package/src/programmers/internal/feature_object_entries.ts +63 -63
  78. package/src/programmers/internal/wrap_metadata_rest_tuple.ts +16 -16
  79. package/src/schemas/IJsonApplication.ts +8 -8
  80. package/src/schemas/IJsonComponents.ts +34 -45
  81. package/src/schemas/IJsonSchema.ts +134 -134
  82. package/src/transformers/CallExpressionTransformer.ts +179 -179
  83. package/src/transformers/FileTransformer.ts +47 -47
  84. package/src/transformers/features/miscellaneous/ApplicationTransformer.ts +104 -104
  85. package/src/transformers/features/miscellaneous/MetadataTransformer.ts +53 -53
  86. package/src/utils/ArrayUtil.ts +45 -45
  87. package/src/utils/RandomGenerator.ts +81 -81
@@ -1,250 +1,250 @@
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 { TypeFactory } from "../factories/TypeFactory";
7
- import { ValueFactory } from "../factories/ValueFactory";
8
-
9
- import { MetadataObject } from "../metadata/MetadataObject";
10
-
11
- import { IProject } from "../transformers/IProject";
12
-
13
- import { CheckerProgrammer } from "./CheckerProgrammer";
14
- import { FunctionImporter } from "./helpers/FunctionImporeter";
15
- import { IExpressionEntry } from "./helpers/IExpressionEntry";
16
- import { OptionPredicator } from "./helpers/OptionPredicator";
17
- import { disable_function_importer_declare } from "./helpers/disable_function_importer_declare";
18
- import { check_object } from "./internal/check_object";
19
- import { feature_object_entries } from "./internal/feature_object_entries";
20
-
21
- export namespace IsProgrammer {
22
- export const configure =
23
- (options?: Partial<CONFIG.IOptions>) =>
24
- (importer: FunctionImporter): CheckerProgrammer.IConfig => ({
25
- prefix: "$i",
26
- trace: false,
27
- path: false,
28
- equals: !!options?.object,
29
- numeric: OptionPredicator.numeric({
30
- numeric: options?.numeric,
31
- }),
32
- atomist: () => (entry) => () =>
33
- [
34
- entry.expression,
35
- ...entry.tags.map((tag) => tag.expression),
36
- ].reduce((x, y) => ts.factory.createLogicalAnd(x, y)),
37
- combiner: () => (type: "and" | "or") => {
38
- const initial: ts.TrueLiteral | ts.FalseLiteral =
39
- type === "and"
40
- ? ts.factory.createTrue()
41
- : ts.factory.createFalse();
42
- const binder =
43
- type === "and"
44
- ? ts.factory.createLogicalAnd
45
- : ts.factory.createLogicalOr;
46
- return (
47
- _input: ts.Expression,
48
- binaries: CheckerProgrammer.IBinary[],
49
- ) =>
50
- binaries.length
51
- ? binaries
52
- .map((binary) => binary.expression)
53
- .reduce((x, y) => binder(x, y))
54
- : initial;
55
- },
56
- joiner: {
57
- object:
58
- options?.object ||
59
- check_object({
60
- equals: !!options?.object,
61
- undefined: OptionPredicator.undefined({
62
- undefined: options?.undefined,
63
- }),
64
- assert: true,
65
- reduce: ts.factory.createLogicalAnd,
66
- positive: ts.factory.createTrue(),
67
- superfluous: () => ts.factory.createFalse(),
68
- })(importer),
69
- array: (input, arrow) =>
70
- ts.factory.createCallExpression(
71
- IdentifierFactory.access(input)("every"),
72
- undefined,
73
- [arrow],
74
- ),
75
- failure: () => ts.factory.createFalse(),
76
- },
77
- success: ts.factory.createTrue(),
78
- });
79
-
80
- export namespace CONFIG {
81
- export interface IOptions {
82
- numeric: boolean;
83
- undefined: boolean;
84
- object: (
85
- input: ts.Expression,
86
- entries: IExpressionEntry<ts.Expression>[],
87
- ) => ts.Expression;
88
- }
89
- }
90
-
91
- /* -----------------------------------------------------------
92
- WRITERS
93
- ----------------------------------------------------------- */
94
- /**
95
- * @deprecated Use `write()` function instead
96
- */
97
- export const generate =
98
- (
99
- project: IProject,
100
- modulo: ts.LeftHandSideExpression,
101
- equals: boolean = false,
102
- ) =>
103
- (type: ts.Type, name?: string) =>
104
- write(project)(modulo)(equals)(type, name);
105
-
106
- export const write =
107
- (project: IProject) =>
108
- (modulo: ts.LeftHandSideExpression, disable?: boolean) =>
109
- (equals: boolean) => {
110
- const importer: FunctionImporter =
111
- disable === <any>{}
112
- ? disable_function_importer_declare(new FunctionImporter())
113
- : new FunctionImporter();
114
-
115
- // CONFIGURATION
116
- const config: CheckerProgrammer.IConfig = {
117
- ...configure({
118
- object: check_object({
119
- equals,
120
- undefined: OptionPredicator.undefined(project.options),
121
- assert: true,
122
- reduce: ts.factory.createLogicalAnd,
123
- positive: ts.factory.createTrue(),
124
- superfluous: () => ts.factory.createFalse(),
125
- })(importer),
126
- numeric: OptionPredicator.numeric(project.options),
127
- })(importer),
128
- trace: equals,
129
- addition: () => importer.declare(modulo),
130
- };
131
-
132
- config.decoder =
133
- () => (input, target, explore, tags, jsDocTags) => {
134
- if (
135
- target.size() === 1 &&
136
- target.objects.length === 1 &&
137
- target.required === true &&
138
- target.nullable === false
139
- ) {
140
- // ONLY WHEN OBJECT WITH SOME ATOMIC PROPERTIES
141
- const obj: MetadataObject = target.objects[0]!;
142
- if (
143
- obj._Is_simple() &&
144
- (equals === false ||
145
- OptionPredicator.undefined(project.options) ===
146
- false)
147
- )
148
- return ts.factory.createLogicalAnd(
149
- ExpressionFactory.isObject({
150
- checkNull: true,
151
- checkArray: false,
152
- })(input),
153
- config.joiner.object(
154
- ts.factory.createAsExpression(
155
- input,
156
- TypeFactory.keyword("any"),
157
- ),
158
- feature_object_entries(config as any)(
159
- importer,
160
- )(obj)(
161
- ts.factory.createAsExpression(
162
- input,
163
- TypeFactory.keyword("any"),
164
- ),
165
- ),
166
- ),
167
- );
168
- }
169
- return CheckerProgrammer.decode(project)(config)(importer)(
170
- input,
171
- target,
172
- explore,
173
- tags,
174
- jsDocTags,
175
- );
176
- };
177
-
178
- // GENERATE CHECKER
179
- return CheckerProgrammer.write(project)(config)(importer);
180
- };
181
-
182
- export const write_function_statements =
183
- (project: IProject) =>
184
- (importer: FunctionImporter) =>
185
- (collection: MetadataCollection) => {
186
- const config = configure()(importer);
187
- const objects =
188
- CheckerProgrammer.write_object_functions(project)(config)(
189
- importer,
190
- )(collection);
191
- const unions =
192
- CheckerProgrammer.write_union_functions(project)(config)(
193
- importer,
194
- )(collection);
195
- const arrays =
196
- CheckerProgrammer.write_array_functions(project)(config)(
197
- importer,
198
- )(collection);
199
- const tuples =
200
- CheckerProgrammer.write_tuple_functions(project)(config)(
201
- importer,
202
- )(collection);
203
-
204
- return [
205
- ...objects.filter((_, i) =>
206
- importer.hasLocal(`${config.prefix}o${i}`),
207
- ),
208
- ...unions.filter((_, i) =>
209
- importer.hasLocal(`${config.prefix}u${i}`),
210
- ),
211
- ...arrays.filter((_, i) =>
212
- importer.hasLocal(`${config.prefix}a${i}`),
213
- ),
214
- ...tuples.filter((_, i) =>
215
- importer.hasLocal(`${config.prefix}t${i}`),
216
- ),
217
- ];
218
- };
219
-
220
- /* -----------------------------------------------------------
221
- DECODERS
222
- ----------------------------------------------------------- */
223
- export const decode = (project: IProject) => (importer: FunctionImporter) =>
224
- CheckerProgrammer.decode(project)(configure()(importer))(importer);
225
-
226
- export const decode_object = (importer: FunctionImporter) =>
227
- CheckerProgrammer.decode_object(configure()(importer))(importer);
228
-
229
- export const decode_to_json =
230
- (checkNull: boolean) =>
231
- (input: ts.Expression): ts.Expression =>
232
- ts.factory.createLogicalAnd(
233
- ExpressionFactory.isObject({
234
- checkArray: false,
235
- checkNull,
236
- })(input),
237
- ts.factory.createStrictEquality(
238
- ts.factory.createStringLiteral("function"),
239
- ValueFactory.TYPEOF(
240
- IdentifierFactory.access(input)("toJSON"),
241
- ),
242
- ),
243
- );
244
-
245
- export const decode_functional = (input: ts.Expression) =>
246
- ts.factory.createStrictEquality(
247
- ts.factory.createStringLiteral("function"),
248
- ValueFactory.TYPEOF(input),
249
- );
250
- }
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 { TypeFactory } from "../factories/TypeFactory";
7
+ import { ValueFactory } from "../factories/ValueFactory";
8
+
9
+ import { MetadataObject } from "../metadata/MetadataObject";
10
+
11
+ import { IProject } from "../transformers/IProject";
12
+
13
+ import { CheckerProgrammer } from "./CheckerProgrammer";
14
+ import { FunctionImporter } from "./helpers/FunctionImporeter";
15
+ import { IExpressionEntry } from "./helpers/IExpressionEntry";
16
+ import { OptionPredicator } from "./helpers/OptionPredicator";
17
+ import { disable_function_importer_declare } from "./helpers/disable_function_importer_declare";
18
+ import { check_object } from "./internal/check_object";
19
+ import { feature_object_entries } from "./internal/feature_object_entries";
20
+
21
+ export namespace IsProgrammer {
22
+ export const configure =
23
+ (options?: Partial<CONFIG.IOptions>) =>
24
+ (importer: FunctionImporter): CheckerProgrammer.IConfig => ({
25
+ prefix: "$i",
26
+ trace: false,
27
+ path: false,
28
+ equals: !!options?.object,
29
+ numeric: OptionPredicator.numeric({
30
+ numeric: options?.numeric,
31
+ }),
32
+ atomist: () => (entry) => () =>
33
+ [
34
+ entry.expression,
35
+ ...entry.tags.map((tag) => tag.expression),
36
+ ].reduce((x, y) => ts.factory.createLogicalAnd(x, y)),
37
+ combiner: () => (type: "and" | "or") => {
38
+ const initial: ts.TrueLiteral | ts.FalseLiteral =
39
+ type === "and"
40
+ ? ts.factory.createTrue()
41
+ : ts.factory.createFalse();
42
+ const binder =
43
+ type === "and"
44
+ ? ts.factory.createLogicalAnd
45
+ : ts.factory.createLogicalOr;
46
+ return (
47
+ _input: ts.Expression,
48
+ binaries: CheckerProgrammer.IBinary[],
49
+ ) =>
50
+ binaries.length
51
+ ? binaries
52
+ .map((binary) => binary.expression)
53
+ .reduce((x, y) => binder(x, y))
54
+ : initial;
55
+ },
56
+ joiner: {
57
+ object:
58
+ options?.object ||
59
+ check_object({
60
+ equals: !!options?.object,
61
+ undefined: OptionPredicator.undefined({
62
+ undefined: options?.undefined,
63
+ }),
64
+ assert: true,
65
+ reduce: ts.factory.createLogicalAnd,
66
+ positive: ts.factory.createTrue(),
67
+ superfluous: () => ts.factory.createFalse(),
68
+ })(importer),
69
+ array: (input, arrow) =>
70
+ ts.factory.createCallExpression(
71
+ IdentifierFactory.access(input)("every"),
72
+ undefined,
73
+ [arrow],
74
+ ),
75
+ failure: () => ts.factory.createFalse(),
76
+ },
77
+ success: ts.factory.createTrue(),
78
+ });
79
+
80
+ export namespace CONFIG {
81
+ export interface IOptions {
82
+ numeric: boolean;
83
+ undefined: boolean;
84
+ object: (
85
+ input: ts.Expression,
86
+ entries: IExpressionEntry<ts.Expression>[],
87
+ ) => ts.Expression;
88
+ }
89
+ }
90
+
91
+ /* -----------------------------------------------------------
92
+ WRITERS
93
+ ----------------------------------------------------------- */
94
+ /**
95
+ * @deprecated Use `write()` function instead
96
+ */
97
+ export const generate =
98
+ (
99
+ project: IProject,
100
+ modulo: ts.LeftHandSideExpression,
101
+ equals: boolean = false,
102
+ ) =>
103
+ (type: ts.Type, name?: string) =>
104
+ write(project)(modulo)(equals)(type, name);
105
+
106
+ export const write =
107
+ (project: IProject) =>
108
+ (modulo: ts.LeftHandSideExpression, disable?: boolean) =>
109
+ (equals: boolean) => {
110
+ const importer: FunctionImporter =
111
+ disable === <any>{}
112
+ ? disable_function_importer_declare(new FunctionImporter())
113
+ : new FunctionImporter();
114
+
115
+ // CONFIGURATION
116
+ const config: CheckerProgrammer.IConfig = {
117
+ ...configure({
118
+ object: check_object({
119
+ equals,
120
+ undefined: OptionPredicator.undefined(project.options),
121
+ assert: true,
122
+ reduce: ts.factory.createLogicalAnd,
123
+ positive: ts.factory.createTrue(),
124
+ superfluous: () => ts.factory.createFalse(),
125
+ })(importer),
126
+ numeric: OptionPredicator.numeric(project.options),
127
+ })(importer),
128
+ trace: equals,
129
+ addition: () => importer.declare(modulo),
130
+ };
131
+
132
+ config.decoder =
133
+ () => (input, target, explore, tags, jsDocTags) => {
134
+ if (
135
+ target.size() === 1 &&
136
+ target.objects.length === 1 &&
137
+ target.required === true &&
138
+ target.nullable === false
139
+ ) {
140
+ // ONLY WHEN OBJECT WITH SOME ATOMIC PROPERTIES
141
+ const obj: MetadataObject = target.objects[0]!;
142
+ if (
143
+ obj._Is_simple() &&
144
+ (equals === false ||
145
+ OptionPredicator.undefined(project.options) ===
146
+ false)
147
+ )
148
+ return ts.factory.createLogicalAnd(
149
+ ExpressionFactory.isObject({
150
+ checkNull: true,
151
+ checkArray: false,
152
+ })(input),
153
+ config.joiner.object(
154
+ ts.factory.createAsExpression(
155
+ input,
156
+ TypeFactory.keyword("any"),
157
+ ),
158
+ feature_object_entries(config as any)(
159
+ importer,
160
+ )(obj)(
161
+ ts.factory.createAsExpression(
162
+ input,
163
+ TypeFactory.keyword("any"),
164
+ ),
165
+ ),
166
+ ),
167
+ );
168
+ }
169
+ return CheckerProgrammer.decode(project)(config)(importer)(
170
+ input,
171
+ target,
172
+ explore,
173
+ tags,
174
+ jsDocTags,
175
+ );
176
+ };
177
+
178
+ // GENERATE CHECKER
179
+ return CheckerProgrammer.write(project)(config)(importer);
180
+ };
181
+
182
+ export const write_function_statements =
183
+ (project: IProject) =>
184
+ (importer: FunctionImporter) =>
185
+ (collection: MetadataCollection) => {
186
+ const config = configure()(importer);
187
+ const objects =
188
+ CheckerProgrammer.write_object_functions(project)(config)(
189
+ importer,
190
+ )(collection);
191
+ const unions =
192
+ CheckerProgrammer.write_union_functions(project)(config)(
193
+ importer,
194
+ )(collection);
195
+ const arrays =
196
+ CheckerProgrammer.write_array_functions(project)(config)(
197
+ importer,
198
+ )(collection);
199
+ const tuples =
200
+ CheckerProgrammer.write_tuple_functions(project)(config)(
201
+ importer,
202
+ )(collection);
203
+
204
+ return [
205
+ ...objects.filter((_, i) =>
206
+ importer.hasLocal(`${config.prefix}o${i}`),
207
+ ),
208
+ ...unions.filter((_, i) =>
209
+ importer.hasLocal(`${config.prefix}u${i}`),
210
+ ),
211
+ ...arrays.filter((_, i) =>
212
+ importer.hasLocal(`${config.prefix}a${i}`),
213
+ ),
214
+ ...tuples.filter((_, i) =>
215
+ importer.hasLocal(`${config.prefix}t${i}`),
216
+ ),
217
+ ];
218
+ };
219
+
220
+ /* -----------------------------------------------------------
221
+ DECODERS
222
+ ----------------------------------------------------------- */
223
+ export const decode = (project: IProject) => (importer: FunctionImporter) =>
224
+ CheckerProgrammer.decode(project)(configure()(importer))(importer);
225
+
226
+ export const decode_object = (importer: FunctionImporter) =>
227
+ CheckerProgrammer.decode_object(configure()(importer))(importer);
228
+
229
+ export const decode_to_json =
230
+ (checkNull: boolean) =>
231
+ (input: ts.Expression): ts.Expression =>
232
+ ts.factory.createLogicalAnd(
233
+ ExpressionFactory.isObject({
234
+ checkArray: false,
235
+ checkNull,
236
+ })(input),
237
+ ts.factory.createStrictEquality(
238
+ ts.factory.createStringLiteral("function"),
239
+ ValueFactory.TYPEOF(
240
+ IdentifierFactory.access(input)("toJSON"),
241
+ ),
242
+ ),
243
+ );
244
+
245
+ export const decode_functional = (input: ts.Expression) =>
246
+ ts.factory.createStrictEquality(
247
+ ts.factory.createStringLiteral("function"),
248
+ ValueFactory.TYPEOF(input),
249
+ );
250
+ }
@@ -1,66 +1,66 @@
1
- import ts from "typescript";
2
-
3
- import { MetadataCollection } from "../factories/MetadataCollection";
4
- import { MetadataFactory } from "../factories/MetadataFactory";
5
-
6
- import { Metadata } from "../metadata/Metadata";
7
-
8
- import { IProject } from "../transformers/IProject";
9
-
10
- import { Atomic } from "../typings/Atomic";
11
-
12
- import { ArrayUtil } from "../utils/ArrayUtil";
13
-
14
- export namespace LiteralsProgrammer {
15
- /**
16
- * @deprecated Use `write()` function instead
17
- */
18
- export const generate = (project: IProject) => (type: ts.Type) =>
19
- write(project)(type);
20
-
21
- export const write = (project: IProject) => (type: ts.Type) => {
22
- const meta: Metadata = MetadataFactory.analyze(project.checker)({
23
- resolve: true,
24
- constant: true,
25
- absorb: true,
26
- validate: (meta) => {
27
- const length: number =
28
- meta.constants
29
- .map((c) => c.values.length)
30
- .reduce((a, b) => a + b, 0) +
31
- meta.atomics.filter((type) => type === "boolean").length;
32
- if (0 === length) throw new Error(ErrorMessages.NO);
33
- else if (meta.size() !== length)
34
- throw new Error(ErrorMessages.ONLY);
35
- },
36
- })(new MetadataCollection())(type);
37
- const values: Set<Atomic.Type> = new Set([
38
- ...ArrayUtil.flat<Atomic.Type>(meta.constants.map((c) => c.values)),
39
- ...(meta.atomics.filter((type) => type === "boolean").length
40
- ? [true, false]
41
- : []),
42
- ]);
43
- return ts.factory.createAsExpression(
44
- ts.factory.createArrayLiteralExpression(
45
- [...values].map((v) =>
46
- typeof v === "boolean"
47
- ? v
48
- ? ts.factory.createTrue()
49
- : ts.factory.createFalse()
50
- : typeof v === "number"
51
- ? ts.factory.createNumericLiteral(v)
52
- : typeof v === "bigint"
53
- ? ts.factory.createBigIntLiteral(v.toString())
54
- : ts.factory.createStringLiteral(v),
55
- ),
56
- true,
57
- ),
58
- ts.factory.createTypeReferenceNode("const"),
59
- );
60
- };
61
- }
62
-
63
- enum ErrorMessages {
64
- NO = "Error on typia.literals(): no literal type found.",
65
- ONLY = "Error on typia.literals(): only literal type allowed.",
66
- }
1
+ import ts from "typescript";
2
+
3
+ import { MetadataCollection } from "../factories/MetadataCollection";
4
+ import { MetadataFactory } from "../factories/MetadataFactory";
5
+
6
+ import { Metadata } from "../metadata/Metadata";
7
+
8
+ import { IProject } from "../transformers/IProject";
9
+
10
+ import { Atomic } from "../typings/Atomic";
11
+
12
+ import { ArrayUtil } from "../utils/ArrayUtil";
13
+
14
+ export namespace LiteralsProgrammer {
15
+ /**
16
+ * @deprecated Use `write()` function instead
17
+ */
18
+ export const generate = (project: IProject) => (type: ts.Type) =>
19
+ write(project)(type);
20
+
21
+ export const write = (project: IProject) => (type: ts.Type) => {
22
+ const meta: Metadata = MetadataFactory.analyze(project.checker)({
23
+ resolve: true,
24
+ constant: true,
25
+ absorb: true,
26
+ validate: (meta) => {
27
+ const length: number =
28
+ meta.constants
29
+ .map((c) => c.values.length)
30
+ .reduce((a, b) => a + b, 0) +
31
+ meta.atomics.filter((type) => type === "boolean").length;
32
+ if (0 === length) throw new Error(ErrorMessages.NO);
33
+ else if (meta.size() !== length)
34
+ throw new Error(ErrorMessages.ONLY);
35
+ },
36
+ })(new MetadataCollection())(type);
37
+ const values: Set<Atomic.Type> = new Set([
38
+ ...ArrayUtil.flat<Atomic.Type>(meta.constants.map((c) => c.values)),
39
+ ...(meta.atomics.filter((type) => type === "boolean").length
40
+ ? [true, false]
41
+ : []),
42
+ ]);
43
+ return ts.factory.createAsExpression(
44
+ ts.factory.createArrayLiteralExpression(
45
+ [...values].map((v) =>
46
+ typeof v === "boolean"
47
+ ? v
48
+ ? ts.factory.createTrue()
49
+ : ts.factory.createFalse()
50
+ : typeof v === "number"
51
+ ? ts.factory.createNumericLiteral(v)
52
+ : typeof v === "bigint"
53
+ ? ts.factory.createBigIntLiteral(v.toString())
54
+ : ts.factory.createStringLiteral(v),
55
+ ),
56
+ true,
57
+ ),
58
+ ts.factory.createTypeReferenceNode("const"),
59
+ );
60
+ };
61
+ }
62
+
63
+ enum ErrorMessages {
64
+ NO = "Error on typia.literals(): no literal type found.",
65
+ ONLY = "Error on typia.literals(): only literal type allowed.",
66
+ }