typia 5.0.0-dev.20230821 → 5.0.0-dev.20230822

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