typia 4.0.0 → 4.0.1-dev.20230604

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/lib/programmers/ApplicationProgrammer.js +1 -1
  2. package/lib/programmers/internal/application_array.js +19 -41
  3. package/lib/programmers/internal/application_array.js.map +1 -1
  4. package/lib/programmers/internal/application_definition.d.ts +1 -1
  5. package/lib/programmers/internal/application_definition.js +8 -8
  6. package/lib/programmers/internal/application_definition.js.map +1 -1
  7. package/lib/programmers/internal/application_native.js +3 -3
  8. package/lib/programmers/internal/application_object.js +4 -4
  9. package/lib/programmers/internal/application_schema.js +3 -3
  10. package/lib/programmers/internal/application_schema.js.map +1 -1
  11. package/lib/programmers/internal/application_tuple.js +8 -30
  12. package/lib/programmers/internal/application_tuple.js.map +1 -1
  13. package/lib/schemas/IJsonComponents.d.ts +1 -12
  14. package/lib/schemas/IJsonSchema.d.ts +1 -1
  15. package/package.json +1 -1
  16. package/src/IRandomGenerator.ts +34 -34
  17. package/src/factories/IdentifierFactory.ts +65 -65
  18. package/src/factories/MetadataCollection.ts +254 -254
  19. package/src/factories/MetadataFactory.ts +40 -40
  20. package/src/factories/MetadataTagFactory.ts +300 -300
  21. package/src/factories/internal/metadata/emplace_metadata_array.ts +34 -34
  22. package/src/factories/internal/metadata/emplace_metadata_definition.ts +35 -35
  23. package/src/factories/internal/metadata/emplace_metadata_object.ts +142 -142
  24. package/src/factories/internal/metadata/emplace_metadata_tuple.ts +50 -50
  25. package/src/factories/internal/metadata/explore_metadata.ts +66 -66
  26. package/src/factories/internal/metadata/iterate_metadata.ts +77 -77
  27. package/src/factories/internal/metadata/iterate_metadata_array.ts +25 -25
  28. package/src/factories/internal/metadata/iterate_metadata_collection.ts +130 -130
  29. package/src/factories/internal/metadata/iterate_metadata_definition.ts +30 -30
  30. package/src/factories/internal/metadata/iterate_metadata_object.ts +49 -49
  31. package/src/factories/internal/metadata/iterate_metadata_sort.ts +68 -68
  32. package/src/factories/internal/metadata/iterate_metadata_tuple.ts +24 -24
  33. package/src/factories/internal/metadata/iterate_metadata_union.ts +68 -68
  34. package/src/metadata/IMetadata.ts +27 -27
  35. package/src/metadata/IMetadataAlias.ts +12 -12
  36. package/src/metadata/IMetadataApplication.ts +7 -7
  37. package/src/metadata/IMetadataArray.ts +10 -10
  38. package/src/metadata/IMetadataCollection.ts +11 -11
  39. package/src/metadata/IMetadataDictionary.ts +14 -14
  40. package/src/metadata/IMetadataObject.ts +18 -18
  41. package/src/metadata/IMetadataProperty.ts +11 -11
  42. package/src/metadata/IMetadataTuple.ts +10 -10
  43. package/src/metadata/Metadata.ts +585 -585
  44. package/src/metadata/MetadataAlias.ts +61 -61
  45. package/src/metadata/MetadataArray.ts +52 -52
  46. package/src/metadata/MetadataObject.ts +114 -114
  47. package/src/metadata/MetadataProperty.ts +64 -64
  48. package/src/metadata/MetadataTuple.ts +53 -53
  49. package/src/programmers/ApplicationProgrammer.ts +55 -55
  50. package/src/programmers/AssertProgrammer.ts +291 -291
  51. package/src/programmers/CheckerProgrammer.ts +1182 -1182
  52. package/src/programmers/CloneProgrammer.ts +595 -595
  53. package/src/programmers/FeatureProgrammer.ts +495 -495
  54. package/src/programmers/IsProgrammer.ts +250 -250
  55. package/src/programmers/LiteralsProgrammer.ts +66 -66
  56. package/src/programmers/PruneProgrammer.ts +550 -550
  57. package/src/programmers/RandomProgrammer.ts +589 -589
  58. package/src/programmers/StringifyProgrammer.ts +990 -990
  59. package/src/programmers/ValidateProgrammer.ts +313 -313
  60. package/src/programmers/helpers/FunctionImporeter.ts +78 -78
  61. package/src/programmers/helpers/RandomJoiner.ts +173 -173
  62. package/src/programmers/helpers/UnionExplorer.ts +301 -301
  63. package/src/programmers/helpers/UnionPredicator.ts +81 -81
  64. package/src/programmers/helpers/disable_function_importer_declare.ts +26 -26
  65. package/src/programmers/internal/JSON_SCHEMA_PREFIX.ts +1 -1
  66. package/src/programmers/internal/application_array.ts +30 -64
  67. package/src/programmers/internal/application_boolean.ts +15 -15
  68. package/src/programmers/internal/application_constant.ts +26 -26
  69. package/src/programmers/internal/application_default.ts +17 -17
  70. package/src/programmers/internal/application_definition.ts +45 -45
  71. package/src/programmers/internal/application_native.ts +39 -39
  72. package/src/programmers/internal/application_number.ts +74 -74
  73. package/src/programmers/internal/application_object.ts +167 -167
  74. package/src/programmers/internal/application_schema.ts +156 -164
  75. package/src/programmers/internal/application_tuple.ts +47 -77
  76. package/src/programmers/internal/check_union_array_like.ts +329 -329
  77. package/src/programmers/internal/feature_object_entries.ts +63 -63
  78. package/src/programmers/internal/wrap_metadata_rest_tuple.ts +16 -16
  79. package/src/schemas/IJsonApplication.ts +8 -8
  80. package/src/schemas/IJsonComponents.ts +34 -45
  81. package/src/schemas/IJsonSchema.ts +134 -134
  82. package/src/transformers/CallExpressionTransformer.ts +179 -179
  83. package/src/transformers/FileTransformer.ts +47 -47
  84. package/src/transformers/features/miscellaneous/ApplicationTransformer.ts +104 -104
  85. package/src/transformers/features/miscellaneous/MetadataTransformer.ts +53 -53
  86. package/src/utils/ArrayUtil.ts +45 -45
  87. package/src/utils/RandomGenerator.ts +81 -81
