typia 3.8.2 → 3.8.3-dev.20230429

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 (173) hide show
  1. package/README.md +1 -1
  2. package/lib/Primitive.d.ts +10 -10
  3. package/lib/factories/MetadataFactory.d.ts +1 -1
  4. package/lib/factories/MetadataFactory.js +1 -1
  5. package/lib/factories/MetadataFactory.js.map +1 -1
  6. package/lib/factories/internal/metadata/emplace_metadata_object.js +3 -1
  7. package/lib/factories/internal/metadata/emplace_metadata_object.js.map +1 -1
  8. package/lib/factories/internal/metadata/iterate_metadata_tuple.js +2 -0
  9. package/lib/factories/internal/metadata/iterate_metadata_tuple.js.map +1 -1
  10. package/lib/metadata/IMetadata.d.ts +1 -0
  11. package/lib/metadata/Metadata.d.ts +1 -0
  12. package/lib/metadata/Metadata.js +5 -0
  13. package/lib/metadata/Metadata.js.map +1 -1
  14. package/lib/programmers/CheckerProgrammer.js +17 -6
  15. package/lib/programmers/CheckerProgrammer.js.map +1 -1
  16. package/lib/programmers/internal/application_object.js +1 -0
  17. package/lib/programmers/internal/application_object.js.map +1 -1
  18. package/lib/programmers/internal/application_tuple.js +1 -1
  19. package/lib/programmers/internal/application_tuple.js.map +1 -1
  20. package/lib/schemas/IJsonSchema.d.ts +1 -0
  21. package/package.json +1 -1
  22. package/src/Primitive.ts +123 -120
  23. package/src/executable/TypiaGenerateWizard.ts +85 -85
  24. package/src/executable/TypiaSetupWizard.ts +133 -133
  25. package/src/executable/setup/CommandExecutor.ts +8 -8
  26. package/src/executable/setup/PackageManager.ts +71 -71
  27. package/src/executable/typia.ts +52 -52
  28. package/src/factories/CommentFactory.ts +14 -14
  29. package/src/factories/ExpressionFactory.ts +70 -70
  30. package/src/factories/IdentifierFactory.ts +63 -63
  31. package/src/factories/LiteralFactory.ts +39 -39
  32. package/src/factories/MetadataCollection.ts +121 -121
  33. package/src/factories/MetadataFactory.ts +62 -62
  34. package/src/factories/MetadataTagFactory.ts +297 -297
  35. package/src/factories/StatementFactory.ts +24 -24
  36. package/src/factories/TemplateFactory.ts +58 -58
  37. package/src/factories/TypeFactory.ts +124 -124
  38. package/src/factories/internal/metadata/MetadataHelper.ts +12 -12
  39. package/src/factories/internal/metadata/emplace_metadata_object.ts +143 -140
  40. package/src/factories/internal/metadata/iterate_metadata.ts +67 -67
  41. package/src/factories/internal/metadata/iterate_metadata_map.ts +41 -41
  42. package/src/factories/internal/metadata/iterate_metadata_native.ts +219 -219
  43. package/src/factories/internal/metadata/iterate_metadata_resolve.ts +27 -27
  44. package/src/factories/internal/metadata/iterate_metadata_set.ts +33 -33
  45. package/src/factories/internal/metadata/iterate_metadata_tuple.ts +48 -44
  46. package/src/factories/internal/metadata/iterate_metadata_union.ts +57 -57
  47. package/src/functional/$any.ts +2 -2
  48. package/src/functional/$is_between.ts +2 -2
  49. package/src/functional/$is_custom.ts +14 -14
  50. package/src/functional/$is_date.ts +3 -3
  51. package/src/functional/$is_datetime.ts +2 -2
  52. package/src/functional/$is_email.ts +4 -4
  53. package/src/functional/$is_ipv4.ts +4 -4
  54. package/src/functional/$is_ipv6.ts +4 -4
  55. package/src/functional/$is_url.ts +4 -4
  56. package/src/functional/$is_uuid.ts +4 -4
  57. package/src/functional/$join.ts +46 -46
  58. package/src/functional/$number.ts +12 -12
  59. package/src/functional/$rest.ts +3 -3
  60. package/src/functional/$string.ts +37 -37
  61. package/src/functional/$tail.ts +5 -5
  62. package/src/metadata/IMetadata.ts +1 -0
  63. package/src/metadata/Metadata.ts +539 -533
  64. package/src/metadata/MetadataConstant.ts +3 -3
  65. package/src/metadata/MetadataObject.ts +127 -127
  66. package/src/module.ts +2043 -2043
  67. package/src/programmers/ApplicationProgrammer.ts +62 -62
  68. package/src/programmers/AssertCloneProgrammer.ts +79 -79
  69. package/src/programmers/AssertParseProgrammer.ts +74 -74
  70. package/src/programmers/AssertProgrammer.ts +285 -285
  71. package/src/programmers/AssertPruneProgrammer.ts +76 -76
  72. package/src/programmers/AssertStringifyProgrammer.ts +74 -74
  73. package/src/programmers/CheckerProgrammer.ts +901 -875
  74. package/src/programmers/CloneProgrammer.ts +389 -389
  75. package/src/programmers/FeatureProgrammer.ts +500 -500
  76. package/src/programmers/IsCloneProgrammer.ts +86 -86
  77. package/src/programmers/IsParseProgrammer.ts +80 -80
  78. package/src/programmers/IsProgrammer.ts +212 -212
  79. package/src/programmers/IsPruneProgrammer.ts +81 -81
  80. package/src/programmers/IsStringifyProgrammer.ts +84 -84
  81. package/src/programmers/LiteralsProgrammer.ts +65 -65
  82. package/src/programmers/PruneProgrammer.ts +347 -347
  83. package/src/programmers/RandomProgrammer.ts +423 -423
  84. package/src/programmers/StringifyProgrammer.ts +798 -798
  85. package/src/programmers/TypiaProgrammer.ts +129 -129
  86. package/src/programmers/ValidateCloneProgrammer.ts +93 -93
  87. package/src/programmers/ValidateParseProgrammer.ts +78 -78
  88. package/src/programmers/ValidateProgrammer.ts +316 -316
  89. package/src/programmers/ValidatePruneProgrammer.ts +86 -86
  90. package/src/programmers/ValidateStringifyProgrammer.ts +92 -92
  91. package/src/programmers/helpers/CloneJoiner.ts +131 -131
  92. package/src/programmers/helpers/FunctionImporeter.ts +54 -54
  93. package/src/programmers/helpers/OptionPredicator.ts +15 -15
  94. package/src/programmers/helpers/PruneJoiner.ts +144 -144
  95. package/src/programmers/helpers/StringifyJoinder.ts +113 -113
  96. package/src/programmers/helpers/StringifyPredicator.ts +12 -12
  97. package/src/programmers/helpers/UnionExplorer.ts +275 -275
  98. package/src/programmers/helpers/UnionPredicator.ts +81 -81
  99. package/src/programmers/internal/application_array.ts +36 -36
  100. package/src/programmers/internal/application_boolean.ts +17 -17
  101. package/src/programmers/internal/application_constant.ts +30 -30
  102. package/src/programmers/internal/application_default_string.ts +33 -33
  103. package/src/programmers/internal/application_native.ts +32 -32
  104. package/src/programmers/internal/application_number.ts +73 -73
  105. package/src/programmers/internal/application_object.ts +155 -154
  106. package/src/programmers/internal/application_schema.ts +213 -213
  107. package/src/programmers/internal/application_string.ts +45 -45
  108. package/src/programmers/internal/application_templates.ts +26 -26
  109. package/src/programmers/internal/application_tuple.ts +31 -29
  110. package/src/programmers/internal/check_array.ts +30 -30
  111. package/src/programmers/internal/check_custom.ts +31 -31
  112. package/src/programmers/internal/check_dynamic_properties.ts +194 -194
  113. package/src/programmers/internal/check_everything.ts +28 -28
  114. package/src/programmers/internal/check_native.ts +21 -21
  115. package/src/programmers/internal/check_string_tags.ts +67 -67
  116. package/src/programmers/internal/check_union_array_like.ts +271 -271
  117. package/src/programmers/internal/check_union_tuple.ts +33 -33
  118. package/src/programmers/internal/random_custom.ts +29 -29
  119. package/src/programmers/internal/stringify_dynamic_properties.ts +167 -167
  120. package/src/programmers/internal/stringify_native.ts +7 -7
  121. package/src/programmers/internal/stringify_regular_properties.ts +83 -83
  122. package/src/schemas/IJsonSchema.ts +130 -129
  123. package/src/transform.ts +17 -17
  124. package/src/transformers/CallExpressionTransformer.ts +177 -177
  125. package/src/transformers/FileTransformer.ts +47 -47
  126. package/src/transformers/NodeTransformer.ts +13 -13
  127. package/src/transformers/features/miscellaneous/ApplicationTransformer.ts +114 -114
  128. package/src/transformers/features/miscellaneous/AssertCloneTransformer.ts +9 -9
  129. package/src/transformers/features/miscellaneous/AssertPruneTransformer.ts +9 -9
  130. package/src/transformers/features/miscellaneous/CloneTransformer.ts +9 -9
  131. package/src/transformers/features/miscellaneous/CreateAssertCloneTransformer.ts +9 -9
  132. package/src/transformers/features/miscellaneous/CreateAssertPruneTransformer.ts +9 -9
  133. package/src/transformers/features/miscellaneous/CreateCloneTransformer.ts +9 -9
  134. package/src/transformers/features/miscellaneous/CreateIsCloneTransformer.ts +9 -9
  135. package/src/transformers/features/miscellaneous/CreateIsPruneTransformer.ts +9 -9
  136. package/src/transformers/features/miscellaneous/CreatePruneTransformer.ts +9 -9
  137. package/src/transformers/features/miscellaneous/CreateRandomTransformer.ts +39 -39
  138. package/src/transformers/features/miscellaneous/CreateValidateCloneTransformer.ts +9 -9
  139. package/src/transformers/features/miscellaneous/CreateValidatePruneTransformer.ts +9 -9
  140. package/src/transformers/features/miscellaneous/IsCloneTransformer.ts +9 -9
  141. package/src/transformers/features/miscellaneous/IsPruneTransformer.ts +9 -9
  142. package/src/transformers/features/miscellaneous/LiteralsTransformer.ts +28 -28
  143. package/src/transformers/features/miscellaneous/MetadataTransformer.ts +52 -52
  144. package/src/transformers/features/miscellaneous/PruneTransformer.ts +9 -9
  145. package/src/transformers/features/miscellaneous/RandomTransformer.ts +42 -42
  146. package/src/transformers/features/miscellaneous/ValidateCloneTransformer.ts +9 -9
  147. package/src/transformers/features/miscellaneous/ValidatePruneTransformer.ts +9 -9
  148. package/src/transformers/features/parsers/AssertParseTransformer.ts +9 -9
  149. package/src/transformers/features/parsers/CreateAssertParseTransformer.ts +9 -9
  150. package/src/transformers/features/parsers/CreateIsParseTransformer.ts +9 -9
  151. package/src/transformers/features/parsers/CreateValidateParseTransformer.ts +9 -9
  152. package/src/transformers/features/parsers/IsParseTransformer.ts +9 -9
  153. package/src/transformers/features/parsers/ValidateParseTransformer.ts +9 -9
  154. package/src/transformers/features/stringifiers/AssertStringifyTransformer.ts +10 -10
  155. package/src/transformers/features/stringifiers/CreateAssertStringifyTransformer.ts +12 -12
  156. package/src/transformers/features/stringifiers/CreateIsStringifyTransformer.ts +9 -9
  157. package/src/transformers/features/stringifiers/CreateStringifyTransformer.ts +9 -9
  158. package/src/transformers/features/stringifiers/CreateValidateStringifyProgrammer.ts +12 -12
  159. package/src/transformers/features/stringifiers/IsStringifyTransformer.ts +9 -9
  160. package/src/transformers/features/stringifiers/StringifyTransformer.ts +9 -9
  161. package/src/transformers/features/stringifiers/ValidateStringifyTransformer.ts +10 -10
  162. package/src/transformers/features/validators/AssertTransformer.ts +11 -11
  163. package/src/transformers/features/validators/CreateAssertTransformer.ts +13 -13
  164. package/src/transformers/features/validators/CreateIsTransformer.ts +11 -11
  165. package/src/transformers/features/validators/CreateValidateTransformer.ts +13 -13
  166. package/src/transformers/features/validators/IsTransformer.ts +11 -11
  167. package/src/transformers/features/validators/ValidateTransformer.ts +11 -11
  168. package/src/transformers/internal/GenericTransformer.ts +97 -97
  169. package/src/utils/ArrayUtil.ts +44 -44
  170. package/src/utils/Escaper.ts +45 -45
  171. package/src/utils/MapUtil.ts +12 -12
  172. package/src/utils/PatternUtil.ts +30 -30
  173. package/src/utils/RandomGenerator.ts +81 -81
