typia 5.2.0 → 5.2.1

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.
@@ -1,252 +1,252 @@
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 "../schemas/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
- equals: !!options?.object,
27
- trace: false,
28
- path: false,
29
- numeric: OptionPredicator.numeric({
30
- numeric: options?.numeric,
31
- }),
32
- atomist: () => (entry) => () =>
33
- [
34
- ...(entry.expression ? [entry.expression] : []),
35
- ...(entry.conditions.length === 0
36
- ? []
37
- : [
38
- entry.conditions
39
- .map((set) =>
40
- set
41
- .map((s) => s.expression)
42
- .reduce((a, b) =>
43
- ts.factory.createLogicalAnd(a, b),
44
- ),
45
- )
46
- .reduce((a, b) =>
47
- ts.factory.createLogicalOr(a, b),
48
- ),
49
- ]),
50
- ].reduce((x, y) => ts.factory.createLogicalAnd(x, y)),
51
- combiner: () => (type: "and" | "or") => {
52
- const initial: ts.TrueLiteral | ts.FalseLiteral =
53
- type === "and"
54
- ? ts.factory.createTrue()
55
- : ts.factory.createFalse();
56
- const binder =
57
- type === "and"
58
- ? ts.factory.createLogicalAnd
59
- : ts.factory.createLogicalOr;
60
- return (
61
- _input: ts.Expression,
62
- binaries: CheckerProgrammer.IBinary[],
63
- ) =>
64
- binaries.length
65
- ? binaries
66
- .map((binary) => binary.expression)
67
- .reduce((x, y) => binder(x, y))
68
- : initial;
69
- },
70
- joiner: {
71
- object:
72
- options?.object ||
73
- check_object({
74
- equals: !!options?.object,
75
- undefined: OptionPredicator.undefined({
76
- undefined: options?.undefined,
77
- }),
78
- assert: true,
79
- reduce: ts.factory.createLogicalAnd,
80
- positive: ts.factory.createTrue(),
81
- superfluous: () => ts.factory.createFalse(),
82
- })(importer),
83
- array: (input, arrow) =>
84
- ts.factory.createCallExpression(
85
- IdentifierFactory.access(input)("every"),
86
- undefined,
87
- [arrow],
88
- ),
89
- failure: () => ts.factory.createFalse(),
90
- },
91
- success: ts.factory.createTrue(),
92
- });
93
-
94
- export namespace CONFIG {
95
- export interface IOptions {
96
- numeric: boolean;
97
- undefined: boolean;
98
- object: (
99
- input: ts.Expression,
100
- entries: IExpressionEntry<ts.Expression>[],
101
- ) => ts.Expression;
102
- }
103
- }
104
-
105
- /* -----------------------------------------------------------
106
- WRITERS
107
- ----------------------------------------------------------- */
108
- export const write =
109
- (project: IProject) =>
110
- (modulo: ts.LeftHandSideExpression, disable?: boolean) =>
111
- (equals: boolean) => {
112
- const importer: FunctionImporter =
113
- disable === <any>{}
114
- ? disable_function_importer_declare(
115
- new FunctionImporter(modulo.getText()),
116
- )
117
- : new FunctionImporter(modulo.getText());
118
-
119
- // CONFIGURATION
120
- const config: CheckerProgrammer.IConfig = {
121
- ...configure({
122
- object: check_object({
123
- equals,
124
- undefined: OptionPredicator.undefined(project.options),
125
- assert: true,
126
- reduce: ts.factory.createLogicalAnd,
127
- positive: ts.factory.createTrue(),
128
- superfluous: () => ts.factory.createFalse(),
129
- })(importer),
130
- numeric: OptionPredicator.numeric(project.options),
131
- })(importer),
132
- trace: equals,
133
- addition: () => importer.declare(modulo),
134
- };
135
-
136
- config.decoder = () => (input, target, explore) => {
137
- if (
138
- target.size() === 1 &&
139
- target.objects.length === 1 &&
140
- target.isRequired() === true &&
141
- target.nullable === false
142
- ) {
143
- // ONLY WHEN OBJECT WITH SOME ATOMIC PROPERTIES
144
- const obj: MetadataObject = target.objects[0]!;
145
- if (
146
- obj._Is_simple(explore.from === "top" ? 0 : 1) &&
147
- (equals === false ||
148
- OptionPredicator.undefined(project.options) ===
149
- false)
150
- )
151
- return ts.factory.createLogicalAnd(
152
- ExpressionFactory.isObject({
153
- checkNull: true,
154
- checkArray: false,
155
- })(input),
156
- config.joiner.object(
157
- ts.factory.createAsExpression(
158
- input,
159
- TypeFactory.keyword("any"),
160
- ),
161
- feature_object_entries(config as any)(importer)(
162
- obj,
163
- )(
164
- ts.factory.createAsExpression(
165
- input,
166
- TypeFactory.keyword("any"),
167
- ),
168
- "top",
169
- ),
170
- ),
171
- );
172
- }
173
- return CheckerProgrammer.decode(project)(config)(importer)(
174
- input,
175
- target,
176
- explore,
177
- );
178
- };
179
-
180
- // GENERATE CHECKER
181
- return CheckerProgrammer.write(project)(config)(importer);
182
- };
183
-
184
- export const write_function_statements =
185
- (project: IProject) =>
186
- (importer: FunctionImporter) =>
187
- (collection: MetadataCollection) => {
188
- const config = configure()(importer);
189
- const objects =
190
- CheckerProgrammer.write_object_functions(project)(config)(
191
- importer,
192
- )(collection);
193
- const unions =
194
- CheckerProgrammer.write_union_functions(project)(config)(
195
- importer,
196
- )(collection);
197
- const arrays =
198
- CheckerProgrammer.write_array_functions(project)(config)(
199
- importer,
200
- )(collection);
201
- const tuples =
202
- CheckerProgrammer.write_tuple_functions(project)(config)(
203
- importer,
204
- )(collection);
205
-
206
- return [
207
- ...objects.filter((_, i) =>
208
- importer.hasLocal(`${config.prefix}o${i}`),
209
- ),
210
- ...unions.filter((_, i) =>
211
- importer.hasLocal(`${config.prefix}u${i}`),
212
- ),
213
- ...arrays.filter((_, i) =>
214
- importer.hasLocal(`${config.prefix}a${i}`),
215
- ),
216
- ...tuples.filter((_, i) =>
217
- importer.hasLocal(`${config.prefix}t${i}`),
218
- ),
219
- ];
220
- };
221
-
222
- /* -----------------------------------------------------------
223
- DECODERS
224
- ----------------------------------------------------------- */
225
- export const decode = (project: IProject) => (importer: FunctionImporter) =>
226
- CheckerProgrammer.decode(project)(configure()(importer))(importer);
227
-
228
- export const decode_object = (importer: FunctionImporter) =>
229
- CheckerProgrammer.decode_object(configure()(importer))(importer);
230
-
231
- export const decode_to_json =
232
- (checkNull: boolean) =>
233
- (input: ts.Expression): ts.Expression =>
234
- ts.factory.createLogicalAnd(
235
- ExpressionFactory.isObject({
236
- checkArray: false,
237
- checkNull,
238
- })(input),
239
- ts.factory.createStrictEquality(
240
- ts.factory.createStringLiteral("function"),
241
- ValueFactory.TYPEOF(
242
- IdentifierFactory.access(input)("toJSON"),
243
- ),
244
- ),
245
- );
246
-
247
- export const decode_functional = (input: ts.Expression) =>
248
- ts.factory.createStrictEquality(
249
- ts.factory.createStringLiteral("function"),
250
- ValueFactory.TYPEOF(input),
251
- );
252
- }
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 "../schemas/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
+ equals: !!options?.object,
27
+ trace: false,
28
+ path: false,
29
+ numeric: OptionPredicator.numeric({
30
+ numeric: options?.numeric,
31
+ }),
32
+ atomist: () => (entry) => () =>
33
+ [
34
+ ...(entry.expression ? [entry.expression] : []),
35
+ ...(entry.conditions.length === 0
36
+ ? []
37
+ : [
38
+ entry.conditions
39
+ .map((set) =>
40
+ set
41
+ .map((s) => s.expression)
42
+ .reduce((a, b) =>
43
+ ts.factory.createLogicalAnd(a, b),
44
+ ),
45
+ )
46
+ .reduce((a, b) =>
47
+ ts.factory.createLogicalOr(a, b),
48
+ ),
49
+ ]),
50
+ ].reduce((x, y) => ts.factory.createLogicalAnd(x, y)),
51
+ combiner: () => (type: "and" | "or") => {
52
+ const initial: ts.TrueLiteral | ts.FalseLiteral =
53
+ type === "and"
54
+ ? ts.factory.createTrue()
55
+ : ts.factory.createFalse();
56
+ const binder =
57
+ type === "and"
58
+ ? ts.factory.createLogicalAnd
59
+ : ts.factory.createLogicalOr;
60
+ return (
61
+ _input: ts.Expression,
62
+ binaries: CheckerProgrammer.IBinary[],
63
+ ) =>
64
+ binaries.length
65
+ ? binaries
66
+ .map((binary) => binary.expression)
67
+ .reduce((x, y) => binder(x, y))
68
+ : initial;
69
+ },
70
+ joiner: {
71
+ object:
72
+ options?.object ||
73
+ check_object({
74
+ equals: !!options?.object,
75
+ undefined: OptionPredicator.undefined({
76
+ undefined: options?.undefined,
77
+ }),
78
+ assert: true,
79
+ reduce: ts.factory.createLogicalAnd,
80
+ positive: ts.factory.createTrue(),
81
+ superfluous: () => ts.factory.createFalse(),
82
+ })(importer),
83
+ array: (input, arrow) =>
84
+ ts.factory.createCallExpression(
85
+ IdentifierFactory.access(input)("every"),
86
+ undefined,
87
+ [arrow],
88
+ ),
89
+ failure: () => ts.factory.createFalse(),
90
+ },
91
+ success: ts.factory.createTrue(),
92
+ });
93
+
94
+ export namespace CONFIG {
95
+ export interface IOptions {
96
+ numeric: boolean;
97
+ undefined: boolean;
98
+ object: (
99
+ input: ts.Expression,
100
+ entries: IExpressionEntry<ts.Expression>[],
101
+ ) => ts.Expression;
102
+ }
103
+ }
104
+
105
+ /* -----------------------------------------------------------
106
+ WRITERS
107
+ ----------------------------------------------------------- */
108
+ export const write =
109
+ (project: IProject) =>
110
+ (modulo: ts.LeftHandSideExpression, disable?: boolean) =>
111
+ (equals: boolean) => {
112
+ const importer: FunctionImporter =
113
+ disable === <any>{}
114
+ ? disable_function_importer_declare(
115
+ new FunctionImporter(modulo.getText()),
116
+ )
117
+ : new FunctionImporter(modulo.getText());
118
+
119
+ // CONFIGURATION
120
+ const config: CheckerProgrammer.IConfig = {
121
+ ...configure({
122
+ object: check_object({
123
+ equals,
124
+ undefined: OptionPredicator.undefined(project.options),
125
+ assert: true,
126
+ reduce: ts.factory.createLogicalAnd,
127
+ positive: ts.factory.createTrue(),
128
+ superfluous: () => ts.factory.createFalse(),
129
+ })(importer),
130
+ numeric: OptionPredicator.numeric(project.options),
131
+ })(importer),
132
+ trace: equals,
133
+ addition: () => importer.declare(modulo),
134
+ };
135
+
136
+ config.decoder = () => (input, target, explore) => {
137
+ if (
138
+ target.size() === 1 &&
139
+ target.objects.length === 1 &&
140
+ target.isRequired() === true &&
141
+ target.nullable === false
142
+ ) {
143
+ // ONLY WHEN OBJECT WITH SOME ATOMIC PROPERTIES
144
+ const obj: MetadataObject = target.objects[0]!;
145
+ if (
146
+ obj._Is_simple(explore.from === "top" ? 0 : 1) &&
147
+ (equals === false ||
148
+ OptionPredicator.undefined(project.options) ===
149
+ false)
150
+ )
151
+ return ts.factory.createLogicalAnd(
152
+ ExpressionFactory.isObject({
153
+ checkNull: true,
154
+ checkArray: false,
155
+ })(input),
156
+ config.joiner.object(
157
+ ts.factory.createAsExpression(
158
+ input,
159
+ TypeFactory.keyword("any"),
160
+ ),
161
+ feature_object_entries(config as any)(importer)(
162
+ obj,
163
+ )(
164
+ ts.factory.createAsExpression(
165
+ input,
166
+ TypeFactory.keyword("any"),
167
+ ),
168
+ "top",
169
+ ),
170
+ ),
171
+ );
172
+ }
173
+ return CheckerProgrammer.decode(project)(config)(importer)(
174
+ input,
175
+ target,
176
+ explore,
177
+ );
178
+ };
179
+
180
+ // GENERATE CHECKER
181
+ return CheckerProgrammer.write(project)(config)(importer);
182
+ };
183
+
184
+ export const write_function_statements =
185
+ (project: IProject) =>
186
+ (importer: FunctionImporter) =>
187
+ (collection: MetadataCollection) => {
188
+ const config = configure()(importer);
189
+ const objects =
190
+ CheckerProgrammer.write_object_functions(project)(config)(
191
+ importer,
192
+ )(collection);
193
+ const unions =
194
+ CheckerProgrammer.write_union_functions(project)(config)(
195
+ importer,
196
+ )(collection);
197
+ const arrays =
198
+ CheckerProgrammer.write_array_functions(project)(config)(
199
+ importer,
200
+ )(collection);
201
+ const tuples =
202
+ CheckerProgrammer.write_tuple_functions(project)(config)(
203
+ importer,
204
+ )(collection);
205
+
206
+ return [
207
+ ...objects.filter((_, i) =>
208
+ importer.hasLocal(`${config.prefix}o${i}`),
209
+ ),
210
+ ...unions.filter((_, i) =>
211
+ importer.hasLocal(`${config.prefix}u${i}`),
212
+ ),
213
+ ...arrays.filter((_, i) =>
214
+ importer.hasLocal(`${config.prefix}a${i}`),
215
+ ),
216
+ ...tuples.filter((_, i) =>
217
+ importer.hasLocal(`${config.prefix}t${i}`),
218
+ ),
219
+ ];
220
+ };
221
+
222
+ /* -----------------------------------------------------------
223
+ DECODERS
224
+ ----------------------------------------------------------- */
225
+ export const decode = (project: IProject) => (importer: FunctionImporter) =>
226
+ CheckerProgrammer.decode(project)(configure()(importer))(importer);
227
+
228
+ export const decode_object = (importer: FunctionImporter) =>
229
+ CheckerProgrammer.decode_object(configure()(importer))(importer);
230
+
231
+ export const decode_to_json =
232
+ (checkNull: boolean) =>
233
+ (input: ts.Expression): ts.Expression =>
234
+ ts.factory.createLogicalAnd(
235
+ ExpressionFactory.isObject({
236
+ checkArray: false,
237
+ checkNull,
238
+ })(input),
239
+ ts.factory.createStrictEquality(
240
+ ts.factory.createStringLiteral("function"),
241
+ ValueFactory.TYPEOF(
242
+ IdentifierFactory.access(input)("toJSON"),
243
+ ),
244
+ ),
245
+ );
246
+
247
+ export const decode_functional = (input: ts.Expression) =>
248
+ ts.factory.createStrictEquality(
249
+ ts.factory.createStringLiteral("function"),
250
+ ValueFactory.TYPEOF(input),
251
+ );
252
+ }