typia 5.0.0-dev.20230730 → 5.0.0-dev.20230808

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 (239) hide show
  1. package/lib/executable/TypiaSetupWizard.js +39 -28
  2. package/lib/executable/TypiaSetupWizard.js.map +1 -1
  3. package/lib/factories/CommentFactory.d.ts +1 -1
  4. package/lib/factories/CommentFactory.js +31 -28
  5. package/lib/factories/CommentFactory.js.map +1 -1
  6. package/lib/functional/$string.js +16 -20
  7. package/lib/functional/$string.js.map +1 -1
  8. package/lib/programmers/RandomProgrammer.js +1 -1
  9. package/lib/programmers/RandomProgrammer.js.map +1 -1
  10. package/lib/programmers/internal/application_object.js +2 -0
  11. package/lib/programmers/internal/application_object.js.map +1 -1
  12. package/lib/programmers/internal/stringify_dynamic_properties.js +1 -0
  13. package/lib/programmers/internal/stringify_dynamic_properties.js.map +1 -1
  14. package/lib/schemas/metadata/Metadata.d.ts +1 -0
  15. package/lib/schemas/metadata/Metadata.js +3 -0
  16. package/lib/schemas/metadata/Metadata.js.map +1 -1
  17. package/lib/utils/PatternUtil.d.ts +1 -1
  18. package/lib/utils/PatternUtil.js +3 -1
  19. package/lib/utils/PatternUtil.js.map +1 -1
  20. package/package.json +3 -3
  21. package/src/executable/TypiaSetupWizard.ts +34 -14
  22. package/src/factories/CommentFactory.ts +84 -76
  23. package/src/factories/MetadataCollection.ts +264 -264
  24. package/src/factories/MetadataFactory.ts +30 -30
  25. package/src/factories/MetadataTagFactory.ts +362 -362
  26. package/src/factories/ProtocolFactory.ts +80 -80
  27. package/src/factories/internal/metadata/MetadataHelper.ts +12 -12
  28. package/src/factories/internal/metadata/emend_metadata_atomics.ts +33 -33
  29. package/src/factories/internal/metadata/emplace_metadata_alias.ts +40 -40
  30. package/src/factories/internal/metadata/emplace_metadata_array.ts +34 -34
  31. package/src/factories/internal/metadata/emplace_metadata_object.ts +136 -136
  32. package/src/factories/internal/metadata/emplace_metadata_tuple.ts +50 -50
  33. package/src/factories/internal/metadata/explore_metadata.ts +40 -40
  34. package/src/factories/internal/metadata/iterate_metadata.ts +81 -81
  35. package/src/factories/internal/metadata/iterate_metadata_alias.ts +30 -30
  36. package/src/factories/internal/metadata/iterate_metadata_array.ts +24 -24
  37. package/src/factories/internal/metadata/iterate_metadata_atomic.ts +59 -59
  38. package/src/factories/internal/metadata/iterate_metadata_coalesce.ts +33 -33
  39. package/src/factories/internal/metadata/iterate_metadata_collection.ts +133 -133
  40. package/src/factories/internal/metadata/iterate_metadata_constant.ts +58 -58
  41. package/src/factories/internal/metadata/iterate_metadata_intersection.ts +83 -83
  42. package/src/factories/internal/metadata/iterate_metadata_map.ts +41 -41
  43. package/src/factories/internal/metadata/iterate_metadata_native.ts +219 -219
  44. package/src/factories/internal/metadata/iterate_metadata_object.ts +43 -43
  45. package/src/factories/internal/metadata/iterate_metadata_resolve.ts +49 -49
  46. package/src/factories/internal/metadata/iterate_metadata_set.ts +33 -33
  47. package/src/factories/internal/metadata/iterate_metadata_sort.ts +69 -69
  48. package/src/factories/internal/metadata/iterate_metadata_tag.ts +31 -31
  49. package/src/factories/internal/metadata/iterate_metadata_template.ts +38 -38
  50. package/src/factories/internal/metadata/iterate_metadata_tuple.ts +24 -24
  51. package/src/factories/internal/metadata/iterate_metadata_union.ts +24 -24
  52. package/src/factories/internal/protocols/ProtocolMetadataUtil.ts +180 -180
  53. package/src/factories/internal/protocols/emplace_protocol_object.ts +25 -25
  54. package/src/factories/internal/protocols/emplace_protocol_property.ts +12 -12
  55. package/src/factories/internal/protocols/iterate_protocol_atomic.ts +34 -34
  56. package/src/factories/internal/protocols/iterate_protocol_constant.ts +27 -27
  57. package/src/factories/internal/protocols/iterate_protocol_main.ts +19 -19
  58. package/src/factories/internal/protocols/iterate_protocol_map.ts +58 -58
  59. package/src/factories/internal/protocols/iterate_protocol_metadata.ts +96 -96
  60. package/src/factories/internal/protocols/iterate_protocol_native.ts +34 -34
  61. package/src/factories/internal/protocols/iterate_protocol_never.ts +1 -1
  62. package/src/factories/internal/protocols/iterate_protocol_object.ts +110 -110
  63. package/src/factories/internal/protocols/iterate_protocol_repeated.ts +34 -34
  64. package/src/factories/internal/protocols/iterate_protocol_tuple.ts +29 -29
  65. package/src/functional/$number.ts +13 -13
  66. package/src/functional/$proto_bytes.ts +25 -25
  67. package/src/functional/$proto_field.ts +30 -30
  68. package/src/functional/$proto_float.ts +37 -37
  69. package/src/functional/$proto_i32.ts +29 -29
  70. package/src/functional/$proto_i64.ts +37 -37
  71. package/src/functional/$proto_size.ts +82 -82
  72. package/src/functional/$proto_string.ts +24 -24
  73. package/src/functional/$string.ts +32 -19
  74. package/src/functional/$varint.ts +130 -130
  75. package/src/functional/$zigzag.ts +39 -39
  76. package/src/json.ts +648 -648
  77. package/src/misc.ts +651 -651
  78. package/src/module.ts +781 -781
  79. package/src/programmers/CheckerProgrammer.ts +1182 -1182
  80. package/src/programmers/FeatureProgrammer.ts +495 -495
  81. package/src/programmers/IsProgrammer.ts +239 -239
  82. package/src/programmers/IsPruneProgrammer.ts +73 -73
  83. package/src/programmers/RandomProgrammer.ts +579 -581
  84. package/src/programmers/helpers/AtomicPredicator.ts +31 -31
  85. package/src/programmers/helpers/IExpressionEntry.ts +12 -12
  86. package/src/programmers/helpers/PruneJoiner.ts +143 -143
  87. package/src/programmers/helpers/RandomJoiner.ts +173 -173
  88. package/src/programmers/helpers/RandomRanger.ts +216 -216
  89. package/src/programmers/helpers/StringifyPredicator.ts +13 -13
  90. package/src/programmers/helpers/UnionExplorer.ts +301 -301
  91. package/src/programmers/helpers/UnionPredicator.ts +81 -81
  92. package/src/programmers/internal/application_alias.ts +66 -66
  93. package/src/programmers/internal/application_array.ts +30 -30
  94. package/src/programmers/internal/application_constant.ts +26 -26
  95. package/src/programmers/internal/application_default.ts +17 -17
  96. package/src/programmers/internal/application_default_string.ts +33 -33
  97. package/src/programmers/internal/application_native.ts +39 -39
  98. package/src/programmers/internal/application_number.ts +77 -77
  99. package/src/programmers/internal/application_object.ts +165 -162
  100. package/src/programmers/internal/application_resolved.ts +55 -55
  101. package/src/programmers/internal/application_schema.ts +157 -157
  102. package/src/programmers/internal/application_string.ts +44 -44
  103. package/src/programmers/internal/application_templates.ts +25 -25
  104. package/src/programmers/internal/application_tuple.ts +57 -57
  105. package/src/programmers/internal/check_array.ts +30 -30
  106. package/src/programmers/internal/check_array_length.ts +35 -35
  107. package/src/programmers/internal/check_bigint.ts +82 -82
  108. package/src/programmers/internal/check_custom.ts +31 -31
  109. package/src/programmers/internal/check_number.ts +177 -177
  110. package/src/programmers/internal/check_string.ts +25 -25
  111. package/src/programmers/internal/check_string_tags.ts +67 -67
  112. package/src/programmers/internal/check_template.ts +56 -56
  113. package/src/programmers/internal/check_union_array_like.ts +329 -329
  114. package/src/programmers/internal/decode_union_object.ts +73 -73
  115. package/src/programmers/internal/feature_object_entries.ts +63 -63
  116. package/src/programmers/internal/get_comment_tags.ts +23 -23
  117. package/src/programmers/internal/metadata_to_pattern.ts +34 -34
  118. package/src/programmers/internal/prune_object_properties.ts +60 -60
  119. package/src/programmers/internal/random_custom.ts +29 -29
  120. package/src/programmers/internal/stringify_dynamic_properties.ts +4 -0
  121. package/src/programmers/internal/stringify_regular_properties.ts +83 -83
  122. package/src/programmers/internal/template_to_pattern.ts +15 -15
  123. package/src/programmers/internal/wrap_metadata_rest_tuple.ts +16 -16
  124. package/src/programmers/json/JsonApplicationProgrammer.ts +47 -47
  125. package/src/programmers/json/JsonAssertParseProgrammer.ts +66 -66
  126. package/src/programmers/json/JsonAssertStringifyProgrammer.ts +66 -66
  127. package/src/programmers/json/JsonIsParseProgrammer.ts +72 -72
  128. package/src/programmers/json/JsonIsStringifyProgrammer.ts +76 -76
  129. package/src/programmers/json/JsonStringifyProgrammer.ts +979 -979
  130. package/src/programmers/json/JsonValidateParseProgrammer.ts +70 -70
  131. package/src/programmers/json/JsonValidateStringifyProgrammer.ts +84 -84
  132. package/src/programmers/misc/MiscAssertCloneProgrammer.ts +71 -71
  133. package/src/programmers/misc/MiscAssertPruneProgrammer.ts +68 -68
  134. package/src/programmers/misc/MiscCloneProgrammer.ts +587 -587
  135. package/src/programmers/misc/MiscIsCloneProgrammer.ts +78 -78
  136. package/src/programmers/misc/MiscLiteralsProgrammer.ts +63 -63
  137. package/src/programmers/misc/MiscPruneProgrammer.ts +542 -542
  138. package/src/programmers/misc/MiscValidateCloneProgrammer.ts +85 -85
  139. package/src/programmers/misc/MiscValidatePruneProgrammer.ts +78 -78
  140. package/src/programmers/protobuf/ProtobufAssertDecodeProgrammer.ts +69 -69
  141. package/src/programmers/protobuf/ProtobufAssertEncodeProgrammer.ts +66 -66
  142. package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +38 -38
  143. package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +36 -36
  144. package/src/programmers/protobuf/ProtobufIsDecodeProgrammer.ts +75 -75
  145. package/src/programmers/protobuf/ProtobufIsEncodeProgrammer.ts +76 -76
  146. package/src/programmers/protobuf/ProtobufMessageProgrammer.ts +125 -125
  147. package/src/programmers/protobuf/ProtobufValidateDecodeProgrammer.ts +83 -83
  148. package/src/programmers/protobuf/ProtobufValidateEncodeProgrammer.ts +86 -86
  149. package/src/protobuf.ts +210 -210
  150. package/src/schemas/json/IJsonApplication.ts +8 -8
  151. package/src/schemas/json/IJsonComponents.ts +33 -33
  152. package/src/schemas/json/IJsonSchema.ts +133 -133
  153. package/src/schemas/metadata/ICommentTag.ts +4 -4
  154. package/src/schemas/metadata/IJsDocTagInfo.ts +10 -10
  155. package/src/schemas/metadata/IMetadata.ts +28 -28
  156. package/src/schemas/metadata/IMetadataAlias.ts +14 -14
  157. package/src/schemas/metadata/IMetadataApplication.ts +7 -7
  158. package/src/schemas/metadata/IMetadataArray.ts +10 -10
  159. package/src/schemas/metadata/IMetadataCollection.ts +11 -11
  160. package/src/schemas/metadata/IMetadataConstant.ts +16 -16
  161. package/src/schemas/metadata/IMetadataDictionary.ts +14 -14
  162. package/src/schemas/metadata/IMetadataEntry.ts +6 -6
  163. package/src/schemas/metadata/IMetadataObject.ts +18 -18
  164. package/src/schemas/metadata/IMetadataProperty.ts +11 -11
  165. package/src/schemas/metadata/IMetadataResolved.ts +6 -6
  166. package/src/schemas/metadata/IMetadataTag.ts +113 -113
  167. package/src/schemas/metadata/IMetadataTuple.ts +10 -10
  168. package/src/schemas/metadata/Metadata.ts +610 -608
  169. package/src/schemas/metadata/MetadataAlias.ts +66 -66
  170. package/src/schemas/metadata/MetadataArray.ts +55 -55
  171. package/src/schemas/metadata/MetadataConstant.ts +3 -3
  172. package/src/schemas/metadata/MetadataObject.ts +129 -129
  173. package/src/schemas/metadata/MetadataProperty.ts +64 -64
  174. package/src/schemas/metadata/MetadataResolved.ts +51 -51
  175. package/src/schemas/metadata/MetadataTuple.ts +53 -53
  176. package/src/schemas/protobuf/IProtocolMap.ts +5 -5
  177. package/src/schemas/protobuf/IProtocolMessage.ts +6 -6
  178. package/src/schemas/protobuf/IProtocolProperty.ts +13 -13
  179. package/src/transformers/CallExpressionTransformer.ts +240 -240
  180. package/src/transformers/features/AssertTransformer.ts +11 -11
  181. package/src/transformers/features/CreateAssertTransformer.ts +13 -13
  182. package/src/transformers/features/CreateIsTransformer.ts +11 -11
  183. package/src/transformers/features/CreateRandomTransformer.ts +39 -39
  184. package/src/transformers/features/CreateValidateTransformer.ts +13 -13
  185. package/src/transformers/features/IsTransformer.ts +11 -11
  186. package/src/transformers/features/RandomTransformer.ts +42 -42
  187. package/src/transformers/features/ValidateTransformer.ts +11 -11
  188. package/src/transformers/features/json/JsonApplicationTransformer.ts +102 -102
  189. package/src/transformers/features/json/JsonAssertParseTransformer.ts +10 -10
  190. package/src/transformers/features/json/JsonAssertStringifyTransformer.ts +10 -10
  191. package/src/transformers/features/json/JsonCreateAssertParseTransformer.ts +10 -10
  192. package/src/transformers/features/json/JsonCreateAssertStringifyTransformer.ts +12 -12
  193. package/src/transformers/features/json/JsonCreateIsParseTransformer.ts +9 -9
  194. package/src/transformers/features/json/JsonCreateIsStringifyTransformer.ts +10 -10
  195. package/src/transformers/features/json/JsonCreateStringifyTransformer.ts +9 -9
  196. package/src/transformers/features/json/JsonCreateValidateParseTransformer.ts +10 -10
  197. package/src/transformers/features/json/JsonCreateValidateStringifyProgrammer.ts +12 -12
  198. package/src/transformers/features/json/JsonIsParseTransformer.ts +9 -9
  199. package/src/transformers/features/json/JsonIsStringifyTransformer.ts +10 -10
  200. package/src/transformers/features/json/JsonStringifyTransformer.ts +9 -9
  201. package/src/transformers/features/json/JsonValidateParseTransformer.ts +10 -10
  202. package/src/transformers/features/json/JsonValidateStringifyTransformer.ts +10 -10
  203. package/src/transformers/features/misc/MetadataTransformer.ts +53 -53
  204. package/src/transformers/features/misc/MiscAssertCloneTransformer.ts +10 -10
  205. package/src/transformers/features/misc/MiscAssertPruneTransformer.ts +10 -10
  206. package/src/transformers/features/misc/MiscCloneTransformer.ts +9 -9
  207. package/src/transformers/features/misc/MiscCreateAssertCloneTransformer.ts +10 -10
  208. package/src/transformers/features/misc/MiscCreateAssertPruneTransformer.ts +10 -10
  209. package/src/transformers/features/misc/MiscCreateCloneTransformer.ts +9 -9
  210. package/src/transformers/features/misc/MiscCreateIsCloneTransformer.ts +9 -9
  211. package/src/transformers/features/misc/MiscCreateIsPruneTransformer.ts +9 -9
  212. package/src/transformers/features/misc/MiscCreatePruneTransformer.ts +9 -9
  213. package/src/transformers/features/misc/MiscCreateValidateCloneTransformer.ts +10 -10
  214. package/src/transformers/features/misc/MiscCreateValidatePruneTransformer.ts +10 -10
  215. package/src/transformers/features/misc/MiscIsCloneTransformer.ts +9 -9
  216. package/src/transformers/features/misc/MiscIsPruneTransformer.ts +9 -9
  217. package/src/transformers/features/misc/MiscLiteralsTransformer.ts +28 -28
  218. package/src/transformers/features/misc/MiscPruneTransformer.ts +9 -9
  219. package/src/transformers/features/misc/MiscValidateCloneTransformer.ts +10 -10
  220. package/src/transformers/features/misc/MiscValidatePruneTransformer.ts +10 -10
  221. package/src/transformers/features/protobuf/CreateProtobufAssertDecodeTransformer.ts +10 -10
  222. package/src/transformers/features/protobuf/CreateProtobufAssertEncodeTransformer.ts +10 -10
  223. package/src/transformers/features/protobuf/CreateProtobufDecodeTransformer.ts +10 -10
  224. package/src/transformers/features/protobuf/CreateProtobufEncodeTransformer.ts +10 -10
  225. package/src/transformers/features/protobuf/CreateProtobufIsDecodeTransformer.ts +10 -10
  226. package/src/transformers/features/protobuf/CreateProtobufIsEncodeTransformer.ts +10 -10
  227. package/src/transformers/features/protobuf/CreateProtobufValidateDecodeTransformer.ts +10 -10
  228. package/src/transformers/features/protobuf/CreateProtobufValidateEncodeTransformer.ts +10 -10
  229. package/src/transformers/features/protobuf/ProtobufAssertDecodeTransformer.ts +10 -10
  230. package/src/transformers/features/protobuf/ProtobufAssertEncodeTransformer.ts +10 -10
  231. package/src/transformers/features/protobuf/ProtobufDecodeTransformer.ts +10 -10
  232. package/src/transformers/features/protobuf/ProtobufEncodeTransformer.ts +10 -10
  233. package/src/transformers/features/protobuf/ProtobufIsDecodeTransformer.ts +10 -10
  234. package/src/transformers/features/protobuf/ProtobufIsEncodeTransformer.ts +10 -10
  235. package/src/transformers/features/protobuf/ProtobufMessageTransformer.ts +31 -31
  236. package/src/transformers/features/protobuf/ProtobufValidateDecodeTransformer.ts +10 -10
  237. package/src/transformers/features/protobuf/ProtobufValidateEncodeTransformer.ts +10 -10
  238. package/src/utils/NameEncoder.ts +32 -32
  239. package/src/utils/PatternUtil.ts +4 -1
