@fgv/ts-res 5.0.0-21 → 5.0.0-23

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 (197) hide show
  1. package/README.md +794 -137
  2. package/dist/ts-res.d.ts +506 -33
  3. package/lib/packlets/common/convert.d.ts +10 -0
  4. package/lib/packlets/common/convert.js +11 -1
  5. package/lib/packlets/common/resources.d.ts +40 -1
  6. package/lib/packlets/common/validate/regularExpressions.d.ts +4 -0
  7. package/lib/packlets/common/validate/regularExpressions.js +5 -1
  8. package/lib/packlets/common/validate/resources.d.ts +35 -1
  9. package/lib/packlets/common/validate/resources.js +54 -0
  10. package/lib/packlets/config/convert.js +1 -1
  11. package/lib/packlets/config/json.d.ts +1 -1
  12. package/lib/packlets/qualifier-types/config/convert.d.ts +22 -1
  13. package/lib/packlets/qualifier-types/config/convert.js +67 -2
  14. package/lib/packlets/qualifier-types/config/json.d.ts +11 -2
  15. package/lib/packlets/qualifier-types/helpers.d.ts +1 -2
  16. package/lib/packlets/qualifier-types/languageQualifierType.d.ts +27 -1
  17. package/lib/packlets/qualifier-types/languageQualifierType.js +75 -0
  18. package/lib/packlets/qualifier-types/literalQualifierType.d.ts +30 -4
  19. package/lib/packlets/qualifier-types/literalQualifierType.js +77 -0
  20. package/lib/packlets/qualifier-types/literalValueHierarchy.d.ts +6 -10
  21. package/lib/packlets/qualifier-types/literalValueHierarchy.js +10 -0
  22. package/lib/packlets/qualifier-types/qualifierType.d.ts +28 -0
  23. package/lib/packlets/qualifier-types/territoryQualifierType.d.ts +28 -4
  24. package/lib/packlets/qualifier-types/territoryQualifierType.js +77 -0
  25. package/lib/packlets/resource-json/compiled/convert.js +2 -1
  26. package/lib/packlets/resource-json/compiled/json.d.ts +6 -2
  27. package/lib/packlets/resource-types/config/convert.js +3 -1
  28. package/lib/packlets/resource-types/config/json.d.ts +2 -0
  29. package/lib/packlets/resource-types/jsonResourceType.d.ts +10 -6
  30. package/lib/packlets/resource-types/jsonResourceType.js +7 -10
  31. package/lib/packlets/resource-types/resourceType.d.ts +20 -8
  32. package/lib/packlets/resource-types/resourceType.js +20 -16
  33. package/lib/packlets/resources/candidateValue.d.ts +92 -0
  34. package/lib/packlets/resources/candidateValue.js +128 -0
  35. package/lib/packlets/resources/candidateValueCollector.d.ts +55 -0
  36. package/lib/packlets/resources/candidateValueCollector.js +96 -0
  37. package/lib/packlets/resources/index.d.ts +2 -0
  38. package/lib/packlets/resources/index.js +2 -0
  39. package/lib/packlets/resources/resource.js +2 -2
  40. package/lib/packlets/resources/resourceBuilder.d.ts +6 -0
  41. package/lib/packlets/resources/resourceBuilder.js +3 -1
  42. package/lib/packlets/resources/resourceCandidate.d.ts +10 -1
  43. package/lib/packlets/resources/resourceCandidate.js +11 -5
  44. package/lib/packlets/resources/resourceManagerBuilder.d.ts +7 -0
  45. package/lib/packlets/resources/resourceManagerBuilder.js +13 -6
  46. package/lib/packlets/runtime/compiledResourceCollection.d.ts +20 -8
  47. package/lib/packlets/runtime/compiledResourceCollection.js +25 -4
  48. package/lib/packlets/runtime/resource-tree/common.d.ts +1 -1
  49. package/lib/packlets/runtime/resourceResolver.d.ts +39 -1
  50. package/lib/packlets/runtime/resourceResolver.js +45 -28
  51. package/lib/test/unit/qualifier-types/testQualifierType.d.ts +4 -0
  52. package/lib/test/unit/qualifier-types/testQualifierType.js +24 -0
  53. package/lib/test/unit/resource-types/derivedResourceType.test.d.ts +2 -0
  54. package/lib/test/unit/resource-types/testDerivedResourceType.d.ts +79 -0
  55. package/lib/test/unit/resource-types/testDerivedResourceType.js +113 -0
  56. package/lib/test/unit/resources/candidateValue.test.d.ts +2 -0
  57. package/lib/test/unit/resources/candidateValueCollector.test.d.ts +2 -0
  58. package/lib/test/unit/runtime/resourceResolver.coverage.test.d.ts +2 -0
  59. package/lib/test/unit/runtime/resourceResolver.newFeatures.test.d.ts +2 -0
  60. package/package.json +7 -7
  61. package/src/index.ts +0 -75
  62. package/src/packlets/bundle/bundleBuilder.ts +0 -159
  63. package/src/packlets/bundle/bundleLoader.ts +0 -157
  64. package/src/packlets/bundle/bundleNormalizer.ts +0 -173
  65. package/src/packlets/bundle/bundleUtils.ts +0 -142
  66. package/src/packlets/bundle/convert.ts +0 -87
  67. package/src/packlets/bundle/index.ts +0 -52
  68. package/src/packlets/bundle/model.ts +0 -138
  69. package/src/packlets/common/conditions.ts +0 -226
  70. package/src/packlets/common/convert.ts +0 -184
  71. package/src/packlets/common/helpers/conditions.ts +0 -84
  72. package/src/packlets/common/helpers/context.ts +0 -88
  73. package/src/packlets/common/helpers/index.ts +0 -26
  74. package/src/packlets/common/helpers/qualifierDefaultValues.ts +0 -91
  75. package/src/packlets/common/helpers/resources.ts +0 -96
  76. package/src/packlets/common/index.ts +0 -30
  77. package/src/packlets/common/resources.ts +0 -78
  78. package/src/packlets/common/validate/conditions.ts +0 -595
  79. package/src/packlets/common/validate/index.ts +0 -27
  80. package/src/packlets/common/validate/regularExpressions.ts +0 -92
  81. package/src/packlets/common/validate/resources.ts +0 -170
  82. package/src/packlets/conditions/condition.ts +0 -351
  83. package/src/packlets/conditions/conditionCollector.ts +0 -103
  84. package/src/packlets/conditions/conditionDecls.ts +0 -50
  85. package/src/packlets/conditions/conditionSet.ts +0 -318
  86. package/src/packlets/conditions/conditionSetCollector.ts +0 -127
  87. package/src/packlets/conditions/conditionSetDecls.ts +0 -41
  88. package/src/packlets/conditions/conditionToken.ts +0 -180
  89. package/src/packlets/conditions/convert/conditionSetDecls.ts +0 -72
  90. package/src/packlets/conditions/convert/decls.ts +0 -95
  91. package/src/packlets/conditions/convert/index.ts +0 -24
  92. package/src/packlets/conditions/index.ts +0 -33
  93. package/src/packlets/config/common.ts +0 -107
  94. package/src/packlets/config/configInitFactory.ts +0 -142
  95. package/src/packlets/config/convert.ts +0 -61
  96. package/src/packlets/config/index.ts +0 -30
  97. package/src/packlets/config/json.ts +0 -43
  98. package/src/packlets/config/predefined/default.ts +0 -150
  99. package/src/packlets/config/predefined/extended.ts +0 -198
  100. package/src/packlets/config/predefined/index.ts +0 -26
  101. package/src/packlets/config/systemConfiguration.ts +0 -217
  102. package/src/packlets/context/contextDecls.ts +0 -76
  103. package/src/packlets/context/contextToken.ts +0 -252
  104. package/src/packlets/context/convert/decls.ts +0 -126
  105. package/src/packlets/context/convert/index.ts +0 -23
  106. package/src/packlets/context/index.ts +0 -27
  107. package/src/packlets/decisions/abstractDecision.ts +0 -87
  108. package/src/packlets/decisions/abstractDecisionCollector.ts +0 -122
  109. package/src/packlets/decisions/candidate.ts +0 -101
  110. package/src/packlets/decisions/common.ts +0 -37
  111. package/src/packlets/decisions/concreteDecision.ts +0 -157
  112. package/src/packlets/decisions/decision.ts +0 -172
  113. package/src/packlets/decisions/index.ts +0 -28
  114. package/src/packlets/import/fsItem.ts +0 -212
  115. package/src/packlets/import/importContext.ts +0 -183
  116. package/src/packlets/import/importManager.ts +0 -187
  117. package/src/packlets/import/importable.ts +0 -107
  118. package/src/packlets/import/importers/collectionImporter.ts +0 -153
  119. package/src/packlets/import/importers/fsItemImporter.ts +0 -154
  120. package/src/packlets/import/importers/importer.ts +0 -58
  121. package/src/packlets/import/importers/index.ts +0 -27
  122. package/src/packlets/import/importers/jsonImporter.ts +0 -133
  123. package/src/packlets/import/importers/pathImporter.ts +0 -134
  124. package/src/packlets/import/index.ts +0 -29
  125. package/src/packlets/qualifier-types/config/convert.ts +0 -108
  126. package/src/packlets/qualifier-types/config/index.ts +0 -27
  127. package/src/packlets/qualifier-types/config/json.ts +0 -126
  128. package/src/packlets/qualifier-types/convert.ts +0 -77
  129. package/src/packlets/qualifier-types/helpers.ts +0 -94
  130. package/src/packlets/qualifier-types/index.ts +0 -34
  131. package/src/packlets/qualifier-types/languageQualifierType.ts +0 -143
  132. package/src/packlets/qualifier-types/literalQualifierType.ts +0 -244
  133. package/src/packlets/qualifier-types/literalValueHierarchy.ts +0 -332
  134. package/src/packlets/qualifier-types/qualifierType.ts +0 -360
  135. package/src/packlets/qualifier-types/qualifierTypeCollector.ts +0 -102
  136. package/src/packlets/qualifier-types/territoryQualifierType.ts +0 -255
  137. package/src/packlets/qualifiers/convert/decls.ts +0 -90
  138. package/src/packlets/qualifiers/convert/index.ts +0 -24
  139. package/src/packlets/qualifiers/convert/qualifier.ts +0 -55
  140. package/src/packlets/qualifiers/index.ts +0 -31
  141. package/src/packlets/qualifiers/qualifier.ts +0 -169
  142. package/src/packlets/qualifiers/qualifierCollector.ts +0 -194
  143. package/src/packlets/qualifiers/qualifierDecl.ts +0 -80
  144. package/src/packlets/qualifiers/qualifierDefaultValueDecls.ts +0 -57
  145. package/src/packlets/qualifiers/qualifierDefaultValueToken.ts +0 -230
  146. package/src/packlets/resource-json/compiled/common.ts +0 -35
  147. package/src/packlets/resource-json/compiled/convert.ts +0 -143
  148. package/src/packlets/resource-json/compiled/index.ts +0 -27
  149. package/src/packlets/resource-json/compiled/json.ts +0 -239
  150. package/src/packlets/resource-json/convert.ts +0 -236
  151. package/src/packlets/resource-json/helpers.ts +0 -284
  152. package/src/packlets/resource-json/index.ts +0 -33
  153. package/src/packlets/resource-json/json.ts +0 -295
  154. package/src/packlets/resource-json/normalized.ts +0 -211
  155. package/src/packlets/resource-json/resourceDeclCollection.ts +0 -117
  156. package/src/packlets/resource-json/resourceDeclContainer.ts +0 -48
  157. package/src/packlets/resource-json/resourceDeclTree.ts +0 -116
  158. package/src/packlets/resource-types/config/convert.ts +0 -36
  159. package/src/packlets/resource-types/config/index.ts +0 -27
  160. package/src/packlets/resource-types/config/json.ts +0 -30
  161. package/src/packlets/resource-types/helpers.ts +0 -44
  162. package/src/packlets/resource-types/index.ts +0 -30
  163. package/src/packlets/resource-types/jsonResourceType.ts +0 -104
  164. package/src/packlets/resource-types/resourceType.ts +0 -259
  165. package/src/packlets/resource-types/resourceTypeCollector.ts +0 -79
  166. package/src/packlets/resources/candidateReducer.ts +0 -331
  167. package/src/packlets/resources/common.ts +0 -114
  168. package/src/packlets/resources/index.ts +0 -28
  169. package/src/packlets/resources/resource.ts +0 -307
  170. package/src/packlets/resources/resourceBuilder.ts +0 -269
  171. package/src/packlets/resources/resourceCandidate.ts +0 -333
  172. package/src/packlets/resources/resourceManagerBuilder.ts +0 -1169
  173. package/src/packlets/runtime/cacheListener.ts +0 -113
  174. package/src/packlets/runtime/cacheMetrics.ts +0 -206
  175. package/src/packlets/runtime/compiledResourceCollection.ts +0 -607
  176. package/src/packlets/runtime/conditionSetResolutionResult.ts +0 -160
  177. package/src/packlets/runtime/context/contextQualifierProvider.ts +0 -117
  178. package/src/packlets/runtime/context/contextQualifierProviderValidator.ts +0 -281
  179. package/src/packlets/runtime/context/index.ts +0 -26
  180. package/src/packlets/runtime/context/simpleContextQualifierProvider.ts +0 -225
  181. package/src/packlets/runtime/context/validatingSimpleContextQualifierProvider.ts +0 -96
  182. package/src/packlets/runtime/iResourceManager.ts +0 -130
  183. package/src/packlets/runtime/index.ts +0 -35
  184. package/src/packlets/runtime/resource-tree/common.ts +0 -126
  185. package/src/packlets/runtime/resource-tree/index.ts +0 -26
  186. package/src/packlets/runtime/resource-tree/readOnlyResourceTree.ts +0 -342
  187. package/src/packlets/runtime/resource-tree/resourceTreeChildren.ts +0 -125
  188. package/src/packlets/runtime/resource-tree/resourceTreeChildrenValidator.ts +0 -187
  189. package/src/packlets/runtime/resourceResolver.ts +0 -626
  190. package/src/packlets/runtime/validate.ts +0 -63
  191. package/src/packlets/zip-archive/convert.ts +0 -121
  192. package/src/packlets/zip-archive/index.ts +0 -76
  193. package/src/packlets/zip-archive/json.ts +0 -91
  194. package/src/packlets/zip-archive/types.ts +0 -140
  195. package/src/packlets/zip-archive/zipArchiveCreator.ts +0 -229
  196. package/src/packlets/zip-archive/zipArchiveFormat.ts +0 -158
  197. package/src/packlets/zip-archive/zipArchiveLoader.ts +0 -370