@@ -1,585 +1,585 @@
1
- import { Atomic } from "../typings/Atomic";
2
- import { ClassProperties } from "../typings/ClassProperties";
3
- import { Writable } from "../typings/Writable";
4
-
5
- import { ArrayUtil } from "../utils/ArrayUtil";
6
-
7
- import { IMetadata } from "./IMetadata";
8
- import { IMetadataCollection } from "./IMetadataCollection";
9
- import { IMetadataDictionary } from "./IMetadataDictionary";
10
- import { MetadataAlias } from "./MetadataAlias";
11
- import { MetadataArray } from "./MetadataArray";
12
- import { MetadataConstant } from "./MetadataConstant";
13
- import { MetadataObject } from "./MetadataObject";
14
- import { MetadataProperty } from "./MetadataProperty";
15
- import { MetadataTuple } from "./MetadataTuple";
16
-
17
- export class Metadata {
18
- public any: boolean;
19
- public required: boolean;
20
- public optional: boolean;
21
- public nullable: boolean;
22
- public functional: boolean;
23
-
24
- public resolved: Metadata | null;
25
- public atomics: Atomic.Literal[];
26
- public constants: MetadataConstant[];
27
- public templates: Metadata[][];
28
-
29
- public rest: Metadata | null;
30
- public aliases: MetadataAlias[];
31
- public arrays: MetadataArray[];
32
- public tuples: MetadataTuple[];
33
- public objects: MetadataObject[];
34
-
35
- public natives: string[];
36
- public sets: Metadata[];
37
- public maps: Metadata.Entry[];
38
-
39
- /** @internal */ private name_?: string;
40
- /** @internal */ private parent_resolved_: boolean = false;
41
- /** @internal */ public union_index?: number;
42
-
43
- /* -----------------------------------------------------------
44
- CONSTRUCTORS
45
- ----------------------------------------------------------- */
46
- /**
47
- * @hidden
48
- */
49
- private constructor(props: ClassProperties<Metadata>) {
50
- this.any = props.any;
51
- this.required = props.required;
52
- this.optional = props.optional;
53
- this.nullable = props.nullable;
54
- this.functional = props.functional;
55
-
56
- this.resolved = props.resolved;
57
- this.atomics = props.atomics;
58
- this.constants = props.constants;
59
- this.templates = props.templates;
60
-
61
- this.rest = props.rest;
62
- this.arrays = props.arrays;
63
- this.tuples = props.tuples;
64
- this.objects = props.objects;
65
- this.aliases = props.aliases;
66
-
67
- this.natives = props.natives;
68
- this.sets = props.sets;
69
- this.maps = props.maps;
70
- }
71
-
72
- /**
73
- * @internal
74
- */
75
- public static create(props: ClassProperties<Metadata>): Metadata {
76
- return new Metadata(props);
77
- }
78
-
79
- /**
80
- * @internal
81
- */
82
- public static initialize(parentResolved: boolean = false): Metadata {
83
- const meta: Metadata = this.create({
84
- any: false,
85
- nullable: false,
86
- required: true,
87
- optional: false,
88
- functional: false,
89
-
90
- resolved: null,
91
- constants: [],
92
- atomics: [],
93
- templates: [],
94
- arrays: [],
95
- tuples: [],
96
- objects: [],
97
- aliases: [],
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
- optional: this.optional,
113
- nullable: this.nullable,
114
- functional: this.functional,
115
-
116
- atomics: this.atomics.slice(),
117
- constants: JSON.parse(JSON.stringify(this.constants)),
118
- templates: this.templates.map((tpl) =>
119
- tpl.map((meta) => meta.toJSON()),
120
- ),
121
- resolved: this.resolved ? this.resolved.toJSON() : null,
122
-
123
- rest: this.rest ? this.rest.toJSON() : null,
124
- arrays: this.arrays.map((array) => array.name),
125
- tuples: this.tuples.map((tuple) => tuple.name),
126
- objects: this.objects.map((obj) => obj.name),
127
- aliases: this.aliases.map((alias) => alias.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(
139
- meta: IMetadata,
140
- collection: IMetadataCollection,
141
- ): Metadata {
142
- const dict: IMetadataDictionary = {
143
- objects: new Map(
144
- collection.objects.map((obj) => [
145
- obj.name,
146
- MetadataObject._From_without_properties(obj),
147
- ]),
148
- ),
149
- aliases: new Map(
150
- collection.aliases.map((alias) => [
151
- alias.name,
152
- MetadataAlias._From_without_value(alias),
153
- ]),
154
- ),
155
- arrays: new Map(
156
- collection.arrays.map((arr) => [
157
- arr.name,
158
- MetadataArray._From_without_value(arr),
159
- ]),
160
- ),
161
- tuples: new Map(
162
- collection.tuples.map((tpl) => [
163
- tpl.name,
164
- MetadataTuple._From_without_elements(tpl),
165
- ]),
166
- ),
167
- };
168
-
169
- for (const obj of collection.objects) {
170
- const initialized = dict.objects.get(obj.name)!;
171
- initialized.properties.push(
172
- ...obj.properties.map((prop) =>
173
- MetadataProperty._From(prop, dict),
174
- ),
175
- );
176
- }
177
- for (const alias of collection.aliases)
178
- Writable(dict.aliases.get(alias.name)!).value = this._From(
179
- alias.value,
180
- dict,
181
- );
182
- for (const array of collection.arrays)
183
- Writable(dict.arrays.get(array.name)!).value = this._From(
184
- array.value,
185
- dict,
186
- );
187
- for (const tuple of collection.tuples)
188
- Writable(dict.tuples.get(tuple.name)!).elements =
189
- tuple.elements.map((elem) => this._From(elem, dict));
190
-
191
- return this._From(meta, dict);
192
- }
193
-
194
- /**
195
- * @internal
196
- */
197
- public static _From(meta: IMetadata, dict: IMetadataDictionary): Metadata {
198
- return this.create({
199
- any: meta.any,
200
- required: meta.required,
201
- optional: meta.optional,
202
- nullable: meta.nullable,
203
- functional: meta.functional,
204
-
205
- constants: JSON.parse(JSON.stringify(meta.constants)),
206
- atomics: meta.atomics.slice(),
207
- templates: meta.templates.map((tpl) =>
208
- tpl.map((meta) => this._From(meta, dict)),
209
- ),
210
- resolved: meta.resolved ? this._From(meta.resolved, dict) : null,
211
-
212
- rest: meta.rest ? this._From(meta.rest, dict) : null,
213
- arrays: meta.arrays.map((id) => {
214
- const array = dict.arrays.get(id);
215
- if (array === undefined)
216
- throw new Error(
217
- `Error on Metadata.from(): failed to find array "${id}".`,
218
- );
219
- return array;
220
- }),
221
- tuples: meta.tuples.map((id) => {
222
- const tuple = dict.tuples.get(id);
223
- if (tuple === undefined)
224
- throw new Error(
225
- `Error on Metadata.from(): failed to find tuple "${id}".`,
226
- );
227
- return tuple;
228
- }),
229
- objects: meta.objects.map((name) => {
230
- const found = dict.objects.get(name);
231
- if (found === undefined)
232
- throw new Error(
233
- `Error on Metadata.from(): failed to find object "${name}".`,
234
- );
235
- return found;
236
- }),
237
- aliases: meta.aliases.map((alias) => {
238
- const found = dict.aliases.get(alias);
239
- if (found === undefined)
240
- throw new Error(
241
- `Error on Metadata.from(): failed to find alias "${alias}".`,
242
- );
243
- return found;
244
- }),
245
-
246
- natives: meta.natives.slice(),
247
- sets: meta.sets.map((meta) => this._From(meta, dict)),
248
- maps: meta.maps.map((entry) => ({
249
- key: this._From(entry.key, dict),
250
- value: this._From(entry.value, dict),
251
- })),
252
- });
253
- }
254
-
255
- /* -----------------------------------------------------------
256
- ACCESSORS
257
- ----------------------------------------------------------- */
258
- public getName(): string {
259
- this.name_ ??= getName(this);
260
- return this.name_;
261
- }
262
-
263
- public empty(): boolean {
264
- return this.bucket() === 0 || this.size() === 0;
265
- }
266
- public size(): number {
267
- return (
268
- (this.resolved ? 1 : 0) +
269
- (this.functional ? 1 : 0) +
270
- (this.rest ? this.rest.size() : 0) +
271
- this.templates.length +
272
- this.atomics.length +
273
- this.constants
274
- .map((c) => c.values.length)
275
- .reduce((x, y) => x + y, 0) +
276
- this.arrays.length +
277
- this.tuples.length +
278
- this.natives.length +
279
- this.maps.length +
280
- this.sets.length +
281
- this.objects.length +
282
- this.aliases.length
283
- );
284
- }
285
- public bucket(): number {
286
- return (
287
- (this.resolved ? 1 : 0) +
288
- (this.functional ? 1 : 0) +
289
- (this.templates.length ? 1 : 0) +
290
- (this.atomics.length ? 1 : 0) +
291
- (this.constants.length ? 1 : 0) +
292
- (this.rest ? this.rest.size() : 0) +
293
- (this.arrays.length ? 1 : 0) +
294
- (this.tuples.length ? 1 : 0) +
295
- (this.natives.length ? 1 : 0) +
296
- (this.sets.length ? 1 : 0) +
297
- (this.maps.length ? 1 : 0) +
298
- (this.objects.length ? 1 : 0) +
299
- (this.aliases.length ? 1 : 0)
300
- );
301
- }
302
- public isConstant(): boolean {
303
- return this.bucket() === (this.constants.length ? 1 : 0);
304
- }
305
-
306
- /**
307
- * @internal
308
- */
309
- public isUnionBucket(): boolean {
310
- const size: number = this.bucket();
311
- const emended: number = this.constants.length ? size - 1 : size;
312
- return emended > 1;
313
- }
314
-
315
- /**
316
- * @internal
317
- */
318
- public getSoleLiteral(): string | null {
319
- if (
320
- this.size() === 1 &&
321
- this.constants.length === 1 &&
322
- this.constants[0]!.type === "string" &&
323
- this.constants[0]!.values.length === 1
324
- )
325
- return this.constants[0]!.values[0] as string;
326
- else return null;
327
- }
328
-
329
- /**
330
- * @internal
331
- */
332
- public isSoleLiteral(): boolean {
333
- return this.getSoleLiteral() !== null;
334
- }
335
-
336
- /**
337
- * @internal
338
- */
339
- public isParentResolved(): boolean {
340
- return this.parent_resolved_;
341
- }
342
- }
343
- export namespace Metadata {
344
- export const intersects = (x: Metadata, y: Metadata): boolean => {
345
- // CHECK ANY & OPTIONAL
346
- if (x.any || y.any) return true;
347
- if (x.required === false && false === y.required) return true;
348
- if (x.nullable === true && true === y.nullable) return true;
349
- if (x.functional === true && y.functional === true) return true;
350
-
351
- //----
352
- // INSTANCES
353
- //----
354
- // ARRAYS
355
- if (x.arrays.length && y.arrays.length) return true;
356
- if (x.tuples.length && y.tuples.length) return true;
357
- if (x.objects.length && y.objects.length) return true;
358
- if (x.aliases.length && y.aliases.length) return true;
359
-
360
- //----
361
- // VALUES
362
- //----
363
- // ATOMICS
364
- for (const atomic of x.atomics)
365
- if (y.atomics.includes(atomic)) return true;
366
-
367
- // CONSTANTS
368
- for (const constant of x.constants) {
369
- const opposite: MetadataConstant | undefined = y.constants.find(
370
- (elem) => elem.type === constant.type,
371
- );
372
- if (opposite === undefined) continue;
373
-
374
- const values: Set<any> = new Set([
375
- ...constant.values,
376
- ...opposite.values,
377
- ]);
378
- if (values.size !== constant.values.length + opposite.values.length)
379
- return true;
380
- }
381
- return false;
382
- };
383
-
384
- export const covers = (
385
- x: Metadata,
386
- y: Metadata,
387
- level: number = 0,
388
- ): boolean => {
389
- // CHECK ANY
390
- if (x === y) return false;
391
- else if (x.any) return true;
392
- else if (y.any) return false;
393
-
394
- //----
395
- // INSTANCES
396
- //----
397
- if (level === 0) {
398
- // ARRAYS
399
- for (const ya of y.arrays)
400
- if (
401
- !x.arrays.some((xa) =>
402
- covers(xa.value, ya.value, level + 1),
403
- )
404
- ) {
405
- return false;
406
- }
407
-
408
- // TUPLES
409
- for (const yt of y.tuples)
410
- if (
411
- yt.elements.length !== 0 &&
412
- x.tuples.some(
413
- (xt) =>
414
- xt.elements.length >= yt.elements.length &&
415
- xt.elements
416
- .slice(yt.elements.length)
417
- .every((xv, i) =>
418
- covers(xv, yt.elements[i]!, level + 1),
419
- ),
420
- ) === false
421
- )
422
- return false;
423
- }
424
-
425
- // OBJECTS
426
- for (const yo of y.objects)
427
- if (x.objects.some((xo) => MetadataObject.covers(xo, yo)) === false)
428
- return false;
429
-
430
- // ALIASES
431
- for (const yd of y.aliases)
432
- if (x.aliases.some((xd) => xd.name === yd.name) === false)
433
- return false;
434
-
435
- // NATIVES
436
- for (const yn of y.natives)
437
- if (x.natives.some((xn) => xn === yn) === false) return false;
438
-
439
- // SETS
440
- for (const ys of y.sets)
441
- if (x.sets.some((xs) => covers(xs, ys)) === false) return false;
442
-
443
- //----
444
- // VALUES
445
- //----
446
- // ATOMICS
447
- if (y.atomics.some((atomic) => x.atomics.includes(atomic) === false))
448
- return false;
449
-
450
- // CONSTANTS
451
- for (const yc of y.constants) {
452
- const xc: MetadataConstant | undefined = x.constants.find(
453
- (elem) => elem.type === yc.type,
454
- );
455
- if (xc === undefined) return false;
456
- else if (
457
- (yc.values as number[]).some(
458
- (yv) => xc.values.includes(yv as never) === false,
459
- )
460
- )
461
- return false;
462
- }
463
-
464
- // FUNCTIONAL
465
- if (x.functional === false && y.functional) return false;
466
-
467
- // SUCCESS
468
- return true;
469
- };
470
-
471
- /**
472
- * @internal
473
- */
474
- export const merge = (x: Metadata, y: Metadata): Metadata => {
475
- const output: Metadata = Metadata.create({
476
- any: x.any || y.any,
477
- nullable: x.nullable || y.nullable,
478
- required: x.required && y.required,
479
- optional: x.optional || y.optional,
480
- functional: x.functional || y.functional,
481
-
482
- resolved:
483
- x.resolved !== null && y.resolved !== null
484
- ? merge(x.resolved, y.resolved)
485
- : x.resolved || y.resolved,
486
- atomics: [...new Set([...x.atomics, ...y.atomics])],
487
- constants: [...x.constants],
488
- templates: x.templates.slice(),
489
-
490
- rest:
491
- x.rest !== null && y.rest !== null
492
- ? merge(x.rest, y.rest)
493
- : x.rest ?? y.rest,
494
- arrays: x.arrays.slice(),
495
- tuples: x.tuples.slice(),
496
- objects: x.objects.slice(),
497
- aliases: x.aliases.slice(),
498
-
499
- natives: [...new Set([...x.natives, ...y.natives])],
500
- sets: x.sets.slice(),
501
- maps: x.maps.slice(),
502
- });
503
- for (const constant of y.constants) {
504
- const target: MetadataConstant = ArrayUtil.take(
505
- output.constants,
506
- (elem) => elem.type === constant.type,
507
- () => ({
508
- type: constant.type,
509
- values: [],
510
- }),
511
- );
512
- for (const value of constant.values)
513
- ArrayUtil.add(target.values, value);
514
- }
515
- for (const array of y.arrays)
516
- ArrayUtil.set(output.arrays, array, (elem) => elem.name);
517
- for (const tuple of y.tuples)
518
- ArrayUtil.set(output.tuples, tuple, (elem) => elem.name);
519
- for (const obj of y.objects)
520
- ArrayUtil.set(output.objects, obj, (elem) => elem.name);
521
- for (const alias of y.aliases)
522
- ArrayUtil.set(output.aliases, alias, (elem) => elem.name);
523
-
524
- return output;
525
- };
526
- }
527
-
528
- const getName = (metadata: Metadata): string => {
529
- if (metadata.any === true) return "any";
530
-
531
- const elements: string[] = [];
532
-
533
- // OPTIONAL
534
- if (metadata.nullable === true) elements.push("null");
535
- if (metadata.required === false) elements.push("undefined");
536
-
537
- // ATOMIC
538
- for (const type of metadata.atomics) {
539
- elements.push(type);
540
- }
541
- for (const constant of metadata.constants)
542
- for (const value of constant.values)
543
- elements.push(JSON.stringify(value));
544
- for (const template of metadata.templates)
545
- elements.push(
546
- "`" +
547
- template
548
- .map((child) =>
549
- child.isConstant() && child.size() === 1
550
- ? child.constants[0]!.values[0]!
551
- : `$\{${child.getName()}\}`,
552
- )
553
- .join("")
554
- .split("`")
555
- .join("\\`") +
556
- "`",
557
- );
558
-
559
- // NATIVES
560
- for (const native of metadata.natives) elements.push(native);
561
- for (const set of metadata.sets) elements.push(`Set<${set.getName()}>`);
562
- for (const map of metadata.maps)
563
- elements.push(`Map<${map.key.getName()}, ${map.value.getName()}>`);
564
-
565
- // INSTANCES
566
- if (metadata.rest !== null) elements.push(`...${metadata.rest.getName()}`);
567
- for (const tuple of metadata.tuples) elements.push(tuple.name);
568
- for (const array of metadata.arrays) elements.push(array.name);
569
- for (const object of metadata.objects) elements.push(object.name);
570
- for (const alias of metadata.aliases) elements.push(alias.name);
571
- if (metadata.resolved !== null) elements.push(metadata.resolved.getName());
572
-
573
- // RETURNS
574
- if (elements.length === 0) return "unknown";
575
- else if (elements.length === 1) return elements[0]!;
576
-
577
- elements.sort();
578
- return `(${elements.join(" | ")})`;
579
- };
580
- export namespace Metadata {
581
- export interface Entry {
582
- key: Metadata;
583
- value: Metadata;
584
- }
585
- }
1
+ import { Atomic } from "../typings/Atomic";
2
+ import { ClassProperties } from "../typings/ClassProperties";
3
+ import { Writable } from "../typings/Writable";
4
+
5
+ import { ArrayUtil } from "../utils/ArrayUtil";
6
+
7
+ import { IMetadata } from "./IMetadata";
8
+ import { IMetadataCollection } from "./IMetadataCollection";
9
+ import { IMetadataDictionary } from "./IMetadataDictionary";
10
+ import { MetadataAlias } from "./MetadataAlias";
11
+ import { MetadataArray } from "./MetadataArray";
12
+ import { MetadataConstant } from "./MetadataConstant";
13
+ import { MetadataObject } from "./MetadataObject";
14
+ import { MetadataProperty } from "./MetadataProperty";
15
+ import { MetadataTuple } from "./MetadataTuple";
16
+
17
+ export class Metadata {
18
+ public any: boolean;
19
+ public required: boolean;
20
+ public optional: boolean;
21
+ public nullable: boolean;
22
+ public functional: boolean;
23
+
24
+ public resolved: Metadata | null;
25
+ public atomics: Atomic.Literal[];
26
+ public constants: MetadataConstant[];
27
+ public templates: Metadata[][];
28
+
29
+ public rest: Metadata | null;
30
+ public aliases: MetadataAlias[];
31
+ public arrays: MetadataArray[];
32
+ public tuples: MetadataTuple[];
33
+ public objects: MetadataObject[];
34
+
35
+ public natives: string[];
36
+ public sets: Metadata[];
37
+ public maps: Metadata.Entry[];
38
+
39
+ /** @internal */ private name_?: string;
40
+ /** @internal */ private parent_resolved_: boolean = false;
41
+ /** @internal */ public union_index?: number;
42
+
43
+ /* -----------------------------------------------------------
44
+ CONSTRUCTORS
45
+ ----------------------------------------------------------- */
46
+ /**
47
+ * @hidden
48
+ */
49
+ private constructor(props: ClassProperties<Metadata>) {
50
+ this.any = props.any;
51
+ this.required = props.required;
52
+ this.optional = props.optional;
53
+ this.nullable = props.nullable;
54
+ this.functional = props.functional;
55
+
56
+ this.resolved = props.resolved;
57
+ this.atomics = props.atomics;
58
+ this.constants = props.constants;
59
+ this.templates = props.templates;
60
+
61
+ this.rest = props.rest;
62
+ this.arrays = props.arrays;
63
+ this.tuples = props.tuples;
64
+ this.objects = props.objects;
65
+ this.aliases = props.aliases;
66
+
67
+ this.natives = props.natives;
68
+ this.sets = props.sets;
69
+ this.maps = props.maps;
70
+ }
71
+
72
+ /**
73
+ * @internal
74
+ */
75
+ public static create(props: ClassProperties<Metadata>): Metadata {
76
+ return new Metadata(props);
77
+ }
78
+
79
+ /**
80
+ * @internal
81
+ */
82
+ public static initialize(parentResolved: boolean = false): Metadata {
83
+ const meta: Metadata = this.create({
84
+ any: false,
85
+ nullable: false,
86
+ required: true,
87
+ optional: false,
88
+ functional: false,
89
+
90
+ resolved: null,
91
+ constants: [],
92
+ atomics: [],
93
+ templates: [],
94
+ arrays: [],
95
+ tuples: [],
96
+ objects: [],
97
+ aliases: [],
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
+ optional: this.optional,
113
+ nullable: this.nullable,
114
+ functional: this.functional,
115
+
116
+ atomics: this.atomics.slice(),
117
+ constants: JSON.parse(JSON.stringify(this.constants)),
118
+ templates: this.templates.map((tpl) =>
119
+ tpl.map((meta) => meta.toJSON()),
120
+ ),
121
+ resolved: this.resolved ? this.resolved.toJSON() : null,
122
+
123
+ rest: this.rest ? this.rest.toJSON() : null,
124
+ arrays: this.arrays.map((array) => array.name),
125
+ tuples: this.tuples.map((tuple) => tuple.name),
126
+ objects: this.objects.map((obj) => obj.name),
127
+ aliases: this.aliases.map((alias) => alias.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(
139
+ meta: IMetadata,
140
+ collection: IMetadataCollection,
141
+ ): Metadata {
142
+ const dict: IMetadataDictionary = {
143
+ objects: new Map(
144
+ collection.objects.map((obj) => [
145
+ obj.name,
146
+ MetadataObject._From_without_properties(obj),
147
+ ]),
148
+ ),
149
+ aliases: new Map(
150
+ collection.aliases.map((alias) => [
151
+ alias.name,
152
+ MetadataAlias._From_without_value(alias),
153
+ ]),
154
+ ),
155
+ arrays: new Map(
156
+ collection.arrays.map((arr) => [
157
+ arr.name,
158
+ MetadataArray._From_without_value(arr),
159
+ ]),
160
+ ),
161
+ tuples: new Map(
162
+ collection.tuples.map((tpl) => [
163
+ tpl.name,
164
+ MetadataTuple._From_without_elements(tpl),
165
+ ]),
166
+ ),
167
+ };
168
+
169
+ for (const obj of collection.objects) {
170
+ const initialized = dict.objects.get(obj.name)!;
171
+ initialized.properties.push(
172
+ ...obj.properties.map((prop) =>
173
+ MetadataProperty._From(prop, dict),
174
+ ),
175
+ );
176
+ }
177
+ for (const alias of collection.aliases)
178
+ Writable(dict.aliases.get(alias.name)!).value = this._From(
179
+ alias.value,
180
+ dict,
181
+ );
182
+ for (const array of collection.arrays)
183
+ Writable(dict.arrays.get(array.name)!).value = this._From(
184
+ array.value,
185
+ dict,
186
+ );
187
+ for (const tuple of collection.tuples)
188
+ Writable(dict.tuples.get(tuple.name)!).elements =
189
+ tuple.elements.map((elem) => this._From(elem, dict));
190
+
191
+ return this._From(meta, dict);
192
+ }
193
+
194
+ /**
195
+ * @internal
196
+ */
197
+ public static _From(meta: IMetadata, dict: IMetadataDictionary): Metadata {
198
+ return this.create({
199
+ any: meta.any,
200
+ required: meta.required,
201
+ optional: meta.optional,
202
+ nullable: meta.nullable,
203
+ functional: meta.functional,
204
+
205
+ constants: JSON.parse(JSON.stringify(meta.constants)),
206
+ atomics: meta.atomics.slice(),
207
+ templates: meta.templates.map((tpl) =>
208
+ tpl.map((meta) => this._From(meta, dict)),
209
+ ),
210
+ resolved: meta.resolved ? this._From(meta.resolved, dict) : null,
211
+
212
+ rest: meta.rest ? this._From(meta.rest, dict) : null,
213
+ arrays: meta.arrays.map((id) => {
214
+ const array = dict.arrays.get(id);
215
+ if (array === undefined)
216
+ throw new Error(
217
+ `Error on Metadata.from(): failed to find array "${id}".`,
218
+ );
219
+ return array;
220
+ }),
221
+ tuples: meta.tuples.map((id) => {
222
+ const tuple = dict.tuples.get(id);
223
+ if (tuple === undefined)
224
+ throw new Error(
225
+ `Error on Metadata.from(): failed to find tuple "${id}".`,
226
+ );
227
+ return tuple;
228
+ }),
229
+ objects: meta.objects.map((name) => {
230
+ const found = dict.objects.get(name);
231
+ if (found === undefined)
232
+ throw new Error(
233
+ `Error on Metadata.from(): failed to find object "${name}".`,
234
+ );
235
+ return found;
236
+ }),
237
+ aliases: meta.aliases.map((alias) => {
238
+ const found = dict.aliases.get(alias);
239
+ if (found === undefined)
240
+ throw new Error(
241
+ `Error on Metadata.from(): failed to find alias "${alias}".`,
242
+ );
243
+ return found;
244
+ }),
245
+
246
+ natives: meta.natives.slice(),
247
+ sets: meta.sets.map((meta) => this._From(meta, dict)),
248
+ maps: meta.maps.map((entry) => ({
249
+ key: this._From(entry.key, dict),
250
+ value: this._From(entry.value, dict),
251
+ })),
252
+ });
253
+ }
254
+
255
+ /* -----------------------------------------------------------
256
+ ACCESSORS
257
+ ----------------------------------------------------------- */
258
+ public getName(): string {
259
+ this.name_ ??= getName(this);
260
+ return this.name_;
261
+ }
262
+
263
+ public empty(): boolean {
264
+ return this.bucket() === 0 || this.size() === 0;
265
+ }
266
+ public size(): number {
267
+ return (
268
+ (this.resolved ? 1 : 0) +
269
+ (this.functional ? 1 : 0) +
270
+ (this.rest ? this.rest.size() : 0) +
271
+ this.templates.length +
272
+ this.atomics.length +
273
+ this.constants
274
+ .map((c) => c.values.length)
275
+ .reduce((x, y) => x + y, 0) +
276
+ this.arrays.length +
277
+ this.tuples.length +
278
+ this.natives.length +
279
+ this.maps.length +
280
+ this.sets.length +
281
+ this.objects.length +
282
+ this.aliases.length
283
+ );
284
+ }
285
+ public bucket(): number {
286
+ return (
287
+ (this.resolved ? 1 : 0) +
288
+ (this.functional ? 1 : 0) +
289
+ (this.templates.length ? 1 : 0) +
290
+ (this.atomics.length ? 1 : 0) +
291
+ (this.constants.length ? 1 : 0) +
292
+ (this.rest ? this.rest.size() : 0) +
293
+ (this.arrays.length ? 1 : 0) +
294
+ (this.tuples.length ? 1 : 0) +
295
+ (this.natives.length ? 1 : 0) +
296
+ (this.sets.length ? 1 : 0) +
297
+ (this.maps.length ? 1 : 0) +
298
+ (this.objects.length ? 1 : 0) +
299
+ (this.aliases.length ? 1 : 0)
300
+ );
301
+ }
302
+ public isConstant(): boolean {
303
+ return this.bucket() === (this.constants.length ? 1 : 0);
304
+ }
305
+
306
+ /**
307
+ * @internal
308
+ */
309
+ public isUnionBucket(): boolean {
310
+ const size: number = this.bucket();
311
+ const emended: number = this.constants.length ? size - 1 : size;
312
+ return emended > 1;
313
+ }
314
+
315
+ /**
316
+ * @internal
317
+ */
318
+ public getSoleLiteral(): string | null {
319
+ if (
320
+ this.size() === 1 &&
321
+ this.constants.length === 1 &&
322
+ this.constants[0]!.type === "string" &&
323
+ this.constants[0]!.values.length === 1
324
+ )
325
+ return this.constants[0]!.values[0] as string;
326
+ else return null;
327
+ }
328
+
329
+ /**
330
+ * @internal
331
+ */
332
+ public isSoleLiteral(): boolean {
333
+ return this.getSoleLiteral() !== null;
334
+ }
335
+
336
+ /**
337
+ * @internal
338
+ */
339
+ public isParentResolved(): boolean {
340
+ return this.parent_resolved_;
341
+ }
342
+ }
343
+ export namespace Metadata {
344
+ export const intersects = (x: Metadata, y: Metadata): boolean => {
345
+ // CHECK ANY & OPTIONAL
346
+ if (x.any || y.any) return true;
347
+ if (x.required === false && false === y.required) return true;
348
+ if (x.nullable === true && true === y.nullable) return true;
349
+ if (x.functional === true && y.functional === true) return true;
350
+
351
+ //----
352
+ // INSTANCES
353
+ //----
354
+ // ARRAYS
355
+ if (x.arrays.length && y.arrays.length) return true;
356
+ if (x.tuples.length && y.tuples.length) return true;
357
+ if (x.objects.length && y.objects.length) return true;
358
+ if (x.aliases.length && y.aliases.length) return true;
359
+
360
+ //----
361
+ // VALUES
362
+ //----
363
+ // ATOMICS
364
+ for (const atomic of x.atomics)
365
+ if (y.atomics.includes(atomic)) return true;
366
+
367
+ // CONSTANTS
368
+ for (const constant of x.constants) {
369
+ const opposite: MetadataConstant | undefined = y.constants.find(
370
+ (elem) => elem.type === constant.type,
371
+ );
372
+ if (opposite === undefined) continue;
373
+
374
+ const values: Set<any> = new Set([
375
+ ...constant.values,
376
+ ...opposite.values,
377
+ ]);
378
+ if (values.size !== constant.values.length + opposite.values.length)
379
+ return true;
380
+ }
381
+ return false;
382
+ };
383
+
384
+ export const covers = (
385
+ x: Metadata,
386
+ y: Metadata,
387
+ level: number = 0,
388
+ ): boolean => {
389
+ // CHECK ANY
390
+ if (x === y) return false;
391
+ else if (x.any) return true;
392
+ else if (y.any) return false;
393
+
394
+ //----
395
+ // INSTANCES
396
+ //----
397
+ if (level === 0) {
398
+ // ARRAYS
399
+ for (const ya of y.arrays)
400
+ if (
401
+ !x.arrays.some((xa) =>
402
+ covers(xa.value, ya.value, level + 1),
403
+ )
404
+ ) {
405
+ return false;
406
+ }
407
+
408
+ // TUPLES
409
+ for (const yt of y.tuples)
410
+ if (
411
+ yt.elements.length !== 0 &&
412
+ x.tuples.some(
413
+ (xt) =>
414
+ xt.elements.length >= yt.elements.length &&
415
+ xt.elements
416
+ .slice(yt.elements.length)
417
+ .every((xv, i) =>
418
+ covers(xv, yt.elements[i]!, level + 1),
419
+ ),
420
+ ) === false
421
+ )
422
+ return false;
423
+ }
424
+
425
+ // OBJECTS
426
+ for (const yo of y.objects)
427
+ if (x.objects.some((xo) => MetadataObject.covers(xo, yo)) === false)
428
+ return false;
429
+
430
+ // ALIASES
431
+ for (const yd of y.aliases)
432
+ if (x.aliases.some((xd) => xd.name === yd.name) === false)
433
+ return false;
434
+
435
+ // NATIVES
436
+ for (const yn of y.natives)
437
+ if (x.natives.some((xn) => xn === yn) === false) return false;
438
+
439
+ // SETS
440
+ for (const ys of y.sets)
441
+ if (x.sets.some((xs) => covers(xs, ys)) === false) return false;
442
+
443
+ //----
444
+ // VALUES
445
+ //----
446
+ // ATOMICS
447
+ if (y.atomics.some((atomic) => x.atomics.includes(atomic) === false))
448
+ return false;
449
+
450
+ // CONSTANTS
451
+ for (const yc of y.constants) {
452
+ const xc: MetadataConstant | undefined = x.constants.find(
453
+ (elem) => elem.type === yc.type,
454
+ );
455
+ if (xc === undefined) return false;
456
+ else if (
457
+ (yc.values as number[]).some(
458
+ (yv) => xc.values.includes(yv as never) === false,
459
+ )
460
+ )
461
+ return false;
462
+ }
463
+
464
+ // FUNCTIONAL
465
+ if (x.functional === false && y.functional) return false;
466
+
467
+ // SUCCESS
468
+ return true;
469
+ };
470
+
471
+ /**
472
+ * @internal
473
+ */
474
+ export const merge = (x: Metadata, y: Metadata): Metadata => {
475
+ const output: Metadata = Metadata.create({
476
+ any: x.any || y.any,
477
+ nullable: x.nullable || y.nullable,
478
+ required: x.required && y.required,
479
+ optional: x.optional || y.optional,
480
+ functional: x.functional || y.functional,
481
+
482
+ resolved:
483
+ x.resolved !== null && y.resolved !== null
484
+ ? merge(x.resolved, y.resolved)
485
+ : x.resolved || y.resolved,
486
+ atomics: [...new Set([...x.atomics, ...y.atomics])],
487
+ constants: [...x.constants],
488
+ templates: x.templates.slice(),
489
+
490
+ rest:
491
+ x.rest !== null && y.rest !== null
492
+ ? merge(x.rest, y.rest)
493
+ : x.rest ?? y.rest,
494
+ arrays: x.arrays.slice(),
495
+ tuples: x.tuples.slice(),
496
+ objects: x.objects.slice(),
497
+ aliases: x.aliases.slice(),
498
+
499
+ natives: [...new Set([...x.natives, ...y.natives])],
500
+ sets: x.sets.slice(),
501
+ maps: x.maps.slice(),
502
+ });
503
+ for (const constant of y.constants) {
504
+ const target: MetadataConstant = ArrayUtil.take(
505
+ output.constants,
506
+ (elem) => elem.type === constant.type,
507
+ () => ({
508
+ type: constant.type,
509
+ values: [],
510
+ }),
511
+ );
512
+ for (const value of constant.values)
513
+ ArrayUtil.add(target.values, value);
514
+ }
515
+ for (const array of y.arrays)
516
+ ArrayUtil.set(output.arrays, array, (elem) => elem.name);
517
+ for (const tuple of y.tuples)
518
+ ArrayUtil.set(output.tuples, tuple, (elem) => elem.name);
519
+ for (const obj of y.objects)
520
+ ArrayUtil.set(output.objects, obj, (elem) => elem.name);
521
+ for (const alias of y.aliases)
522
+ ArrayUtil.set(output.aliases, alias, (elem) => elem.name);
523
+
524
+ return output;
525
+ };
526
+ }
527
+
528
+ const getName = (metadata: Metadata): string => {
529
+ if (metadata.any === true) return "any";
530
+
531
+ const elements: string[] = [];
532
+
533
+ // OPTIONAL
534
+ if (metadata.nullable === true) elements.push("null");
535
+ if (metadata.required === false) elements.push("undefined");
536
+
537
+ // ATOMIC
538
+ for (const type of metadata.atomics) {
539
+ elements.push(type);
540
+ }
541
+ for (const constant of metadata.constants)
542
+ for (const value of constant.values)
543
+ elements.push(JSON.stringify(value));
544
+ for (const template of metadata.templates)
545
+ elements.push(
546
+ "`" +
547
+ template
548
+ .map((child) =>
549
+ child.isConstant() && child.size() === 1
550
+ ? child.constants[0]!.values[0]!
551
+ : `$\{${child.getName()}\}`,
552
+ )
553
+ .join("")
554
+ .split("`")
555
+ .join("\\`") +
556
+ "`",
557
+ );
558
+
559
+ // NATIVES
560
+ for (const native of metadata.natives) elements.push(native);
561
+ for (const set of metadata.sets) elements.push(`Set<${set.getName()}>`);
562
+ for (const map of metadata.maps)
563
+ elements.push(`Map<${map.key.getName()}, ${map.value.getName()}>`);
564
+
565
+ // INSTANCES
566
+ if (metadata.rest !== null) elements.push(`...${metadata.rest.getName()}`);
567
+ for (const tuple of metadata.tuples) elements.push(tuple.name);
568
+ for (const array of metadata.arrays) elements.push(array.name);
569
+ for (const object of metadata.objects) elements.push(object.name);
570
+ for (const alias of metadata.aliases) elements.push(alias.name);
571
+ if (metadata.resolved !== null) elements.push(metadata.resolved.getName());
572
+
573
+ // RETURNS
574
+ if (elements.length === 0) return "unknown";
575
+ else if (elements.length === 1) return elements[0]!;
576
+
577
+ elements.sort();
578
+ return `(${elements.join(" | ")})`;
579
+ };
580
+ export namespace Metadata {
581
+ export interface Entry {
582
+ key: Metadata;
583
+ value: Metadata;
584
+ }
585
+ }