typia 3.6.3 → 3.6.4

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 (138) hide show
  1. package/lib/functional/$is_uuid.js +1 -1
  2. package/lib/functional/$is_uuid.js.map +1 -1
  3. package/package.json +1 -1
  4. package/src/IRandomGenerator.ts +16 -16
  5. package/src/IValidation.ts +21 -21
  6. package/src/Primitive.ts +104 -104
  7. package/src/TypeGuardError.ts +36 -36
  8. package/src/executable/TypiaGenerateWizard.ts +87 -87
  9. package/src/executable/TypiaSetupWizard.ts +142 -142
  10. package/src/executable/setup/CommandExecutor.ts +8 -8
  11. package/src/executable/typia.ts +38 -38
  12. package/src/factories/CommentFactory.ts +10 -10
  13. package/src/factories/ExpressionFactory.ts +77 -77
  14. package/src/factories/IdentifierFactory.ts +73 -73
  15. package/src/factories/LiteralFactory.ts +44 -44
  16. package/src/factories/MetadataCollection.ts +122 -122
  17. package/src/factories/MetadataFactory.ts +51 -51
  18. package/src/factories/MetadataTagFactory.ts +265 -265
  19. package/src/factories/StatementFactory.ts +60 -60
  20. package/src/factories/TemplateFactory.ts +56 -56
  21. package/src/factories/TypeFactory.ts +129 -129
  22. package/src/factories/TypiaFileFactory.ts +117 -117
  23. package/src/factories/ValueFactory.ts +12 -12
  24. package/src/factories/internal/metadata/MetadataHelper.ts +12 -12
  25. package/src/factories/internal/metadata/explore_metadata.ts +92 -92
  26. package/src/factories/internal/metadata/iterate_metadata.ts +80 -80
  27. package/src/factories/internal/metadata/iterate_metadata_array.ts +29 -29
  28. package/src/factories/internal/metadata/iterate_metadata_atomic.ts +59 -59
  29. package/src/factories/internal/metadata/iterate_metadata_coalesce.ts +33 -33
  30. package/src/factories/internal/metadata/iterate_metadata_constant.ts +58 -58
  31. package/src/factories/internal/metadata/iterate_metadata_map.ts +41 -41
  32. package/src/factories/internal/metadata/iterate_metadata_native.ts +222 -222
  33. package/src/factories/internal/metadata/iterate_metadata_object.ts +48 -48
  34. package/src/factories/internal/metadata/iterate_metadata_resolve.ts +27 -27
  35. package/src/factories/internal/metadata/iterate_metadata_set.ts +33 -33
  36. package/src/factories/internal/metadata/iterate_metadata_template.ts +38 -38
  37. package/src/factories/internal/metadata/iterate_metadata_tuple.ts +45 -45
  38. package/src/factories/internal/metadata/iterate_metadata_union.ts +59 -59
  39. package/src/functional/$any.ts +3 -3
  40. package/src/functional/$every.ts +11 -11
  41. package/src/functional/$guard.ts +35 -35
  42. package/src/functional/$is_between.ts +7 -7
  43. package/src/functional/$is_email.ts +5 -5
  44. package/src/functional/$is_ipv4.ts +5 -5
  45. package/src/functional/$is_ipv6.ts +5 -5
  46. package/src/functional/$is_url.ts +5 -5
  47. package/src/functional/$is_uuid.ts +4 -5
  48. package/src/functional/$join.ts +50 -50
  49. package/src/functional/$number.ts +12 -12
  50. package/src/functional/$report.ts +15 -15
  51. package/src/functional/$rest.ts +3 -3
  52. package/src/functional/$string.ts +37 -37
  53. package/src/functional/$tail.ts +6 -6
  54. package/src/functional/Namespace.ts +121 -121
  55. package/src/index.ts +4 -4
  56. package/src/metadata/IJsDocTagInfo.ts +10 -10
  57. package/src/metadata/IMetadata.ts +25 -25
  58. package/src/metadata/IMetadataApplication.ts +7 -7
  59. package/src/metadata/IMetadataConstant.ts +16 -16
  60. package/src/metadata/IMetadataEntry.ts +6 -6
  61. package/src/metadata/IMetadataObject.ts +29 -29
  62. package/src/metadata/IMetadataProperty.ts +11 -11
  63. package/src/metadata/IMetadataTag.ts +113 -113
  64. package/src/metadata/Metadata.ts +534 -534
  65. package/src/metadata/MetadataConstant.ts +3 -3
  66. package/src/metadata/MetadataObject.ts +131 -131
  67. package/src/metadata/MetadataProperty.ts +64 -64
  68. package/src/programmers/ApplicationProgrammer.ts +55 -55
  69. package/src/programmers/IsProgrammer.ts +200 -200
  70. package/src/programmers/helpers/AtomicPredicator.ts +31 -31
  71. package/src/programmers/helpers/CloneJoiner.ts +134 -134
  72. package/src/programmers/helpers/FunctionImporeter.ts +55 -55
  73. package/src/programmers/helpers/IExpressionEntry.ts +12 -12
  74. package/src/programmers/helpers/OptionPredicator.ts +19 -19
  75. package/src/programmers/helpers/PruneJoiner.ts +52 -52
  76. package/src/programmers/helpers/RandomJoiner.ts +149 -149
  77. package/src/programmers/helpers/RandomRanger.ts +216 -216
  78. package/src/programmers/helpers/StringifyJoinder.ts +114 -114
  79. package/src/programmers/helpers/StringifyPredicator.ts +18 -18
  80. package/src/programmers/helpers/UnionExplorer.ts +274 -274
  81. package/src/programmers/helpers/UnionPredicator.ts +81 -81
  82. package/src/programmers/internal/application_array.ts +37 -37
  83. package/src/programmers/internal/application_boolean.ts +17 -17
  84. package/src/programmers/internal/application_constant.ts +29 -29
  85. package/src/programmers/internal/application_default.ts +17 -17
  86. package/src/programmers/internal/application_default_string.ts +32 -32
  87. package/src/programmers/internal/application_native.ts +29 -29
  88. package/src/programmers/internal/application_number.ts +70 -70
  89. package/src/programmers/internal/application_object.ts +153 -153
  90. package/src/programmers/internal/application_schema.ts +184 -184
  91. package/src/programmers/internal/application_string.ts +41 -41
  92. package/src/programmers/internal/application_templates.ts +27 -27
  93. package/src/programmers/internal/application_tuple.ts +29 -29
  94. package/src/programmers/internal/check_array.ts +22 -22
  95. package/src/programmers/internal/check_array_length.ts +44 -44
  96. package/src/programmers/internal/check_bigint.ts +64 -64
  97. package/src/programmers/internal/check_dynamic_properties.ts +197 -197
  98. package/src/programmers/internal/check_everything.ts +28 -28
  99. package/src/programmers/internal/check_native.ts +21 -21
  100. package/src/programmers/internal/check_number.ts +145 -145
  101. package/src/programmers/internal/check_object.ts +48 -48
  102. package/src/programmers/internal/check_string.ts +24 -24
  103. package/src/programmers/internal/check_string_tags.ts +63 -63
  104. package/src/programmers/internal/check_template.ts +50 -50
  105. package/src/programmers/internal/check_union_array_like.ts +260 -260
  106. package/src/programmers/internal/check_union_tuple.ts +33 -33
  107. package/src/programmers/internal/decode_union_object.ts +73 -73
  108. package/src/programmers/internal/feature_object_entries.ts +59 -59
  109. package/src/programmers/internal/metadata_to_pattern.ts +31 -31
  110. package/src/programmers/internal/prune_object_properties.ts +60 -60
  111. package/src/programmers/internal/stringify_dynamic_properties.ts +165 -165
  112. package/src/programmers/internal/stringify_native.ts +8 -8
  113. package/src/programmers/internal/stringify_regular_properties.ts +81 -81
  114. package/src/programmers/internal/template_to_pattern.ts +15 -15
  115. package/src/schemas/IJsonApplication.ts +9 -9
  116. package/src/schemas/IJsonComponents.ts +26 -26
  117. package/src/schemas/IJsonSchema.ts +127 -127
  118. package/src/transform.ts +21 -21
  119. package/src/transformers/CallExpressionTransformer.ts +172 -172
  120. package/src/transformers/ExpressionWithArgumentTransformer.ts +66 -66
  121. package/src/transformers/FileTransformer.ts +49 -49
  122. package/src/transformers/IProject.ts +11 -11
  123. package/src/transformers/ITransformOptions.ts +62 -62
  124. package/src/transformers/ImportTransformer.ts +60 -60
  125. package/src/transformers/NodeTransformer.ts +19 -19
  126. package/src/transformers/features/miscellaneous/ApplicationTransformer.ts +120 -120
  127. package/src/transformers/features/miscellaneous/MetadataTransformer.ts +55 -55
  128. package/src/typings/Atomic.ts +17 -17
  129. package/src/typings/ClassProperties.ts +5 -5
  130. package/src/typings/OmitNever.ts +3 -3
  131. package/src/typings/SpecialFields.ts +3 -3
  132. package/src/typings/Writable.ts +11 -11
  133. package/src/utils/ArrayUtil.ts +49 -49
  134. package/src/utils/Escaper.ts +50 -50
  135. package/src/utils/MapUtil.ts +14 -14
  136. package/src/utils/PatternUtil.ts +30 -30
  137. package/src/utils/RandomGenerator.ts +90 -90
  138. package/src/utils/Singleton.ts +17 -17
@@ -1,534 +1,534 @@
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 function 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 function 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 function 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
- function 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)
519
- elements.push(`Resolve<${object.name}>`);
520
- if (metadata.resolved !== null) elements.push(metadata.resolved.getName());
521
-
522
- // RETURNS
523
- if (elements.length === 0) return "unknown";
524
- else if (elements.length === 1) return elements[0]!;
525
-
526
- elements.sort();
527
- return `(${elements.join(" | ")})`;
528
- }
529
- export namespace Metadata {
530
- export interface Entry {
531
- key: Metadata;
532
- value: Metadata;
533
- }
534
- }
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 function 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 function 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 function 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
+ function 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)
519
+ elements.push(`Resolve<${object.name}>`);
520
+ if (metadata.resolved !== null) elements.push(metadata.resolved.getName());
521
+
522
+ // RETURNS
523
+ if (elements.length === 0) return "unknown";
524
+ else if (elements.length === 1) return elements[0]!;
525
+
526
+ elements.sort();
527
+ return `(${elements.join(" | ")})`;
528
+ }
529
+ export namespace Metadata {
530
+ export interface Entry {
531
+ key: Metadata;
532
+ value: Metadata;
533
+ }
534
+ }