@@ -1,608 +1,610 @@
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 { MetadataResolved } from "./MetadataResolved";
16
- import { MetadataTuple } from "./MetadataTuple";
17
-
18
- export class Metadata {
19
- public any: boolean;
20
- public required: boolean;
21
- public optional: boolean;
22
- public nullable: boolean;
23
- public functional: boolean;
24
-
25
- public resolved: MetadataResolved | null;
26
- public atomics: Atomic.Literal[];
27
- public constants: MetadataConstant[];
28
- public templates: Metadata[][];
29
-
30
- public rest: Metadata | null;
31
- public aliases: MetadataAlias[];
32
- public arrays: MetadataArray[];
33
- public tuples: MetadataTuple[];
34
- public objects: MetadataObject[];
35
-
36
- public natives: string[];
37
- public sets: Metadata[];
38
- public maps: Metadata.Entry[];
39
-
40
- /** @internal */ private name_?: string;
41
- /** @internal */ private parent_resolved_: boolean = false;
42
- /** @internal */ public union_index?: number;
43
-
44
- /* -----------------------------------------------------------
45
- CONSTRUCTORS
46
- ----------------------------------------------------------- */
47
- /**
48
- * @hidden
49
- */
50
- private constructor(props: ClassProperties<Metadata>) {
51
- this.any = props.any;
52
- this.required = props.required;
53
- this.optional = props.optional;
54
- this.nullable = props.nullable;
55
- this.functional = props.functional;
56
-
57
- this.resolved = props.resolved;
58
- this.atomics = props.atomics;
59
- this.constants = props.constants;
60
- this.templates = props.templates;
61
-
62
- this.rest = props.rest;
63
- this.arrays = props.arrays;
64
- this.tuples = props.tuples;
65
- this.objects = props.objects;
66
- this.aliases = props.aliases;
67
-
68
- this.natives = props.natives;
69
- this.sets = props.sets;
70
- this.maps = props.maps;
71
- }
72
-
73
- /**
74
- * @internal
75
- */
76
- public static create(props: ClassProperties<Metadata>): Metadata {
77
- return new Metadata(props);
78
- }
79
-
80
- /**
81
- * @internal
82
- */
83
- public static initialize(parentResolved: boolean = false): Metadata {
84
- const meta: Metadata = this.create({
85
- any: false,
86
- nullable: false,
87
- required: true,
88
- optional: false,
89
- functional: false,
90
-
91
- resolved: null,
92
- constants: [],
93
- atomics: [],
94
- templates: [],
95
- arrays: [],
96
- tuples: [],
97
- objects: [],
98
- aliases: [],
99
-
100
- rest: null,
101
- natives: [],
102
- sets: [],
103
- maps: [],
104
- });
105
- meta.parent_resolved_ = parentResolved;
106
- return meta;
107
- }
108
-
109
- public toJSON(): IMetadata {
110
- return {
111
- any: this.any,
112
- required: this.required,
113
- optional: this.optional,
114
- nullable: this.nullable,
115
- functional: this.functional,
116
-
117
- atomics: this.atomics.slice(),
118
- constants: JSON.parse(JSON.stringify(this.constants)),
119
- templates: this.templates.map((tpl) =>
120
- tpl.map((meta) => meta.toJSON()),
121
- ),
122
- resolved: this.resolved ? this.resolved.toJSON() : null,
123
-
124
- rest: this.rest ? this.rest.toJSON() : null,
125
- arrays: this.arrays.map((array) => array.name),
126
- tuples: this.tuples.map((tuple) => tuple.name),
127
- objects: this.objects.map((obj) => obj.name),
128
- aliases: this.aliases.map((alias) => alias.name),
129
-
130
- natives: this.natives.slice(),
131
- sets: this.sets.map((meta) => meta.toJSON()),
132
- maps: this.maps.map((entry) => ({
133
- key: entry.key.toJSON(),
134
- value: entry.value.toJSON(),
135
- })),
136
- };
137
- }
138
-
139
- public static from(
140
- meta: IMetadata,
141
- collection: IMetadataCollection,
142
- ): Metadata {
143
- const dict: IMetadataDictionary = {
144
- objects: new Map(
145
- collection.objects.map((obj) => [
146
- obj.name,
147
- MetadataObject._From_without_properties(obj),
148
- ]),
149
- ),
150
- aliases: new Map(
151
- collection.aliases.map((alias) => [
152
- alias.name,
153
- MetadataAlias._From_without_value(alias),
154
- ]),
155
- ),
156
- arrays: new Map(
157
- collection.arrays.map((arr) => [
158
- arr.name,
159
- MetadataArray._From_without_value(arr),
160
- ]),
161
- ),
162
- tuples: new Map(
163
- collection.tuples.map((tpl) => [
164
- tpl.name,
165
- MetadataTuple._From_without_elements(tpl),
166
- ]),
167
- ),
168
- };
169
-
170
- for (const obj of collection.objects) {
171
- const initialized = dict.objects.get(obj.name)!;
172
- initialized.properties.push(
173
- ...obj.properties.map((prop) =>
174
- MetadataProperty._From(prop, dict),
175
- ),
176
- );
177
- }
178
- for (const alias of collection.aliases)
179
- Writable(dict.aliases.get(alias.name)!).value = this._From(
180
- alias.value,
181
- dict,
182
- );
183
- for (const array of collection.arrays)
184
- Writable(dict.arrays.get(array.name)!).value = this._From(
185
- array.value,
186
- dict,
187
- );
188
- for (const tuple of collection.tuples)
189
- Writable(dict.tuples.get(tuple.name)!).elements =
190
- tuple.elements.map((elem) => this._From(elem, dict));
191
-
192
- return this._From(meta, dict);
193
- }
194
-
195
- /**
196
- * @internal
197
- */
198
- public static _From(meta: IMetadata, dict: IMetadataDictionary): Metadata {
199
- return this.create({
200
- any: meta.any,
201
- required: meta.required,
202
- optional: meta.optional,
203
- nullable: meta.nullable,
204
- functional: meta.functional,
205
-
206
- constants: JSON.parse(JSON.stringify(meta.constants)),
207
- atomics: meta.atomics.slice(),
208
- templates: meta.templates.map((tpl) =>
209
- tpl.map((meta) => this._From(meta, dict)),
210
- ),
211
- resolved: meta.resolved
212
- ? MetadataResolved._From(meta.resolved, dict)
213
- : null,
214
-
215
- rest: meta.rest ? this._From(meta.rest, dict) : null,
216
- arrays: meta.arrays.map((id) => {
217
- const array = dict.arrays.get(id);
218
- if (array === undefined)
219
- throw new Error(
220
- `Error on Metadata.from(): failed to find array "${id}".`,
221
- );
222
- return array;
223
- }),
224
- tuples: meta.tuples.map((id) => {
225
- const tuple = dict.tuples.get(id);
226
- if (tuple === undefined)
227
- throw new Error(
228
- `Error on Metadata.from(): failed to find tuple "${id}".`,
229
- );
230
- return tuple;
231
- }),
232
- objects: meta.objects.map((name) => {
233
- const found = dict.objects.get(name);
234
- if (found === undefined)
235
- throw new Error(
236
- `Error on Metadata.from(): failed to find object "${name}".`,
237
- );
238
- return found;
239
- }),
240
- aliases: meta.aliases.map((alias) => {
241
- const found = dict.aliases.get(alias);
242
- if (found === undefined)
243
- throw new Error(
244
- `Error on Metadata.from(): failed to find alias "${alias}".`,
245
- );
246
- return found;
247
- }),
248
-
249
- natives: meta.natives.slice(),
250
- sets: meta.sets.map((meta) => this._From(meta, dict)),
251
- maps: meta.maps.map((entry) => ({
252
- key: this._From(entry.key, dict),
253
- value: this._From(entry.value, dict),
254
- })),
255
- });
256
- }
257
-
258
- /* -----------------------------------------------------------
259
- ACCESSORS
260
- ----------------------------------------------------------- */
261
- public getName(): string {
262
- this.name_ ??= getName(this);
263
- return this.name_;
264
- }
265
-
266
- public empty(): boolean {
267
- return this.bucket() === 0 || this.size() === 0;
268
- }
269
-
270
- public size(): number {
271
- return (
272
- (this.any ? 1 : 0) +
273
- (this.resolved ? 1 : 0) +
274
- (this.functional ? 1 : 0) +
275
- (this.rest ? this.rest.size() : 0) +
276
- this.templates.length +
277
- this.atomics.length +
278
- this.constants
279
- .map((c) => c.values.length)
280
- .reduce((x, y) => x + y, 0) +
281
- this.arrays.length +
282
- this.tuples.length +
283
- this.natives.length +
284
- this.maps.length +
285
- this.sets.length +
286
- this.objects.length +
287
- this.aliases.length
288
- );
289
- }
290
-
291
- public bucket(): number {
292
- return (
293
- (this.any ? 1 : 0) +
294
- (this.resolved ? 1 : 0) +
295
- (this.functional ? 1 : 0) +
296
- (this.templates.length ? 1 : 0) +
297
- (this.atomics.length ? 1 : 0) +
298
- (this.constants.length ? 1 : 0) +
299
- (this.rest ? this.rest.size() : 0) +
300
- (this.arrays.length ? 1 : 0) +
301
- (this.tuples.length ? 1 : 0) +
302
- (this.natives.length ? 1 : 0) +
303
- (this.sets.length ? 1 : 0) +
304
- (this.maps.length ? 1 : 0) +
305
- (this.objects.length ? 1 : 0) +
306
- (this.aliases.length ? 1 : 0)
307
- );
308
- }
309
-
310
- public isConstant(): boolean {
311
- return this.bucket() === (this.constants.length ? 1 : 0);
312
- }
313
-
314
- public isRequired(): boolean {
315
- return this.required === true && this.optional === false;
316
- }
317
-
318
- /**
319
- * @internal
320
- */
321
- public isUnionBucket(): boolean {
322
- const size: number = this.bucket();
323
- const emended: number = this.constants.length ? size - 1 : size;
324
- return emended > 1;
325
- }
326
-
327
- /**
328
- * @internal
329
- */
330
- public getSoleLiteral(): string | null {
331
- if (
332
- this.size() === 1 &&
333
- this.constants.length === 1 &&
334
- this.constants[0]!.type === "string" &&
335
- this.constants[0]!.values.length === 1
336
- )
337
- return this.constants[0]!.values[0] as string;
338
- else return null;
339
- }
340
-
341
- /**
342
- * @internal
343
- */
344
- public isSoleLiteral(): boolean {
345
- return this.getSoleLiteral() !== null;
346
- }
347
-
348
- /**
349
- * @internal
350
- */
351
- public isParentResolved(): boolean {
352
- return this.parent_resolved_;
353
- }
354
- }
355
- export namespace Metadata {
356
- export const intersects = (x: Metadata, y: Metadata): boolean => {
357
- // CHECK ANY & OPTIONAL
358
- if (x.any || y.any) return true;
359
- if (x.isRequired() === false && false === y.isRequired()) return true;
360
- if (x.nullable === true && true === y.nullable) return true;
361
- if (x.functional === true && y.functional === true) return true;
362
-
363
- //----
364
- // INSTANCES
365
- //----
366
- // ARRAYS
367
- if (x.arrays.length && y.arrays.length) return true;
368
- if (x.tuples.length && y.tuples.length) return true;
369
- if (x.objects.length && y.objects.length) return true;
370
- if (x.aliases.length && y.aliases.length) return true;
371
-
372
- //----
373
- // VALUES
374
- //----
375
- // ATOMICS
376
- for (const atomic of x.atomics)
377
- if (y.atomics.includes(atomic)) return true;
378
-
379
- // CONSTANTS
380
- for (const constant of x.constants) {
381
- const opposite: MetadataConstant | undefined = y.constants.find(
382
- (elem) => elem.type === constant.type,
383
- );
384
- if (opposite === undefined) continue;
385
-
386
- const values: Set<any> = new Set([
387
- ...constant.values,
388
- ...opposite.values,
389
- ]);
390
- if (values.size !== constant.values.length + opposite.values.length)
391
- return true;
392
- }
393
- return false;
394
- };
395
-
396
- export const covers = (
397
- x: Metadata,
398
- y: Metadata,
399
- level: number = 0,
400
- ): boolean => {
401
- // CHECK ANY
402
- if (x === y) return false;
403
- else if (x.any) return true;
404
- else if (y.any) return false;
405
-
406
- //----
407
- // INSTANCES
408
- //----
409
- if (level === 0) {
410
- // ARRAYS
411
- for (const ya of y.arrays)
412
- if (
413
- !x.arrays.some((xa) =>
414
- covers(xa.value, ya.value, level + 1),
415
- )
416
- ) {
417
- return false;
418
- }
419
-
420
- // TUPLES
421
- for (const yt of y.tuples)
422
- if (
423
- yt.elements.length !== 0 &&
424
- x.tuples.some(
425
- (xt) =>
426
- xt.elements.length >= yt.elements.length &&
427
- xt.elements
428
- .slice(yt.elements.length)
429
- .every((xv, i) =>
430
- covers(xv, yt.elements[i]!, level + 1),
431
- ),
432
- ) === false
433
- )
434
- return false;
435
- }
436
-
437
- // OBJECTS
438
- for (const yo of y.objects)
439
- if (x.objects.some((xo) => MetadataObject.covers(xo, yo)) === false)
440
- return false;
441
-
442
- // ALIASES
443
- for (const yd of y.aliases)
444
- if (x.aliases.some((xd) => xd.name === yd.name) === false)
445
- return false;
446
-
447
- // NATIVES
448
- for (const yn of y.natives)
449
- if (x.natives.some((xn) => xn === yn) === false) return false;
450
-
451
- // SETS
452
- for (const ys of y.sets)
453
- if (x.sets.some((xs) => covers(xs, ys)) === false) return false;
454
-
455
- //----
456
- // VALUES
457
- //----
458
- // ATOMICS
459
- if (y.atomics.some((atomic) => x.atomics.includes(atomic) === false))
460
- return false;
461
-
462
- // CONSTANTS
463
- for (const yc of y.constants) {
464
- if (x.atomics.some((type) => yc.type === type)) continue;
465
- const xc: MetadataConstant | undefined = x.constants.find(
466
- (elem) => elem.type === yc.type,
467
- );
468
- if (xc === undefined) return false;
469
- else if (
470
- (yc.values as number[]).some(
471
- (yv) => xc.values.includes(yv as never) === false,
472
- )
473
- )
474
- return false;
475
- }
476
-
477
- // FUNCTIONAL
478
- if (x.functional === false && y.functional) return false;
479
-
480
- // SUCCESS
481
- return true;
482
- };
483
-
484
- /**
485
- * @internal
486
- */
487
- export const merge = (x: Metadata, y: Metadata): Metadata => {
488
- const output: Metadata = Metadata.create({
489
- any: x.any || y.any,
490
- nullable: x.nullable || y.nullable,
491
- required: x.required && y.required,
492
- optional: x.optional || y.optional,
493
- functional: x.functional || y.functional,
494
-
495
- resolved:
496
- x.resolved !== null && y.resolved !== null
497
- ? //? merge(x.resolved, y.resolved)
498
- MetadataResolved.create({
499
- original: merge(
500
- x.resolved.original,
501
- y.resolved.original,
502
- ),
503
- returns: merge(
504
- x.resolved.returns,
505
- y.resolved.returns,
506
- ),
507
- })
508
- : x.resolved ?? y.resolved,
509
- atomics: [...new Set([...x.atomics, ...y.atomics])],
510
- constants: [...x.constants],
511
- templates: x.templates.slice(),
512
-
513
- rest:
514
- x.rest !== null && y.rest !== null
515
- ? merge(x.rest, y.rest)
516
- : x.rest ?? y.rest,
517
- arrays: x.arrays.slice(),
518
- tuples: x.tuples.slice(),
519
- objects: x.objects.slice(),
520
- aliases: x.aliases.slice(),
521
-
522
- natives: [...new Set([...x.natives, ...y.natives])],
523
- sets: x.sets.slice(),
524
- maps: x.maps.slice(),
525
- });
526
- for (const constant of y.constants) {
527
- const target: MetadataConstant = ArrayUtil.take(
528
- output.constants,
529
- (elem) => elem.type === constant.type,
530
- () => ({
531
- type: constant.type,
532
- values: [],
533
- }),
534
- );
535
- for (const value of constant.values)
536
- ArrayUtil.add(target.values, value);
537
- }
538
- for (const array of y.arrays)
539
- ArrayUtil.set(output.arrays, array, (elem) => elem.name);
540
- for (const tuple of y.tuples)
541
- ArrayUtil.set(output.tuples, tuple, (elem) => elem.name);
542
- for (const obj of y.objects)
543
- ArrayUtil.set(output.objects, obj, (elem) => elem.name);
544
- for (const alias of y.aliases)
545
- ArrayUtil.set(output.aliases, alias, (elem) => elem.name);
546
-
547
- return output;
548
- };
549
- }
550
-
551
- const getName = (metadata: Metadata): string => {
552
- if (metadata.any === true) return "any";
553
-
554
- const elements: string[] = [];
555
-
556
- // OPTIONAL
557
- if (metadata.nullable === true) elements.push("null");
558
- if (metadata.isRequired() === false) elements.push("undefined");
559
-
560
- // ATOMIC
561
- for (const type of metadata.atomics) {
562
- elements.push(type);
563
- }
564
- for (const constant of metadata.constants)
565
- for (const value of constant.values)
566
- elements.push(JSON.stringify(value));
567
- for (const template of metadata.templates)
568
- elements.push(
569
- "`" +
570
- template
571
- .map((child) =>
572
- child.isConstant() && child.size() === 1
573
- ? child.constants[0]!.values[0]!
574
- : `$\{${child.getName()}\}`,
575
- )
576
- .join("")
577
- .split("`")
578
- .join("\\`") +
579
- "`",
580
- );
581
-
582
- // NATIVES
583
- for (const native of metadata.natives) elements.push(native);
584
- for (const set of metadata.sets) elements.push(`Set<${set.getName()}>`);
585
- for (const map of metadata.maps)
586
- elements.push(`Map<${map.key.getName()}, ${map.value.getName()}>`);
587
-
588
- // INSTANCES
589
- if (metadata.rest !== null) elements.push(`...${metadata.rest.getName()}`);
590
- for (const tuple of metadata.tuples) elements.push(tuple.name);
591
- for (const array of metadata.arrays) elements.push(array.name);
592
- for (const object of metadata.objects) elements.push(object.name);
593
- for (const alias of metadata.aliases) elements.push(alias.name);
594
- if (metadata.resolved !== null) elements.push(metadata.resolved.getName());
595
-
596
- // RETURNS
597
- if (elements.length === 0) return "unknown";
598
- else if (elements.length === 1) return elements[0]!;
599
-
600
- elements.sort();
601
- return `(${elements.join(" | ")})`;
602
- };
603
- export namespace Metadata {
604
- export interface Entry {
605
- key: Metadata;
606
- value: Metadata;
607
- }
608
- }
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 { MetadataResolved } from "./MetadataResolved";
16
+ import { MetadataTuple } from "./MetadataTuple";
17
+
18
+ export class Metadata {
19
+ public any: boolean;
20
+ public required: boolean;
21
+ public optional: boolean;
22
+ public nullable: boolean;
23
+ public functional: boolean;
24
+
25
+ public resolved: MetadataResolved | null;
26
+ public atomics: Atomic.Literal[];
27
+ public constants: MetadataConstant[];
28
+ public templates: Metadata[][];
29
+
30
+ public rest: Metadata | null;
31
+ public aliases: MetadataAlias[];
32
+ public arrays: MetadataArray[];
33
+ public tuples: MetadataTuple[];
34
+ public objects: MetadataObject[];
35
+
36
+ public natives: string[];
37
+ public sets: Metadata[];
38
+ public maps: Metadata.Entry[];
39
+
40
+ /** @internal */ private name_?: string;
41
+ /** @internal */ private parent_resolved_: boolean = false;
42
+ /** @internal */ public union_index?: number;
43
+
44
+ /* -----------------------------------------------------------
45
+ CONSTRUCTORS
46
+ ----------------------------------------------------------- */
47
+ /**
48
+ * @hidden
49
+ */
50
+ private constructor(props: ClassProperties<Metadata>) {
51
+ this.any = props.any;
52
+ this.required = props.required;
53
+ this.optional = props.optional;
54
+ this.nullable = props.nullable;
55
+ this.functional = props.functional;
56
+
57
+ this.resolved = props.resolved;
58
+ this.atomics = props.atomics;
59
+ this.constants = props.constants;
60
+ this.templates = props.templates;
61
+
62
+ this.rest = props.rest;
63
+ this.arrays = props.arrays;
64
+ this.tuples = props.tuples;
65
+ this.objects = props.objects;
66
+ this.aliases = props.aliases;
67
+
68
+ this.natives = props.natives;
69
+ this.sets = props.sets;
70
+ this.maps = props.maps;
71
+ }
72
+
73
+ /**
74
+ * @internal
75
+ */
76
+ public static create(props: ClassProperties<Metadata>): Metadata {
77
+ return new Metadata(props);
78
+ }
79
+
80
+ /**
81
+ * @internal
82
+ */
83
+ public static initialize(parentResolved: boolean = false): Metadata {
84
+ const meta: Metadata = this.create({
85
+ any: false,
86
+ nullable: false,
87
+ required: true,
88
+ optional: false,
89
+ functional: false,
90
+
91
+ resolved: null,
92
+ constants: [],
93
+ atomics: [],
94
+ templates: [],
95
+ arrays: [],
96
+ tuples: [],
97
+ objects: [],
98
+ aliases: [],
99
+
100
+ rest: null,
101
+ natives: [],
102
+ sets: [],
103
+ maps: [],
104
+ });
105
+ meta.parent_resolved_ = parentResolved;
106
+ return meta;
107
+ }
108
+
109
+ public toJSON(): IMetadata {
110
+ return {
111
+ any: this.any,
112
+ required: this.required,
113
+ optional: this.optional,
114
+ nullable: this.nullable,
115
+ functional: this.functional,
116
+
117
+ atomics: this.atomics.slice(),
118
+ constants: JSON.parse(JSON.stringify(this.constants)),
119
+ templates: this.templates.map((tpl) =>
120
+ tpl.map((meta) => meta.toJSON()),
121
+ ),
122
+ resolved: this.resolved ? this.resolved.toJSON() : null,
123
+
124
+ rest: this.rest ? this.rest.toJSON() : null,
125
+ arrays: this.arrays.map((array) => array.name),
126
+ tuples: this.tuples.map((tuple) => tuple.name),
127
+ objects: this.objects.map((obj) => obj.name),
128
+ aliases: this.aliases.map((alias) => alias.name),
129
+
130
+ natives: this.natives.slice(),
131
+ sets: this.sets.map((meta) => meta.toJSON()),
132
+ maps: this.maps.map((entry) => ({
133
+ key: entry.key.toJSON(),
134
+ value: entry.value.toJSON(),
135
+ })),
136
+ };
137
+ }
138
+
139
+ public static from(
140
+ meta: IMetadata,
141
+ collection: IMetadataCollection,
142
+ ): Metadata {
143
+ const dict: IMetadataDictionary = {
144
+ objects: new Map(
145
+ collection.objects.map((obj) => [
146
+ obj.name,
147
+ MetadataObject._From_without_properties(obj),
148
+ ]),
149
+ ),
150
+ aliases: new Map(
151
+ collection.aliases.map((alias) => [
152
+ alias.name,
153
+ MetadataAlias._From_without_value(alias),
154
+ ]),
155
+ ),
156
+ arrays: new Map(
157
+ collection.arrays.map((arr) => [
158
+ arr.name,
159
+ MetadataArray._From_without_value(arr),
160
+ ]),
161
+ ),
162
+ tuples: new Map(
163
+ collection.tuples.map((tpl) => [
164
+ tpl.name,
165
+ MetadataTuple._From_without_elements(tpl),
166
+ ]),
167
+ ),
168
+ };
169
+
170
+ for (const obj of collection.objects) {
171
+ const initialized = dict.objects.get(obj.name)!;
172
+ initialized.properties.push(
173
+ ...obj.properties.map((prop) =>
174
+ MetadataProperty._From(prop, dict),
175
+ ),
176
+ );
177
+ }
178
+ for (const alias of collection.aliases)
179
+ Writable(dict.aliases.get(alias.name)!).value = this._From(
180
+ alias.value,
181
+ dict,
182
+ );
183
+ for (const array of collection.arrays)
184
+ Writable(dict.arrays.get(array.name)!).value = this._From(
185
+ array.value,
186
+ dict,
187
+ );
188
+ for (const tuple of collection.tuples)
189
+ Writable(dict.tuples.get(tuple.name)!).elements =
190
+ tuple.elements.map((elem) => this._From(elem, dict));
191
+
192
+ return this._From(meta, dict);
193
+ }
194
+
195
+ /**
196
+ * @internal
197
+ */
198
+ public static _From(meta: IMetadata, dict: IMetadataDictionary): Metadata {
199
+ return this.create({
200
+ any: meta.any,
201
+ required: meta.required,
202
+ optional: meta.optional,
203
+ nullable: meta.nullable,
204
+ functional: meta.functional,
205
+
206
+ constants: JSON.parse(JSON.stringify(meta.constants)),
207
+ atomics: meta.atomics.slice(),
208
+ templates: meta.templates.map((tpl) =>
209
+ tpl.map((meta) => this._From(meta, dict)),
210
+ ),
211
+ resolved: meta.resolved
212
+ ? MetadataResolved._From(meta.resolved, dict)
213
+ : null,
214
+
215
+ rest: meta.rest ? this._From(meta.rest, dict) : null,
216
+ arrays: meta.arrays.map((id) => {
217
+ const array = dict.arrays.get(id);
218
+ if (array === undefined)
219
+ throw new Error(
220
+ `Error on Metadata.from(): failed to find array "${id}".`,
221
+ );
222
+ return array;
223
+ }),
224
+ tuples: meta.tuples.map((id) => {
225
+ const tuple = dict.tuples.get(id);
226
+ if (tuple === undefined)
227
+ throw new Error(
228
+ `Error on Metadata.from(): failed to find tuple "${id}".`,
229
+ );
230
+ return tuple;
231
+ }),
232
+ objects: meta.objects.map((name) => {
233
+ const found = dict.objects.get(name);
234
+ if (found === undefined)
235
+ throw new Error(
236
+ `Error on Metadata.from(): failed to find object "${name}".`,
237
+ );
238
+ return found;
239
+ }),
240
+ aliases: meta.aliases.map((alias) => {
241
+ const found = dict.aliases.get(alias);
242
+ if (found === undefined)
243
+ throw new Error(
244
+ `Error on Metadata.from(): failed to find alias "${alias}".`,
245
+ );
246
+ return found;
247
+ }),
248
+
249
+ natives: meta.natives.slice(),
250
+ sets: meta.sets.map((meta) => this._From(meta, dict)),
251
+ maps: meta.maps.map((entry) => ({
252
+ key: this._From(entry.key, dict),
253
+ value: this._From(entry.value, dict),
254
+ })),
255
+ });
256
+ }
257
+
258
+ /* -----------------------------------------------------------
259
+ ACCESSORS
260
+ ----------------------------------------------------------- */
261
+ public getName(): string {
262
+ this.name_ ??= getName(this);
263
+ return this.name_;
264
+ }
265
+
266
+ public empty(): boolean {
267
+ return this.bucket() === 0 || this.size() === 0;
268
+ }
269
+
270
+ public size(): number {
271
+ return (
272
+ (this.any ? 1 : 0) +
273
+ (this.resolved ? 1 : 0) +
274
+ (this.functional ? 1 : 0) +
275
+ (this.rest ? this.rest.size() : 0) +
276
+ this.templates.length +
277
+ this.atomics.length +
278
+ this.constants
279
+ .map((c) => c.values.length)
280
+ .reduce((x, y) => x + y, 0) +
281
+ this.arrays.length +
282
+ this.tuples.length +
283
+ this.natives.length +
284
+ this.maps.length +
285
+ this.sets.length +
286
+ this.objects.length +
287
+ this.aliases.length
288
+ );
289
+ }
290
+
291
+ public bucket(): number {
292
+ return (
293
+ (this.any ? 1 : 0) +
294
+ (this.resolved ? 1 : 0) +
295
+ (this.functional ? 1 : 0) +
296
+ (this.templates.length ? 1 : 0) +
297
+ (this.atomics.length ? 1 : 0) +
298
+ (this.constants.length ? 1 : 0) +
299
+ (this.rest ? this.rest.size() : 0) +
300
+ (this.arrays.length ? 1 : 0) +
301
+ (this.tuples.length ? 1 : 0) +
302
+ (this.natives.length ? 1 : 0) +
303
+ (this.sets.length ? 1 : 0) +
304
+ (this.maps.length ? 1 : 0) +
305
+ (this.objects.length ? 1 : 0) +
306
+ (this.aliases.length ? 1 : 0)
307
+ );
308
+ }
309
+
310
+ public isConstant(): boolean {
311
+ return this.bucket() === (this.constants.length ? 1 : 0);
312
+ }
313
+
314
+ public isRequired(): boolean {
315
+ return this.required === true && this.optional === false;
316
+ }
317
+
318
+ /**
319
+ * @internal
320
+ */
321
+ public isUnionBucket(): boolean {
322
+ const size: number = this.bucket();
323
+ const emended: number = this.constants.length ? size - 1 : size;
324
+ return emended > 1;
325
+ }
326
+
327
+ /**
328
+ * @internal
329
+ */
330
+ public getSoleLiteral(): string | null {
331
+ if (
332
+ this.size() === 1 &&
333
+ this.constants.length === 1 &&
334
+ this.constants[0]!.type === "string" &&
335
+ this.constants[0]!.values.length === 1
336
+ )
337
+ return this.constants[0]!.values[0] as string;
338
+ else return null;
339
+ }
340
+
341
+ public isSoleLiteral(): boolean {
342
+ return this.getSoleLiteral() !== null;
343
+ }
344
+
345
+ /**
346
+ * @internal
347
+ */
348
+ public isParentResolved(): boolean {
349
+ return this.parent_resolved_;
350
+ }
351
+ }
352
+ export namespace Metadata {
353
+ export const intersects = (x: Metadata, y: Metadata): boolean => {
354
+ // CHECK ANY & OPTIONAL
355
+ if (x.any || y.any) return true;
356
+ if (x.isRequired() === false && false === y.isRequired()) return true;
357
+ if (x.nullable === true && true === y.nullable) return true;
358
+ if (x.functional === true && y.functional === true) return true;
359
+
360
+ //----
361
+ // INSTANCES
362
+ //----
363
+ // ARRAYS
364
+ if (x.arrays.length && y.arrays.length) return true;
365
+ if (x.tuples.length && y.tuples.length) return true;
366
+ if (x.objects.length && y.objects.length) return true;
367
+ if (x.aliases.length && y.aliases.length) return true;
368
+
369
+ // NATIVES
370
+ if (x.natives.length && y.natives.length)
371
+ if (x.natives.some((xn) => y.natives.some((yn) => xn === yn)))
372
+ return true;
373
+
374
+ //----
375
+ // VALUES
376
+ //----
377
+ // ATOMICS
378
+ for (const atomic of x.atomics)
379
+ if (y.atomics.includes(atomic)) return true;
380
+
381
+ // CONSTANTS
382
+ for (const constant of x.constants) {
383
+ const opposite: MetadataConstant | undefined = y.constants.find(
384
+ (elem) => elem.type === constant.type,
385
+ );
386
+ if (opposite === undefined) continue;
387
+
388
+ const values: Set<any> = new Set([
389
+ ...constant.values,
390
+ ...opposite.values,
391
+ ]);
392
+ if (values.size !== constant.values.length + opposite.values.length)
393
+ return true;
394
+ }
395
+ return false;
396
+ };
397
+
398
+ export const covers = (
399
+ x: Metadata,
400
+ y: Metadata,
401
+ level: number = 0,
402
+ ): boolean => {
403
+ // CHECK ANY
404
+ if (x === y) return false;
405
+ else if (x.any) return true;
406
+ else if (y.any) return false;
407
+
408
+ //----
409
+ // INSTANCES
410
+ //----
411
+ if (level === 0) {
412
+ // ARRAYS
413
+ for (const ya of y.arrays)
414
+ if (
415
+ !x.arrays.some((xa) =>
416
+ covers(xa.value, ya.value, level + 1),
417
+ )
418
+ ) {
419
+ return false;
420
+ }
421
+
422
+ // TUPLES
423
+ for (const yt of y.tuples)
424
+ if (
425
+ yt.elements.length !== 0 &&
426
+ x.tuples.some(
427
+ (xt) =>
428
+ xt.elements.length >= yt.elements.length &&
429
+ xt.elements
430
+ .slice(yt.elements.length)
431
+ .every((xv, i) =>
432
+ covers(xv, yt.elements[i]!, level + 1),
433
+ ),
434
+ ) === false
435
+ )
436
+ return false;
437
+ }
438
+
439
+ // OBJECTS
440
+ for (const yo of y.objects)
441
+ if (x.objects.some((xo) => MetadataObject.covers(xo, yo)) === false)
442
+ return false;
443
+
444
+ // ALIASES
445
+ for (const yd of y.aliases)
446
+ if (x.aliases.some((xd) => xd.name === yd.name) === false)
447
+ return false;
448
+
449
+ // NATIVES
450
+ for (const yn of y.natives)
451
+ if (x.natives.some((xn) => xn === yn) === false) return false;
452
+
453
+ // SETS
454
+ for (const ys of y.sets)
455
+ if (x.sets.some((xs) => covers(xs, ys)) === false) return false;
456
+
457
+ //----
458
+ // VALUES
459
+ //----
460
+ // ATOMICS
461
+ if (y.atomics.some((atomic) => x.atomics.includes(atomic) === false))
462
+ return false;
463
+
464
+ // CONSTANTS
465
+ for (const yc of y.constants) {
466
+ if (x.atomics.some((type) => yc.type === type)) continue;
467
+ const xc: MetadataConstant | undefined = x.constants.find(
468
+ (elem) => elem.type === yc.type,
469
+ );
470
+ if (xc === undefined) return false;
471
+ else if (
472
+ (yc.values as number[]).some(
473
+ (yv) => xc.values.includes(yv as never) === false,
474
+ )
475
+ )
476
+ return false;
477
+ }
478
+
479
+ // FUNCTIONAL
480
+ if (x.functional === false && y.functional) return false;
481
+
482
+ // SUCCESS
483
+ return true;
484
+ };
485
+
486
+ /**
487
+ * @internal
488
+ */
489
+ export const merge = (x: Metadata, y: Metadata): Metadata => {
490
+ const output: Metadata = Metadata.create({
491
+ any: x.any || y.any,
492
+ nullable: x.nullable || y.nullable,
493
+ required: x.required && y.required,
494
+ optional: x.optional || y.optional,
495
+ functional: x.functional || y.functional,
496
+
497
+ resolved:
498
+ x.resolved !== null && y.resolved !== null
499
+ ? //? merge(x.resolved, y.resolved)
500
+ MetadataResolved.create({
501
+ original: merge(
502
+ x.resolved.original,
503
+ y.resolved.original,
504
+ ),
505
+ returns: merge(
506
+ x.resolved.returns,
507
+ y.resolved.returns,
508
+ ),
509
+ })
510
+ : x.resolved ?? y.resolved,
511
+ atomics: [...new Set([...x.atomics, ...y.atomics])],
512
+ constants: [...x.constants],
513
+ templates: x.templates.slice(),
514
+
515
+ rest:
516
+ x.rest !== null && y.rest !== null
517
+ ? merge(x.rest, y.rest)
518
+ : x.rest ?? y.rest,
519
+ arrays: x.arrays.slice(),
520
+ tuples: x.tuples.slice(),
521
+ objects: x.objects.slice(),
522
+ aliases: x.aliases.slice(),
523
+
524
+ natives: [...new Set([...x.natives, ...y.natives])],
525
+ sets: x.sets.slice(),
526
+ maps: x.maps.slice(),
527
+ });
528
+ for (const constant of y.constants) {
529
+ const target: MetadataConstant = ArrayUtil.take(
530
+ output.constants,
531
+ (elem) => elem.type === constant.type,
532
+ () => ({
533
+ type: constant.type,
534
+ values: [],
535
+ }),
536
+ );
537
+ for (const value of constant.values)
538
+ ArrayUtil.add(target.values, value);
539
+ }
540
+ for (const array of y.arrays)
541
+ ArrayUtil.set(output.arrays, array, (elem) => elem.name);
542
+ for (const tuple of y.tuples)
543
+ ArrayUtil.set(output.tuples, tuple, (elem) => elem.name);
544
+ for (const obj of y.objects)
545
+ ArrayUtil.set(output.objects, obj, (elem) => elem.name);
546
+ for (const alias of y.aliases)
547
+ ArrayUtil.set(output.aliases, alias, (elem) => elem.name);
548
+
549
+ return output;
550
+ };
551
+ }
552
+
553
+ const getName = (metadata: Metadata): string => {
554
+ if (metadata.any === true) return "any";
555
+
556
+ const elements: string[] = [];
557
+
558
+ // OPTIONAL
559
+ if (metadata.nullable === true) elements.push("null");
560
+ if (metadata.isRequired() === false) elements.push("undefined");
561
+
562
+ // ATOMIC
563
+ for (const type of metadata.atomics) {
564
+ elements.push(type);
565
+ }
566
+ for (const constant of metadata.constants)
567
+ for (const value of constant.values)
568
+ elements.push(JSON.stringify(value));
569
+ for (const template of metadata.templates)
570
+ elements.push(
571
+ "`" +
572
+ template
573
+ .map((child) =>
574
+ child.isConstant() && child.size() === 1
575
+ ? child.constants[0]!.values[0]!
576
+ : `$\{${child.getName()}\}`,
577
+ )
578
+ .join("")
579
+ .split("`")
580
+ .join("\\`") +
581
+ "`",
582
+ );
583
+
584
+ // NATIVES
585
+ for (const native of metadata.natives) elements.push(native);
586
+ for (const set of metadata.sets) elements.push(`Set<${set.getName()}>`);
587
+ for (const map of metadata.maps)
588
+ elements.push(`Map<${map.key.getName()}, ${map.value.getName()}>`);
589
+
590
+ // INSTANCES
591
+ if (metadata.rest !== null) elements.push(`...${metadata.rest.getName()}`);
592
+ for (const tuple of metadata.tuples) elements.push(tuple.name);
593
+ for (const array of metadata.arrays) elements.push(array.name);
594
+ for (const object of metadata.objects) elements.push(object.name);
595
+ for (const alias of metadata.aliases) elements.push(alias.name);
596
+ if (metadata.resolved !== null) elements.push(metadata.resolved.getName());
597
+
598
+ // RETURNS
599
+ if (elements.length === 0) return "unknown";
600
+ else if (elements.length === 1) return elements[0]!;
601
+
602
+ elements.sort();
603
+ return `(${elements.join(" | ")})`;
604
+ };
605
+ export namespace Metadata {
606
+ export interface Entry {
607
+ key: Metadata;
608
+ value: Metadata;
609
+ }
610
+ }