@@ -1,533 +1,539 @@
1
- import { Atomic } from "../typings/Atomic";
2
- import { ClassProperties } from "../typings/ClassProperties";
3
-
4
- import { ArrayUtil } from "../utils/ArrayUtil";
5
-
6
- import { IMetadata } from "./IMetadata";
7
- import { IMetadataObject } from "./IMetadataObject";
8
- import { MetadataConstant } from "./MetadataConstant";
9
- import { MetadataObject } from "./MetadataObject";
10
- import { MetadataProperty } from "./MetadataProperty";
11
-
12
- export class Metadata {
13
- public readonly any: boolean;
14
- public readonly required: boolean;
15
- public readonly nullable: boolean;
16
- public readonly functional: boolean;
17
-
18
- public readonly resolved: Metadata | null;
19
- public readonly atomics: Atomic.Literal[];
20
- public readonly constants: MetadataConstant[];
21
- public readonly templates: Metadata[][];
22
-
23
- public readonly rest: Metadata | null;
24
- public readonly arrays: Metadata[];
25
- public readonly tuples: Metadata[][];
26
- public readonly objects: MetadataObject[];
27
-
28
- public readonly natives: string[];
29
- public readonly sets: Metadata[];
30
- public readonly maps: Metadata.Entry[];
31
-
32
- /**
33
- * @internal
34
- */
35
- private name_: string | undefined = undefined;
36
-
37
- /**
38
- * @internal
39
- */
40
- private parent_resolved_: boolean = false;
41
-
42
- /**
43
- * @internal
44
- */
45
- public union_index?: number;
46
-
47
- /* -----------------------------------------------------------
48
- CONSTRUCTORS
49
- ----------------------------------------------------------- */
50
- /**
51
- * @hidden
52
- */
53
- private constructor(props: ClassProperties<Metadata>) {
54
- this.any = props.any;
55
- this.required = props.required;
56
- this.nullable = props.nullable;
57
- this.functional = props.functional;
58
-
59
- this.resolved = props.resolved;
60
- this.atomics = props.atomics;
61
- this.constants = props.constants;
62
- this.templates = props.templates;
63
-
64
- this.rest = props.rest;
65
- this.arrays = props.arrays;
66
- this.tuples = props.tuples;
67
- this.objects = props.objects;
68
-
69
- this.natives = props.natives;
70
- this.sets = props.sets;
71
- this.maps = props.maps;
72
- }
73
-
74
- /**
75
- * @internal
76
- */
77
- public static create(props: ClassProperties<Metadata>): Metadata {
78
- return new Metadata(props);
79
- }
80
-
81
- /**
82
- * @internal
83
- */
84
- public static initialize(parentResolved: boolean = false): Metadata {
85
- const meta: Metadata = this.create({
86
- any: false,
87
- nullable: false,
88
- required: true,
89
- functional: false,
90
-
91
- resolved: null,
92
- constants: [],
93
- atomics: [],
94
- templates: [],
95
- arrays: [],
96
- tuples: [],
97
- objects: [],
98
-
99
- rest: null,
100
- natives: [],
101
- sets: [],
102
- maps: [],
103
- });
104
- meta.parent_resolved_ = parentResolved;
105
- return meta;
106
- }
107
-
108
- public toJSON(): IMetadata {
109
- return {
110
- any: this.any,
111
- required: this.required,
112
- nullable: this.nullable,
113
- functional: this.functional,
114
-
115
- atomics: this.atomics.slice(),
116
- constants: JSON.parse(JSON.stringify(this.constants)),
117
- templates: this.templates.map((tpl) =>
118
- tpl.map((meta) => meta.toJSON()),
119
- ),
120
- resolved: this.resolved ? this.resolved.toJSON() : null,
121
-
122
- rest: this.rest ? this.rest.toJSON() : null,
123
- arrays: this.arrays.map((meta) => meta.toJSON()),
124
- tuples: this.tuples.map((meta) =>
125
- meta.map((meta) => meta.toJSON()),
126
- ),
127
- objects: this.objects.map((obj) => obj.name),
128
-
129
- natives: this.natives.slice(),
130
- sets: this.sets.map((meta) => meta.toJSON()),
131
- maps: this.maps.map((entry) => ({
132
- key: entry.key.toJSON(),
133
- value: entry.value.toJSON(),
134
- })),
135
- };
136
- }
137
-
138
- public static from(meta: IMetadata, objects: IMetadataObject[]): Metadata {
139
- const dict: Map<string, MetadataObject> = new Map();
140
- for (const obj of objects)
141
- dict.set(obj.name, MetadataObject._From_without_properties(obj));
142
-
143
- for (const obj of objects) {
144
- const initialized = dict.get(obj.name)!;
145
- initialized.properties.push(
146
- ...obj.properties.map((prop) =>
147
- MetadataProperty._From(prop, dict),
148
- ),
149
- );
150
- }
151
- return this._From(meta, dict);
152
- }
153
-
154
- /**
155
- * @internal
156
- */
157
- public static _From(
158
- meta: IMetadata,
159
- objects: Map<string, MetadataObject>,
160
- ): Metadata {
161
- return this.create({
162
- any: meta.any,
163
- required: meta.required,
164
- nullable: meta.nullable,
165
- functional: meta.functional,
166
-
167
- constants: JSON.parse(JSON.stringify(meta.constants)),
168
- atomics: meta.atomics.slice(),
169
- templates: meta.templates.map((tpl) =>
170
- tpl.map((meta) => this._From(meta, objects)),
171
- ),
172
- resolved: meta.resolved ? this._From(meta.resolved, objects) : null,
173
-
174
- rest: meta.rest ? this._From(meta.rest, objects) : null,
175
- arrays: meta.arrays.map((meta) => this._From(meta, objects)),
176
- tuples: meta.tuples.map((tuple) =>
177
- tuple.map((meta) => this._From(meta, objects)),
178
- ),
179
- objects: meta.objects.map((name) => {
180
- const found = objects.get(name);
181
- if (found === undefined)
182
- throw new Error(
183
- `Error on Metadata.from(): failed to find object "${name}".`,
184
- );
185
- return found;
186
- }),
187
-
188
- natives: meta.natives.slice(),
189
- sets: meta.sets.map((meta) => this._From(meta, objects)),
190
- maps: meta.maps.map((entry) => ({
191
- key: this._From(entry.key, objects),
192
- value: this._From(entry.value, objects),
193
- })),
194
- });
195
- }
196
-
197
- /* -----------------------------------------------------------
198
- ACCESSORS
199
- ----------------------------------------------------------- */
200
- public getName(): string {
201
- this.name_ ||= getName(this);
202
- return this.name_;
203
- }
204
-
205
- public empty(): boolean {
206
- return this.bucket() === 0 || this.size() === 0;
207
- }
208
- public size(): number {
209
- return (
210
- (this.resolved ? 1 : 0) +
211
- (this.functional ? 1 : 0) +
212
- this.templates.length +
213
- this.atomics.length +
214
- this.constants
215
- .map((c) => c.values.length)
216
- .reduce((x, y) => x + y, 0) +
217
- (this.rest ? this.rest.size() : 0) +
218
- this.arrays.length +
219
- this.tuples.length +
220
- this.objects.length +
221
- this.natives.length +
222
- this.sets.length +
223
- this.maps.length
224
- );
225
- }
226
- public bucket(): number {
227
- return (
228
- (this.resolved ? 1 : 0) +
229
- (this.functional ? 1 : 0) +
230
- (this.templates.length ? 1 : 0) +
231
- (this.atomics.length ? 1 : 0) +
232
- (this.constants.length ? 1 : 0) +
233
- (this.rest ? this.rest.size() : 0) +
234
- (this.arrays.length ? 1 : 0) +
235
- (this.tuples.length ? 1 : 0) +
236
- (this.objects.length ? 1 : 0) +
237
- (this.natives.length ? 1 : 0) +
238
- (this.sets.length ? 1 : 0) +
239
- (this.maps.length ? 1 : 0)
240
- );
241
- }
242
- public isConstant(): boolean {
243
- return this.bucket() === (this.constants.length ? 1 : 0);
244
- }
245
-
246
- /**
247
- * @internal
248
- */
249
- public isUnionBucket(): boolean {
250
- const size: number = this.bucket();
251
- const emended: number = this.constants.length ? size - 1 : size;
252
- return emended > 1;
253
- }
254
-
255
- /**
256
- * @internal
257
- */
258
- public getSoleLiteral(): string | null {
259
- if (
260
- this.size() === 1 &&
261
- this.constants.length === 1 &&
262
- this.constants[0]!.type === "string" &&
263
- this.constants[0]!.values.length === 1
264
- )
265
- return this.constants[0]!.values[0] as string;
266
- else return null;
267
- }
268
-
269
- /**
270
- * @internal
271
- */
272
- public isSoleLiteral(): boolean {
273
- return this.getSoleLiteral() !== null;
274
- }
275
-
276
- /**
277
- * @internal
278
- */
279
- public isParentResolved(): boolean {
280
- return this.parent_resolved_;
281
- }
282
- }
283
- export namespace Metadata {
284
- export const intersects = (
285
- x: Metadata,
286
- y: Metadata,
287
- deep: boolean,
288
- ): boolean => {
289
- // CHECK ANY & OPTIONAL
290
- if (x.any || y.any) return true;
291
- if (x.required === false && false === y.required) return true;
292
- if (x.nullable === true && true === y.nullable) return true;
293
-
294
- //----
295
- // INSTANCES
296
- //----
297
- // ARRAYS AND OBJECTS
298
- if (deep === true) {
299
- for (const xa of x.arrays)
300
- for (const ya of y.arrays)
301
- if (intersects(xa, ya, deep)) {
302
- return true;
303
- }
304
- for (const xo of x.objects)
305
- for (const yo of y.objects)
306
- if (MetadataObject.intersects(xo, yo)) {
307
- return true;
308
- }
309
- } else {
310
- if (x.arrays.length && y.arrays.length) return true;
311
- if (x.objects.length && y.objects.length) return true;
312
- }
313
-
314
- // TUPLES
315
- for (const xt of x.tuples)
316
- for (const yt of y.tuples)
317
- if (xt.length === 0 || yt.length === 0)
318
- return xt.length === 0 && yt.length === 0;
319
- else if (
320
- xt
321
- .slice(0, Math.min(xt.length, yt.length))
322
- .some((xv, i) => intersects(xv, yt[i]!, deep))
323
- )
324
- return true;
325
-
326
- //----
327
- // VALUES
328
- //----
329
- // ATOMICS
330
- for (const atomic of x.atomics)
331
- if (y.atomics.includes(atomic)) return true;
332
-
333
- // CONSTANTS
334
- for (const constant of x.constants) {
335
- const opposite: MetadataConstant | undefined = y.constants.find(
336
- (elem) => elem.type === constant.type,
337
- );
338
- if (opposite === undefined) continue;
339
-
340
- const values: Set<any> = new Set([
341
- ...constant.values,
342
- ...opposite.values,
343
- ]);
344
- if (values.size !== constant.values.length + opposite.values.length)
345
- return true;
346
- }
347
-
348
- // FUNCTIONAL
349
- if (x.functional === true && y.functional === true) return true;
350
-
351
- return false;
352
- };
353
-
354
- export const covers = (x: Metadata, y: Metadata): boolean => {
355
- // CHECK ANY
356
- if (x.any) return true;
357
- else if (y.any) return false;
358
-
359
- //----
360
- // INSTANCES
361
- //----
362
- // ARRAYS
363
- for (const ya of y.arrays)
364
- if (x.arrays.some((xa) => covers(xa, ya) === true) === false)
365
- return false;
366
-
367
- // OBJECTS
368
- for (const yo of y.objects)
369
- if (x.objects.some((xo) => MetadataObject.covers(xo, yo)) === false)
370
- return false;
371
-
372
- // TUPLES
373
- for (const yt of y.tuples)
374
- if (
375
- yt.length !== 0 &&
376
- x.tuples.some(
377
- (xt) =>
378
- xt.length >= yt.length &&
379
- xt
380
- .slice(yt.length)
381
- .every((xv, i) => covers(xv, yt[i]!)),
382
- ) === false
383
- )
384
- return false;
385
-
386
- // NATIVES
387
-
388
- // SETS
389
- for (const ys of y.sets)
390
- if (x.sets.some((xs) => covers(xs, ys)) === false) return false;
391
-
392
- //----
393
- // VALUES
394
- //----
395
- // ATOMICS
396
- if (y.atomics.some((atomic) => x.atomics.includes(atomic) === false))
397
- return false;
398
-
399
- // CONSTANTS
400
- for (const yc of y.constants) {
401
- const xc: MetadataConstant | undefined = x.constants.find(
402
- (elem) => elem.type === yc.type,
403
- );
404
- if (xc === undefined) return false;
405
- else if (
406
- (yc.values as number[]).some(
407
- (yv) => xc.values.includes(yv as never) === false,
408
- )
409
- )
410
- return false;
411
- }
412
-
413
- // FUNCTIONAL
414
- if (x.functional === false && y.functional) return false;
415
-
416
- // SUCCESS
417
- return true;
418
- };
419
-
420
- export const merge = (x: Metadata, y: Metadata): Metadata => {
421
- const output: Metadata = Metadata.create({
422
- any: x.any || y.any,
423
- nullable: x.nullable || y.nullable,
424
- required: x.required && y.required,
425
- functional: x.functional || y.functional,
426
-
427
- resolved:
428
- x.resolved !== null && y.resolved !== null
429
- ? merge(x.resolved, y.resolved)
430
- : x.resolved || y.resolved,
431
- atomics: [...new Set([...x.atomics, ...y.atomics])],
432
- constants: [...x.constants],
433
- templates: x.templates.slice(),
434
-
435
- rest:
436
- x.rest !== null && y.rest !== null
437
- ? merge(x.rest, y.rest)
438
- : x.rest ?? y.rest,
439
- arrays: x.arrays.slice(),
440
- tuples: x.tuples.slice(),
441
- objects: x.objects.slice(),
442
-
443
- natives: [...new Set([...x.natives, ...y.natives])],
444
- sets: x.sets.slice(),
445
- maps: x.maps.slice(),
446
- });
447
- for (const constant of y.constants) {
448
- const target: MetadataConstant = ArrayUtil.take(
449
- output.constants,
450
- (elem) => elem.type === constant.type,
451
- () => ({
452
- type: constant.type,
453
- values: [],
454
- }),
455
- );
456
- for (const value of constant.values)
457
- ArrayUtil.add(target.values, value);
458
- }
459
- for (const array of y.arrays)
460
- ArrayUtil.set(output.arrays, array, (elem) => elem.getName());
461
- for (const obj of y.objects)
462
- ArrayUtil.set(output.objects, obj, (elem) => elem.name);
463
-
464
- if (x.rest !== null)
465
- ArrayUtil.set(output.arrays, x.rest, (elem) => elem.getName());
466
- if (y.rest !== null)
467
- ArrayUtil.set(output.arrays, y.rest, (elem) => elem.getName());
468
-
469
- return output;
470
- };
471
- }
472
-
473
- const getName = (metadata: Metadata): string => {
474
- if (metadata.any === true) return "any";
475
-
476
- const elements: string[] = [];
477
-
478
- // OPTIONAL
479
- if (metadata.nullable === true) elements.push("null");
480
- if (metadata.required === false) elements.push("undefined");
481
-
482
- // ATOMIC
483
- for (const type of metadata.atomics) {
484
- elements.push(type);
485
- }
486
- for (const constant of metadata.constants)
487
- for (const value of constant.values)
488
- elements.push(JSON.stringify(value));
489
- for (const template of metadata.templates)
490
- elements.push(
491
- "`" +
492
- template
493
- .map((child) =>
494
- child.isConstant() && child.size() === 1
495
- ? child.constants[0]!.values[0]!
496
- : `$\{${child.getName()}\}`,
497
- )
498
- .join("")
499
- .split("`")
500
- .join("\\`") +
501
- "`",
502
- );
503
-
504
- // NATIVES
505
- for (const native of metadata.natives) elements.push(native);
506
- for (const set of metadata.sets) elements.push(`Set<${set.getName()}>`);
507
- for (const map of metadata.maps)
508
- elements.push(`Map<${map.key.getName()}, ${map.value.getName()}>`);
509
-
510
- // ARRAY
511
- if (metadata.rest !== null) elements.push(`...${metadata.rest.getName()}`);
512
- for (const tuple of metadata.tuples)
513
- elements.push(`[${tuple.map((elem) => elem.getName()).join(", ")}]`);
514
- for (const array of metadata.arrays)
515
- elements.push(`Array<${array.getName()}>`);
516
-
517
- // OBJECT
518
- for (const object of metadata.objects) elements.push(object.name);
519
- if (metadata.resolved !== null) elements.push(metadata.resolved.getName());
520
-
521
- // RETURNS
522
- if (elements.length === 0) return "unknown";
523
- else if (elements.length === 1) return elements[0]!;
524
-
525
- elements.sort();
526
- return `(${elements.join(" | ")})`;
527
- };
528
- export namespace Metadata {
529
- export interface Entry {
530
- key: Metadata;
531
- value: Metadata;
532
- }
533
- }
1
+ import { Atomic } from "../typings/Atomic";
2
+ import { ClassProperties } from "../typings/ClassProperties";
3
+
4
+ import { ArrayUtil } from "../utils/ArrayUtil";
5
+
6
+ import { IMetadata } from "./IMetadata";
7
+ import { IMetadataObject } from "./IMetadataObject";
8
+ import { MetadataConstant } from "./MetadataConstant";
9
+ import { MetadataObject } from "./MetadataObject";
10
+ import { MetadataProperty } from "./MetadataProperty";
11
+
12
+ export class Metadata {
13
+ public readonly any: boolean;
14
+ public readonly required: boolean;
15
+ public readonly optional: boolean;
16
+ public readonly nullable: boolean;
17
+ public readonly functional: boolean;
18
+
19
+ public readonly resolved: Metadata | null;
20
+ public readonly atomics: Atomic.Literal[];
21
+ public readonly constants: MetadataConstant[];
22
+ public readonly templates: Metadata[][];
23
+
24
+ public readonly rest: Metadata | null;
25
+ public readonly arrays: Metadata[];
26
+ public readonly tuples: Metadata[][];
27
+ public readonly objects: MetadataObject[];
28
+
29
+ public readonly natives: string[];
30
+ public readonly sets: Metadata[];
31
+ public readonly maps: Metadata.Entry[];
32
+
33
+ /**
34
+ * @internal
35
+ */
36
+ private name_: string | undefined = undefined;
37
+
38
+ /**
39
+ * @internal
40
+ */
41
+ private parent_resolved_: boolean = false;
42
+
43
+ /**
44
+ * @internal
45
+ */
46
+ public union_index?: number;
47
+
48
+ /* -----------------------------------------------------------
49
+ CONSTRUCTORS
50
+ ----------------------------------------------------------- */
51
+ /**
52
+ * @hidden
53
+ */
54
+ private constructor(props: ClassProperties<Metadata>) {
55
+ this.any = props.any;
56
+ this.required = props.required;
57
+ this.optional = props.optional;
58
+ this.nullable = props.nullable;
59
+ this.functional = props.functional;
60
+
61
+ this.resolved = props.resolved;
62
+ this.atomics = props.atomics;
63
+ this.constants = props.constants;
64
+ this.templates = props.templates;
65
+
66
+ this.rest = props.rest;
67
+ this.arrays = props.arrays;
68
+ this.tuples = props.tuples;
69
+ this.objects = props.objects;
70
+
71
+ this.natives = props.natives;
72
+ this.sets = props.sets;
73
+ this.maps = props.maps;
74
+ }
75
+
76
+ /**
77
+ * @internal
78
+ */
79
+ public static create(props: ClassProperties<Metadata>): Metadata {
80
+ return new Metadata(props);
81
+ }
82
+
83
+ /**
84
+ * @internal
85
+ */
86
+ public static initialize(parentResolved: boolean = false): Metadata {
87
+ const meta: Metadata = this.create({
88
+ any: false,
89
+ nullable: false,
90
+ required: true,
91
+ optional: false,
92
+ functional: false,
93
+
94
+ resolved: null,
95
+ constants: [],
96
+ atomics: [],
97
+ templates: [],
98
+ arrays: [],
99
+ tuples: [],
100
+ objects: [],
101
+
102
+ rest: null,
103
+ natives: [],
104
+ sets: [],
105
+ maps: [],
106
+ });
107
+ meta.parent_resolved_ = parentResolved;
108
+ return meta;
109
+ }
110
+
111
+ public toJSON(): IMetadata {
112
+ return {
113
+ any: this.any,
114
+ required: this.required,
115
+ optional: this.optional,
116
+ nullable: this.nullable,
117
+ functional: this.functional,
118
+
119
+ atomics: this.atomics.slice(),
120
+ constants: JSON.parse(JSON.stringify(this.constants)),
121
+ templates: this.templates.map((tpl) =>
122
+ tpl.map((meta) => meta.toJSON()),
123
+ ),
124
+ resolved: this.resolved ? this.resolved.toJSON() : null,
125
+
126
+ rest: this.rest ? this.rest.toJSON() : null,
127
+ arrays: this.arrays.map((meta) => meta.toJSON()),
128
+ tuples: this.tuples.map((meta) =>
129
+ meta.map((meta) => meta.toJSON()),
130
+ ),
131
+ objects: this.objects.map((obj) => obj.name),
132
+
133
+ natives: this.natives.slice(),
134
+ sets: this.sets.map((meta) => meta.toJSON()),
135
+ maps: this.maps.map((entry) => ({
136
+ key: entry.key.toJSON(),
137
+ value: entry.value.toJSON(),
138
+ })),
139
+ };
140
+ }
141
+
142
+ public static from(meta: IMetadata, objects: IMetadataObject[]): Metadata {
143
+ const dict: Map<string, MetadataObject> = new Map();
144
+ for (const obj of objects)
145
+ dict.set(obj.name, MetadataObject._From_without_properties(obj));
146
+
147
+ for (const obj of objects) {
148
+ const initialized = dict.get(obj.name)!;
149
+ initialized.properties.push(
150
+ ...obj.properties.map((prop) =>
151
+ MetadataProperty._From(prop, dict),
152
+ ),
153
+ );
154
+ }
155
+ return this._From(meta, dict);
156
+ }
157
+
158
+ /**
159
+ * @internal
160
+ */
161
+ public static _From(
162
+ meta: IMetadata,
163
+ objects: Map<string, MetadataObject>,
164
+ ): Metadata {
165
+ return this.create({
166
+ any: meta.any,
167
+ required: meta.required,
168
+ optional: meta.optional,
169
+ nullable: meta.nullable,
170
+ functional: meta.functional,
171
+
172
+ constants: JSON.parse(JSON.stringify(meta.constants)),
173
+ atomics: meta.atomics.slice(),
174
+ templates: meta.templates.map((tpl) =>
175
+ tpl.map((meta) => this._From(meta, objects)),
176
+ ),
177
+ resolved: meta.resolved ? this._From(meta.resolved, objects) : null,
178
+
179
+ rest: meta.rest ? this._From(meta.rest, objects) : null,
180
+ arrays: meta.arrays.map((meta) => this._From(meta, objects)),
181
+ tuples: meta.tuples.map((tuple) =>
182
+ tuple.map((meta) => this._From(meta, objects)),
183
+ ),
184
+ objects: meta.objects.map((name) => {
185
+ const found = objects.get(name);
186
+ if (found === undefined)
187
+ throw new Error(
188
+ `Error on Metadata.from(): failed to find object "${name}".`,
189
+ );
190
+ return found;
191
+ }),
192
+
193
+ natives: meta.natives.slice(),
194
+ sets: meta.sets.map((meta) => this._From(meta, objects)),
195
+ maps: meta.maps.map((entry) => ({
196
+ key: this._From(entry.key, objects),
197
+ value: this._From(entry.value, objects),
198
+ })),
199
+ });
200
+ }
201
+
202
+ /* -----------------------------------------------------------
203
+ ACCESSORS
204
+ ----------------------------------------------------------- */
205
+ public getName(): string {
206
+ this.name_ ||= getName(this);
207
+ return this.name_;
208
+ }
209
+
210
+ public empty(): boolean {
211
+ return this.bucket() === 0 || this.size() === 0;
212
+ }
213
+ public size(): number {
214
+ return (
215
+ (this.resolved ? 1 : 0) +
216
+ (this.functional ? 1 : 0) +
217
+ this.templates.length +
218
+ this.atomics.length +
219
+ this.constants
220
+ .map((c) => c.values.length)
221
+ .reduce((x, y) => x + y, 0) +
222
+ (this.rest ? this.rest.size() : 0) +
223
+ this.arrays.length +
224
+ this.tuples.length +
225
+ this.objects.length +
226
+ this.natives.length +
227
+ this.sets.length +
228
+ this.maps.length
229
+ );
230
+ }
231
+ public bucket(): number {
232
+ return (
233
+ (this.resolved ? 1 : 0) +
234
+ (this.functional ? 1 : 0) +
235
+ (this.templates.length ? 1 : 0) +
236
+ (this.atomics.length ? 1 : 0) +
237
+ (this.constants.length ? 1 : 0) +
238
+ (this.rest ? this.rest.size() : 0) +
239
+ (this.arrays.length ? 1 : 0) +
240
+ (this.tuples.length ? 1 : 0) +
241
+ (this.objects.length ? 1 : 0) +
242
+ (this.natives.length ? 1 : 0) +
243
+ (this.sets.length ? 1 : 0) +
244
+ (this.maps.length ? 1 : 0)
245
+ );
246
+ }
247
+ public isConstant(): boolean {
248
+ return this.bucket() === (this.constants.length ? 1 : 0);
249
+ }
250
+
251
+ /**
252
+ * @internal
253
+ */
254
+ public isUnionBucket(): boolean {
255
+ const size: number = this.bucket();
256
+ const emended: number = this.constants.length ? size - 1 : size;
257
+ return emended > 1;
258
+ }
259
+
260
+ /**
261
+ * @internal
262
+ */
263
+ public getSoleLiteral(): string | null {
264
+ if (
265
+ this.size() === 1 &&
266
+ this.constants.length === 1 &&
267
+ this.constants[0]!.type === "string" &&
268
+ this.constants[0]!.values.length === 1
269
+ )
270
+ return this.constants[0]!.values[0] as string;
271
+ else return null;
272
+ }
273
+
274
+ /**
275
+ * @internal
276
+ */
277
+ public isSoleLiteral(): boolean {
278
+ return this.getSoleLiteral() !== null;
279
+ }
280
+
281
+ /**
282
+ * @internal
283
+ */
284
+ public isParentResolved(): boolean {
285
+ return this.parent_resolved_;
286
+ }
287
+ }
288
+ export namespace Metadata {
289
+ export const intersects = (
290
+ x: Metadata,
291
+ y: Metadata,
292
+ deep: boolean,
293
+ ): boolean => {
294
+ // CHECK ANY & OPTIONAL
295
+ if (x.any || y.any) return true;
296
+ if (x.required === false && false === y.required) return true;
297
+ if (x.nullable === true && true === y.nullable) return true;
298
+
299
+ //----
300
+ // INSTANCES
301
+ //----
302
+ // ARRAYS AND OBJECTS
303
+ if (deep === true) {
304
+ for (const xa of x.arrays)
305
+ for (const ya of y.arrays)
306
+ if (intersects(xa, ya, deep)) {
307
+ return true;
308
+ }
309
+ for (const xo of x.objects)
310
+ for (const yo of y.objects)
311
+ if (MetadataObject.intersects(xo, yo)) {
312
+ return true;
313
+ }
314
+ } else {
315
+ if (x.arrays.length && y.arrays.length) return true;
316
+ if (x.objects.length && y.objects.length) return true;
317
+ }
318
+
319
+ // TUPLES
320
+ for (const xt of x.tuples)
321
+ for (const yt of y.tuples)
322
+ if (xt.length === 0 || yt.length === 0)
323
+ return xt.length === 0 && yt.length === 0;
324
+ else if (
325
+ xt
326
+ .slice(0, Math.min(xt.length, yt.length))
327
+ .some((xv, i) => intersects(xv, yt[i]!, deep))
328
+ )
329
+ return true;
330
+
331
+ //----
332
+ // VALUES
333
+ //----
334
+ // ATOMICS
335
+ for (const atomic of x.atomics)
336
+ if (y.atomics.includes(atomic)) return true;
337
+
338
+ // CONSTANTS
339
+ for (const constant of x.constants) {
340
+ const opposite: MetadataConstant | undefined = y.constants.find(
341
+ (elem) => elem.type === constant.type,
342
+ );
343
+ if (opposite === undefined) continue;
344
+
345
+ const values: Set<any> = new Set([
346
+ ...constant.values,
347
+ ...opposite.values,
348
+ ]);
349
+ if (values.size !== constant.values.length + opposite.values.length)
350
+ return true;
351
+ }
352
+
353
+ // FUNCTIONAL
354
+ if (x.functional === true && y.functional === true) return true;
355
+
356
+ return false;
357
+ };
358
+
359
+ export const covers = (x: Metadata, y: Metadata): boolean => {
360
+ // CHECK ANY
361
+ if (x.any) return true;
362
+ else if (y.any) return false;
363
+
364
+ //----
365
+ // INSTANCES
366
+ //----
367
+ // ARRAYS
368
+ for (const ya of y.arrays)
369
+ if (x.arrays.some((xa) => covers(xa, ya) === true) === false)
370
+ return false;
371
+
372
+ // OBJECTS
373
+ for (const yo of y.objects)
374
+ if (x.objects.some((xo) => MetadataObject.covers(xo, yo)) === false)
375
+ return false;
376
+
377
+ // TUPLES
378
+ for (const yt of y.tuples)
379
+ if (
380
+ yt.length !== 0 &&
381
+ x.tuples.some(
382
+ (xt) =>
383
+ xt.length >= yt.length &&
384
+ xt
385
+ .slice(yt.length)
386
+ .every((xv, i) => covers(xv, yt[i]!)),
387
+ ) === false
388
+ )
389
+ return false;
390
+
391
+ // NATIVES
392
+
393
+ // SETS
394
+ for (const ys of y.sets)
395
+ if (x.sets.some((xs) => covers(xs, ys)) === false) return false;
396
+
397
+ //----
398
+ // VALUES
399
+ //----
400
+ // ATOMICS
401
+ if (y.atomics.some((atomic) => x.atomics.includes(atomic) === false))
402
+ return false;
403
+
404
+ // CONSTANTS
405
+ for (const yc of y.constants) {
406
+ const xc: MetadataConstant | undefined = x.constants.find(
407
+ (elem) => elem.type === yc.type,
408
+ );
409
+ if (xc === undefined) return false;
410
+ else if (
411
+ (yc.values as number[]).some(
412
+ (yv) => xc.values.includes(yv as never) === false,
413
+ )
414
+ )
415
+ return false;
416
+ }
417
+
418
+ // FUNCTIONAL
419
+ if (x.functional === false && y.functional) return false;
420
+
421
+ // SUCCESS
422
+ return true;
423
+ };
424
+
425
+ export const merge = (x: Metadata, y: Metadata): Metadata => {
426
+ const output: Metadata = Metadata.create({
427
+ any: x.any || y.any,
428
+ nullable: x.nullable || y.nullable,
429
+ required: x.required && y.required,
430
+ optional: x.optional || y.optional,
431
+ functional: x.functional || y.functional,
432
+
433
+ resolved:
434
+ x.resolved !== null && y.resolved !== null
435
+ ? merge(x.resolved, y.resolved)
436
+ : x.resolved || y.resolved,
437
+ atomics: [...new Set([...x.atomics, ...y.atomics])],
438
+ constants: [...x.constants],
439
+ templates: x.templates.slice(),
440
+
441
+ rest:
442
+ x.rest !== null && y.rest !== null
443
+ ? merge(x.rest, y.rest)
444
+ : x.rest ?? y.rest,
445
+ arrays: x.arrays.slice(),
446
+ tuples: x.tuples.slice(),
447
+ objects: x.objects.slice(),
448
+
449
+ natives: [...new Set([...x.natives, ...y.natives])],
450
+ sets: x.sets.slice(),
451
+ maps: x.maps.slice(),
452
+ });
453
+ for (const constant of y.constants) {
454
+ const target: MetadataConstant = ArrayUtil.take(
455
+ output.constants,
456
+ (elem) => elem.type === constant.type,
457
+ () => ({
458
+ type: constant.type,
459
+ values: [],
460
+ }),
461
+ );
462
+ for (const value of constant.values)
463
+ ArrayUtil.add(target.values, value);
464
+ }
465
+ for (const array of y.arrays)
466
+ ArrayUtil.set(output.arrays, array, (elem) => elem.getName());
467
+ for (const obj of y.objects)
468
+ ArrayUtil.set(output.objects, obj, (elem) => elem.name);
469
+
470
+ if (x.rest !== null)
471
+ ArrayUtil.set(output.arrays, x.rest, (elem) => elem.getName());
472
+ if (y.rest !== null)
473
+ ArrayUtil.set(output.arrays, y.rest, (elem) => elem.getName());
474
+
475
+ return output;
476
+ };
477
+ }
478
+
479
+ const getName = (metadata: Metadata): string => {
480
+ if (metadata.any === true) return "any";
481
+
482
+ const elements: string[] = [];
483
+
484
+ // OPTIONAL
485
+ if (metadata.nullable === true) elements.push("null");
486
+ if (metadata.required === false) elements.push("undefined");
487
+
488
+ // ATOMIC
489
+ for (const type of metadata.atomics) {
490
+ elements.push(type);
491
+ }
492
+ for (const constant of metadata.constants)
493
+ for (const value of constant.values)
494
+ elements.push(JSON.stringify(value));
495
+ for (const template of metadata.templates)
496
+ elements.push(
497
+ "`" +
498
+ template
499
+ .map((child) =>
500
+ child.isConstant() && child.size() === 1
501
+ ? child.constants[0]!.values[0]!
502
+ : `$\{${child.getName()}\}`,
503
+ )
504
+ .join("")
505
+ .split("`")
506
+ .join("\\`") +
507
+ "`",
508
+ );
509
+
510
+ // NATIVES
511
+ for (const native of metadata.natives) elements.push(native);
512
+ for (const set of metadata.sets) elements.push(`Set<${set.getName()}>`);
513
+ for (const map of metadata.maps)
514
+ elements.push(`Map<${map.key.getName()}, ${map.value.getName()}>`);
515
+
516
+ // ARRAY
517
+ if (metadata.rest !== null) elements.push(`...${metadata.rest.getName()}`);
518
+ for (const tuple of metadata.tuples)
519
+ elements.push(`[${tuple.map((elem) => elem.getName()).join(", ")}]`);
520
+ for (const array of metadata.arrays)
521
+ elements.push(`Array<${array.getName()}>`);
522
+
523
+ // OBJECT
524
+ for (const object of metadata.objects) elements.push(object.name);
525
+ if (metadata.resolved !== null) elements.push(metadata.resolved.getName());
526
+
527
+ // RETURNS
528
+ if (elements.length === 0) return "unknown";
529
+ else if (elements.length === 1) return elements[0]!;
530
+
531
+ elements.sort();
532
+ return `(${elements.join(" | ")})`;
533
+ };
534
+ export namespace Metadata {
535
+ export interface Entry {
536
+ key: Metadata;
537
+ value: Metadata;
538
+ }
539
+ }