@@ -1,332 +0,0 @@
1
- /*
2
- * Copyright (c) 2025 Erik Fortune
3
- *
4
- * Permission is hereby granted, free of charge, to any person obtaining a copy
5
- * of this software and associated documentation files (the "Software"), to deal
6
- * in the Software without restriction, including without limitation the rights
7
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- * copies of the Software, and to permit persons to whom the Software is
9
- * furnished to do so, subject to the following conditions:
10
- *
11
- * The above copyright notice and this permission notice shall be included in all
12
- * copies or substantial portions of the Software.
13
- *
14
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
- * SOFTWARE.
21
- */
22
-
23
- import { captureResult, MessageAggregator, Result, succeed, fail } from '@fgv/ts-utils';
24
- import {
25
- ConditionOperator,
26
- NoMatch,
27
- PerfectMatch,
28
- QualifierConditionValue,
29
- QualifierContextValue,
30
- QualifierMatchScore,
31
- Convert as CommonConverters
32
- } from '../common';
33
-
34
- /**
35
- * Declares a hierarchy of literal values. The keys are the names of the values, and the
36
- * values are the names of their parents.
37
- * @remarks
38
- * The hierarchy is defined as a tree, where each value can have multiple children but
39
- * only one parent. The root of the tree has no parent. The hierarchy is used to
40
- * determine the relationship between values when matching conditions and contexts.
41
- * @public
42
- */
43
- export type LiteralValueHierarchyDecl<T extends string> = Partial<Record<T, T>>;
44
-
45
- /**
46
- * Describes a single valid literal value including optional parent and child values.
47
- * @public
48
- */
49
- export interface ILiteralValue<T extends string> {
50
- readonly name: T;
51
- readonly parent?: ILiteralValue<T>;
52
- readonly children?: ReadonlyArray<T>;
53
- }
54
-
55
- /**
56
- * Describes the parameters used to create a new
57
- * {@link QualifierTypes.LiteralValueHierarchy | LiteralValueHierarchy}.
58
- * @public
59
- */
60
- export interface ILiteralValueHierarchyCreateParams<T extends string = string> {
61
- values: ReadonlyArray<T>;
62
- hierarchy?: LiteralValueHierarchyDecl<T>;
63
- }
64
-
65
- /**
66
- * Internal interface used during hierarchy construction to build
67
- * {@link QualifierTypes.ILiteralValue | ILiteralValue} objects.
68
- * @public
69
- */
70
- interface ILiteralValueBuilder<T extends string> {
71
- readonly name: T;
72
- parent?: ILiteralValue<T>;
73
- children?: T[];
74
- }
75
-
76
- /**
77
- * A class that implements a hierarchy of literal values. The hierarchy is defined as a
78
- * tree, where each value can have multiple children but only one parent. The root of the
79
- * tree has no parent. The hierarchy is used to determine the relationship between values
80
- * when matching conditions and contexts.
81
- *
82
- * @remarks
83
- * The hierarchy can be created in two modes:
84
- * - **Constrained mode**: When enumerated values are provided, only those values are allowed
85
- * in the hierarchy and matching operations.
86
- * - **Open values mode**: When no enumerated values are provided, all values referenced in
87
- * the hierarchy are automatically collected and used. This allows for flexible hierarchies
88
- * where any value can be used in matching operations.
89
- * @public
90
- */
91
- export class LiteralValueHierarchy<T extends string = string> {
92
- /**
93
- * A map of all allowed literal values to the corresponding
94
- * {@link QualifierTypes.ILiteralValue | ILiteralValue} validated definition.
95
- */
96
- public readonly values: ReadonlyMap<T, ILiteralValue<T>>;
97
-
98
- /**
99
- * Indicates whether this hierarchy was created with open values (no enumerated values
100
- * provided), allowing any value to be used in matching operations.
101
- */
102
- public readonly isOpenValues: boolean;
103
-
104
- protected constructor(params: ILiteralValueHierarchyCreateParams<T>) {
105
- const result = LiteralValueHierarchy._buildValuesFromHierarchy(params);
106
- this.values = result.orThrow();
107
- this.isOpenValues = !params.values || params.values.length === 0;
108
- }
109
-
110
- /**
111
- * Creates a new {@link QualifierTypes.LiteralValueHierarchy | LiteralValueHierarchy} instance.
112
- * @param params - The {@link QualifierTypes.ILiteralValueHierarchyCreateParams | parameters}
113
- * used to create the hierarchy.
114
- * @returns `Success` with the new hierarchy or `Failure` with an error message.
115
- */
116
- public static create<T extends string>(
117
- params: ILiteralValueHierarchyCreateParams<T>
118
- ): Result<LiteralValueHierarchy<T>> {
119
- return captureResult(() => new LiteralValueHierarchy(params));
120
- }
121
-
122
- /**
123
- * Checks if a value exists in the hierarchy.
124
- * @param value - The value to check.
125
- * @returns `true` if the value exists in the hierarchy, `false` otherwise.
126
- */
127
- public hasValue(value: T): boolean {
128
- return this.values.has(value);
129
- }
130
-
131
- /**
132
- * Gets all root values (values with no parent) in the hierarchy.
133
- * @returns `Success` with an array of root values.
134
- */
135
- public getRoots(): Result<T[]> {
136
- return succeed(
137
- Array.from(this.values.values())
138
- .filter((v) => !v.parent)
139
- .map((v) => v.name)
140
- );
141
- }
142
-
143
- /**
144
- * Gets all ancestors of a value in the hierarchy.
145
- * @param value - The value to get ancestors for.
146
- * @returns `Success` with an array of ancestor values, ordered from immediate parent
147
- * to root, or `Failure` if the value is not in the hierarchy.
148
- */
149
- public getAncestors(value: T): Result<T[]> {
150
- const current = this.values.get(value);
151
- /* c8 ignore next 3 - functional error case tested but coverage intermittently missed */
152
- if (!current) {
153
- return fail(`${value}: not found in hierarchy`);
154
- }
155
-
156
- const ancestors: T[] = [];
157
- let ancestor = current.parent;
158
-
159
- while (ancestor) {
160
- ancestors.push(ancestor.name);
161
- ancestor = ancestor.parent;
162
- }
163
-
164
- return succeed(ancestors);
165
- }
166
-
167
- /**
168
- * Determines if a value is an ancestor of a possible ancestor value.
169
- * @param value - The value to check.
170
- * @param possibleAncestor - The possible ancestor value.
171
- * @returns `true` if the value is an ancestor of the possible ancestor, `false` otherwise.
172
- */
173
- public isAncestor(value: T, possibleAncestor: T): boolean {
174
- const ancestors = this.getAncestors(value);
175
- if (ancestors.isSuccess()) {
176
- return ancestors.value.includes(possibleAncestor);
177
- }
178
- return false;
179
- }
180
-
181
- /**
182
- * Gets all descendants of a value in the hierarchy.
183
- * @param value - The value to get descendants for.
184
- * @returns `Success` with an array of descendant values, or `Failure` if the value
185
- * is not in the hierarchy.
186
- */
187
- public getDescendants(value: T): Result<T[]> {
188
- const current = this.values.get(value);
189
- if (!current) {
190
- return fail(`${value}: not found in hierarchy`);
191
- }
192
-
193
- const descendants: T[] = [];
194
- if (current.children) {
195
- for (const childName of current.children) {
196
- descendants.push(childName);
197
- const childDescendants = this.getDescendants(childName);
198
- if (childDescendants.isSuccess()) {
199
- descendants.push(...childDescendants.value);
200
- }
201
- }
202
- }
203
-
204
- return succeed(descendants);
205
- }
206
-
207
- public match(condition: T, context: T): QualifierMatchScore;
208
- /**
209
- * Matches a condition value against a context value, where an exact match of the
210
- * condition and context returns {@link PerfectMatch | PerfectMatch}, a condition
211
- * value that does not match the context value or any of its ancestors returns
212
- * {@link NoMatch | NoMatch}, and a condition value that matches the context value
213
- * or any of its ancestors returns a positive score that is less than
214
- * {@link PerfectMatch | PerfectMatch}, with the score decreasing with each ancestor
215
- * in the hierarchy.
216
- * @param condition - The condition value to match.
217
- * @param context - The context value to match against.
218
- * @param __operator - The operator used for matching (not used in this implementation).
219
- * @returns A {@link QualifierMatchScore | QualifierMatchScore} indicating the match score.
220
- */
221
- public match(
222
- condition: QualifierConditionValue,
223
- context: QualifierContextValue,
224
- __operator?: ConditionOperator
225
- ): QualifierMatchScore;
226
- public match(condition: string, context: string, __operator?: ConditionOperator): QualifierMatchScore {
227
- // For open hierarchies, skip validation and allow any values
228
- if (!this.isOpenValues) {
229
- // Validate that both condition and context exist in the hierarchy
230
- /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */
231
- if (!this.values.has(condition as T)) {
232
- return NoMatch;
233
- }
234
- /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */
235
- if (!this.values.has(context as T)) {
236
- return NoMatch;
237
- }
238
- }
239
-
240
- if ((condition as string) === (context as string)) {
241
- return PerfectMatch;
242
- }
243
-
244
- // For open hierarchies, if values aren't in the hierarchy, treat as no match
245
- // but don't fail validation
246
- const values: ReadonlyMap<string, ILiteralValue<string>> = this.values;
247
- /* c8 ignore next 1 - ? is defense in depth */
248
- let value = values.get(context)?.parent;
249
- /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */
250
- if (!value) {
251
- return NoMatch;
252
- }
253
- let score = PerfectMatch as number;
254
- while (value) {
255
- score *= 0.9;
256
- if (value.name === condition) {
257
- return CommonConverters.qualifierMatchScore.convert(score).orDefault(NoMatch);
258
- }
259
- value = value.parent;
260
- }
261
- return NoMatch;
262
- }
263
-
264
- protected static _buildValuesFromHierarchy<T extends string>(
265
- params: ILiteralValueHierarchyCreateParams<T>
266
- ): Result<ReadonlyMap<T, ILiteralValue<T>>> {
267
- const errors = new MessageAggregator();
268
- const values = params.values;
269
- const hierarchy = params.hierarchy ?? {};
270
-
271
- // If no values are provided, collect all values from the hierarchy
272
- let allValues: T[];
273
- if (!values || values.length === 0) {
274
- const hierarchyValues = new Set<T>();
275
- // Add all keys and values from the hierarchy
276
- Object.keys(hierarchy).forEach((key) => hierarchyValues.add(key as T));
277
- Object.values(hierarchy).forEach((value) => hierarchyValues.add(value as T));
278
- allValues = Array.from(hierarchyValues);
279
- } else {
280
- allValues = [...values];
281
- }
282
-
283
- const valueMap = new Map<T, ILiteralValueBuilder<T>>(allValues.map((name) => [name, { name }]));
284
-
285
- if (hierarchy) {
286
- for (const [valueName, parentName] of Object.entries(hierarchy)) {
287
- const value = valueMap.get(valueName as T);
288
- if (!value) {
289
- errors.addMessage(`${valueName}: invalid literal value.`);
290
- continue;
291
- }
292
-
293
- const parent = valueMap.get(parentName as T);
294
- if (!parent) {
295
- errors.addMessage(`${valueName}: parent ${parentName} is not a valid literal value.`);
296
- continue;
297
- }
298
-
299
- let ancestor = parent.parent;
300
- let circular = false;
301
- const ancestors: T[] = [];
302
-
303
- while (ancestor) {
304
- /* c8 ignore next 1 - defensive coding: complex circular reference detection intermittently missed */
305
- ancestors.push(ancestor.name);
306
- /* c8 ignore next 1 - defensive coding: ancestor.parent === value is very difficult to reach */
307
- if (ancestor.parent === parent || ancestor.parent === value) {
308
- /* c8 ignore next 3 - defensive coding: circular reference error reporting intermittently missed */
309
- errors.addMessage(`${valueName}: Circular reference detected: ${ancestors.join('->')}`);
310
- circular = true;
311
- break;
312
- }
313
- ancestor = ancestor.parent;
314
- }
315
-
316
- /* c8 ignore next 2 - defensive coding: circular reference handling intermittently missed */
317
- if (circular) {
318
- continue;
319
- }
320
-
321
- value.parent = parent;
322
- if (parent.children) {
323
- parent.children.push(value.name);
324
- } else {
325
- parent.children = [value.name];
326
- }
327
- }
328
- }
329
-
330
- return errors.returnOrReport(succeed(valueMap));
331
- }
332
- }
@@ -1,360 +0,0 @@
1
- /*
2
- * Copyright (c) 2025 Erik Fortune
3
- *
4
- * Permission is hereby granted, free of charge, to any person obtaining a copy
5
- * of this software and associated documentation files (the "Software"), to deal
6
- * in the Software without restriction, including without limitation the rights
7
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- * copies of the Software, and to permit persons to whom the Software is
9
- * furnished to do so, subject to the following conditions:
10
- *
11
- * The above copyright notice and this permission notice shall be included in all
12
- * copies or substantial portions of the Software.
13
- *
14
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
- * SOFTWARE.
21
- */
22
-
23
- import { Collections, ICollectible, Result, fail, succeed } from '@fgv/ts-utils';
24
- import {
25
- ConditionOperator,
26
- Convert,
27
- NoMatch,
28
- PerfectMatch,
29
- QualifierConditionValue,
30
- QualifierContextValue,
31
- QualifierMatchScore,
32
- QualifierTypeIndex,
33
- QualifierTypeName,
34
- Validate
35
- } from '../common';
36
-
37
- /**
38
- * Interface for a qualifier type. A qualifier type implements the build and
39
- * runtime semantics for some class of related qualifiers (e.g. language,
40
- * territories, etc).
41
- * @public
42
- */
43
- export interface IQualifierType extends ICollectible<QualifierTypeName, QualifierTypeIndex> {
44
- /**
45
- * The name of the qualifier type.
46
- */
47
- readonly name: QualifierTypeName;
48
-
49
- /**
50
- * Unique key for this qualifier.
51
- */
52
- readonly key: QualifierTypeName;
53
-
54
- /**
55
- * Global index for this qualifier type. Immutable once set, either at
56
- * construction or using {@link QualifierTypes.IQualifierType.setIndex | setIndex}.
57
- */
58
- readonly index: QualifierTypeIndex | undefined;
59
-
60
- /**
61
- * Validates a condition value for this qualifier type.
62
- * @param value - The string value to validate.
63
- * @returns `Success` with the {@link QualifierConditionValue | validated value}
64
- * if the value is valid for use in a condition, `Failure` with error details
65
- * otherwise.
66
- */
67
- isValidConditionValue(value: string): value is QualifierConditionValue;
68
-
69
- /**
70
- * Validates a context value for this qualifier type.
71
- * @param value - The string value to validate.
72
- * @returns `Success` with the {@link QualifierContextValue | validated value}
73
- * if the value is valid for use in a runtime context, `Failure` with error
74
- * details otherwise.
75
- */
76
- isValidContextValue(value: string): value is QualifierContextValue;
77
-
78
- /**
79
- * Determines if a supplied condition value is a potential match for a possible context value.
80
- * @param conditionValue - The condition value.
81
- * @param contextValue - The context value.
82
- * @returns `true` if the condition value is a potential match for the context value, `false` otherwise.
83
- */
84
- isPotentialMatch(conditionValue: string, contextValue: string): boolean;
85
-
86
- /**
87
- * Validates that a value and optional operator are valid for use in a condition
88
- * for qualifiers of this type.
89
- * @param value - The string value to validate.
90
- * @param operator - An optional operator to validate. Defaults to 'matches'.
91
- */
92
- validateCondition(value: string, operator?: ConditionOperator): Result<QualifierConditionValue>;
93
-
94
- /**
95
- * Validates that a value is valid for use in a runtime context for qualifiers
96
- * of this type.
97
- * @param value - The string value to validate.
98
- * @returns `Success` with the {@link QualifierContextValue | validated value}
99
- * if the value is valid for use in a runtime context, `Failure` with error
100
- * details otherwise.
101
- */
102
- validateContextValue(value: string): Result<QualifierContextValue>;
103
-
104
- /**
105
- * Determines the extent to which a condition matches a context value for this
106
- * qualifier type.
107
- * @param condition - The condition value to evaluate.
108
- * @param context - The context value to evaluate.
109
- * @param operator - The operator to use in evaluating the match.
110
- * @returns a {@link QualifierMatchScore | score} indicating the extent to which
111
- * the condition matches the context value.
112
- */
113
- matches(
114
- condition: QualifierConditionValue,
115
- context: QualifierContextValue,
116
- operator: ConditionOperator
117
- ): QualifierMatchScore;
118
-
119
- /**
120
- * Sets the index for this qualifier type. Once set, index is immutable.
121
- */
122
- setIndex(index: number): Result<QualifierTypeIndex>;
123
- }
124
-
125
- /**
126
- * Parameters used to create a base {@link QualifierTypes.QualifierType | qualifier type}.
127
- * @public
128
- */
129
- export interface IQualifierTypeCreateParams {
130
- /**
131
- * The name of the qualifier type. No default value.
132
- */
133
- name: string;
134
-
135
- /**
136
- * Global index for this qualifier type.
137
- */
138
- index?: number;
139
-
140
- /**
141
- * Flag indicating whether this qualifier type allows a list of values in a context.
142
- * Defaults to `false`.
143
- */
144
- allowContextList?: boolean;
145
- }
146
-
147
- /**
148
- * Abstract base class for qualifier types. Provides default implementations for
149
- * the {@link QualifierTypes.IQualifierType | IQualifierType} interface.
150
- * @public
151
- */
152
- export abstract class QualifierType implements IQualifierType {
153
- /**
154
- * {@inheritdoc QualifierTypes.IQualifierType.name}
155
- */
156
- public readonly name: QualifierTypeName;
157
-
158
- /**
159
- * {@inheritdoc QualifierTypes.IQualifierType.key}
160
- */
161
- public get key(): QualifierTypeName {
162
- return this._collectible.key;
163
- }
164
-
165
- /**
166
- * {@inheritdoc QualifierTypes.IQualifierType.index}
167
- */
168
- public get index(): QualifierTypeIndex | undefined {
169
- return this._collectible.index;
170
- }
171
-
172
- protected readonly _collectible: Collections.Collectible<QualifierTypeName, QualifierTypeIndex>;
173
-
174
- /**
175
- * Flag indicating whether this qualifier type allows a list of values in a context.
176
- * @public
177
- */
178
- public readonly allowContextList: boolean;
179
-
180
- /**
181
- * Constructor for use by derived classes.
182
- * @param name - The name of the qualifier type.
183
- * @param allowContextList - Flag indicating whether this qualifier type allows a
184
- * comma-separated list of runtime values in the context. Defaults to `false`.
185
- */
186
- protected constructor({ name, index, allowContextList }: IQualifierTypeCreateParams) {
187
- this.name = Convert.qualifierTypeName.convert(name).orThrow();
188
- this.allowContextList = allowContextList === true;
189
- this._collectible = new Collections.Collectible<QualifierTypeName, QualifierTypeIndex>({
190
- key: this.name,
191
- index: index,
192
- indexConverter: Convert.qualifierTypeIndex
193
- });
194
- }
195
-
196
- /**
197
- * {@inheritdoc QualifierTypes.IQualifierType.isValidConditionValue}
198
- */
199
- public abstract isValidConditionValue(value: string): value is QualifierConditionValue;
200
-
201
- /**
202
- * {@inheritdoc QualifierTypes.IQualifierType.isValidContextValue}
203
- */
204
- public isValidContextValue(value: string): value is QualifierContextValue {
205
- if (this.isValidConditionValue(value)) {
206
- return true;
207
- }
208
- if (this.allowContextList) {
209
- /* c8 ignore next 1 - functional code tested but coverage intermittently missed */
210
- return value.split(',').every((v) => this.isValidConditionValue(v.trim()));
211
- }
212
- /* c8 ignore next 1 - functional code tested but coverage intermittently missed */
213
- return false;
214
- }
215
-
216
- /**
217
- * {@inheritdoc QualifierTypes.IQualifierType.isPotentialMatch}
218
- */
219
- public isPotentialMatch(conditionValue: string, contextValue: string): boolean {
220
- if (this.isValidConditionValue(conditionValue) && this.isValidContextValue(contextValue)) {
221
- return this._matchOne(conditionValue, contextValue, 'matches') !== NoMatch;
222
- }
223
- return false;
224
- }
225
-
226
- /**
227
- * {@inheritdoc QualifierTypes.IQualifierType.validateCondition}
228
- */
229
- public validateCondition(value: string, operator?: ConditionOperator): Result<QualifierConditionValue> {
230
- operator = operator ?? 'matches';
231
- /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */
232
- if (operator !== 'matches') {
233
- return fail(`${operator}: invalid condition operator`);
234
- /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */
235
- } else if (!this.isValidConditionValue(value)) {
236
- return fail(`${value}: invalid condition value for qualifierType ${this.name}`);
237
- }
238
- return succeed(value);
239
- }
240
-
241
- /**
242
- * {@inheritdoc QualifierTypes.IQualifierType.validateContextValue}
243
- */
244
- public validateContextValue(value: string): Result<QualifierContextValue> {
245
- /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */
246
- if (!this.isValidContextValue(value)) {
247
- return fail(`${value}: invalid context value for qualifierType ${this.name}`);
248
- }
249
- return succeed(value);
250
- }
251
-
252
- /**
253
- * {@inheritdoc QualifierTypes.IQualifierType.matches}
254
- */
255
- public matches(
256
- condition: QualifierConditionValue,
257
- context: QualifierContextValue,
258
- operator: ConditionOperator
259
- ): QualifierMatchScore {
260
- if (this.allowContextList) {
261
- return this._matchList(condition, QualifierType._splitContext(context), operator);
262
- }
263
- return this._matchOne(condition, context, operator);
264
- }
265
-
266
- /**
267
- * {@inheritdoc QualifierTypes.IQualifierType.setIndex}
268
- */
269
- public setIndex(index: number): Result<QualifierTypeIndex> {
270
- return this._collectible.setIndex(index);
271
- }
272
-
273
- /**
274
- * {@inheritdoc Validate.isValidQualifierTypeName}
275
- */
276
- public static isValidName(name: string): name is QualifierTypeName {
277
- return Validate.isValidQualifierTypeName(name);
278
- }
279
-
280
- /**
281
- * {@inheritdoc Validate.isValidQualifierTypeIndex}
282
- */
283
- public static isValidIndex(index: number): index is QualifierTypeIndex {
284
- return Validate.isValidQualifierTypeIndex(index);
285
- }
286
-
287
- /**
288
- * Compares two qualifier types by index.
289
- * @param t1 - The first qualifier type to compare.
290
- * @param t2 - The second qualifier type to compare.
291
- * @returns a number indicating the relative order of the two qualifier types.
292
- */
293
- public static compare(t1: QualifierType, t2: QualifierType): number {
294
- const i1 = t1._collectible.index ?? -1;
295
- const i2 = t2._collectible.index ?? -1;
296
- let diff = i1 - i2;
297
-
298
- if (diff === 0) {
299
- diff = t1.name.localeCompare(t2.name);
300
- }
301
-
302
- return diff;
303
- }
304
-
305
- /**
306
- * Matches a single condition value against a single context value.
307
- * @param condition - The {@link QualifierConditionValue | condition value} to match.
308
- * @param context - The {@link QualifierContextValue | context value} to match.
309
- * @param operator - The {@link ConditionOperator | operator} to use in the match.
310
- * @returns a {@link QualifierMatchScore | score} indicating the extent to which
311
- * the condition matches the context value.
312
- * @public
313
- */
314
- protected abstract _matchOne(
315
- condition: QualifierConditionValue,
316
- context: QualifierContextValue,
317
- operator: ConditionOperator
318
- ): QualifierMatchScore;
319
-
320
- /**
321
- * Matches a single condition value against a list of context values.
322
- * @param condition - The {@link QualifierConditionValue | condition value} to match.
323
- * @param context - The comma-separated list of {@link QualifierContextValue | context values} to match.
324
- * @param operator - The {@link ConditionOperator | operator} to use in the match.
325
- * @returns a {@link QualifierMatchScore | score} indicating the extent to which
326
- * the condition matches the context value.
327
- * @public
328
- */
329
- protected _matchList(
330
- condition: QualifierConditionValue,
331
- context: QualifierContextValue[],
332
- operator: ConditionOperator
333
- ): QualifierMatchScore {
334
- const scorePerPosition = 1 / context.length;
335
- let baseScore = PerfectMatch - scorePerPosition;
336
-
337
- for (let i = 0; i < context.length; i++) {
338
- const score = this._matchOne(condition, context[i], operator);
339
- if (score > NoMatch) {
340
- const adjusted = baseScore + scorePerPosition * score;
341
- if (Validate.isValidQualifierMatchScore(adjusted)) {
342
- return adjusted;
343
- }
344
- }
345
- baseScore -= scorePerPosition;
346
- }
347
- return NoMatch;
348
- }
349
-
350
- /**
351
- * Splits a comma-separated {@link QualifierContextValue | context value} into an
352
- * array of individual values.
353
- * @param value - The value to split.
354
- * @returns an array of individual context values.
355
- * @public
356
- */
357
- protected static _splitContext(value: QualifierContextValue): QualifierContextValue[] {
358
- return value.split(',').map((s) => s.trim() as QualifierContextValue);
359
- }
360
- }