@fgv/ts-res 5.0.0-22 → 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 (189) hide show
  1. package/README.md +665 -44
  2. package/dist/ts-res.d.ts +421 -19
  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 +14 -0
  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/jsonResourceType.d.ts +4 -0
  28. package/lib/packlets/resource-types/jsonResourceType.js +4 -0
  29. package/lib/packlets/resource-types/resourceType.d.ts +4 -0
  30. package/lib/packlets/resources/candidateValue.d.ts +92 -0
  31. package/lib/packlets/resources/candidateValue.js +128 -0
  32. package/lib/packlets/resources/candidateValueCollector.d.ts +55 -0
  33. package/lib/packlets/resources/candidateValueCollector.js +96 -0
  34. package/lib/packlets/resources/index.d.ts +2 -0
  35. package/lib/packlets/resources/index.js +2 -0
  36. package/lib/packlets/resources/resource.js +2 -2
  37. package/lib/packlets/resources/resourceBuilder.d.ts +6 -0
  38. package/lib/packlets/resources/resourceBuilder.js +3 -1
  39. package/lib/packlets/resources/resourceCandidate.d.ts +10 -1
  40. package/lib/packlets/resources/resourceCandidate.js +11 -5
  41. package/lib/packlets/resources/resourceManagerBuilder.d.ts +7 -0
  42. package/lib/packlets/resources/resourceManagerBuilder.js +13 -6
  43. package/lib/packlets/runtime/compiledResourceCollection.d.ts +20 -8
  44. package/lib/packlets/runtime/compiledResourceCollection.js +25 -4
  45. package/lib/packlets/runtime/resource-tree/common.d.ts +1 -1
  46. package/lib/test/unit/qualifier-types/testQualifierType.d.ts +4 -0
  47. package/lib/test/unit/qualifier-types/testQualifierType.js +24 -0
  48. package/lib/test/unit/resource-types/testDerivedResourceType.d.ts +1 -0
  49. package/lib/test/unit/resource-types/testDerivedResourceType.js +3 -0
  50. package/lib/test/unit/resources/candidateValue.test.d.ts +2 -0
  51. package/lib/test/unit/resources/candidateValueCollector.test.d.ts +2 -0
  52. package/package.json +7 -7
  53. package/src/index.ts +0 -75
  54. package/src/packlets/bundle/bundleBuilder.ts +0 -159
  55. package/src/packlets/bundle/bundleLoader.ts +0 -157
  56. package/src/packlets/bundle/bundleNormalizer.ts +0 -173
  57. package/src/packlets/bundle/bundleUtils.ts +0 -142
  58. package/src/packlets/bundle/convert.ts +0 -87
  59. package/src/packlets/bundle/index.ts +0 -52
  60. package/src/packlets/bundle/model.ts +0 -138
  61. package/src/packlets/common/conditions.ts +0 -226
  62. package/src/packlets/common/convert.ts +0 -184
  63. package/src/packlets/common/helpers/conditions.ts +0 -84
  64. package/src/packlets/common/helpers/context.ts +0 -88
  65. package/src/packlets/common/helpers/index.ts +0 -26
  66. package/src/packlets/common/helpers/qualifierDefaultValues.ts +0 -91
  67. package/src/packlets/common/helpers/resources.ts +0 -96
  68. package/src/packlets/common/index.ts +0 -30
  69. package/src/packlets/common/resources.ts +0 -105
  70. package/src/packlets/common/validate/conditions.ts +0 -595
  71. package/src/packlets/common/validate/index.ts +0 -27
  72. package/src/packlets/common/validate/regularExpressions.ts +0 -92
  73. package/src/packlets/common/validate/resources.ts +0 -170
  74. package/src/packlets/conditions/condition.ts +0 -351
  75. package/src/packlets/conditions/conditionCollector.ts +0 -103
  76. package/src/packlets/conditions/conditionDecls.ts +0 -50
  77. package/src/packlets/conditions/conditionSet.ts +0 -318
  78. package/src/packlets/conditions/conditionSetCollector.ts +0 -127
  79. package/src/packlets/conditions/conditionSetDecls.ts +0 -41
  80. package/src/packlets/conditions/conditionToken.ts +0 -180
  81. package/src/packlets/conditions/convert/conditionSetDecls.ts +0 -72
  82. package/src/packlets/conditions/convert/decls.ts +0 -95
  83. package/src/packlets/conditions/convert/index.ts +0 -24
  84. package/src/packlets/conditions/index.ts +0 -33
  85. package/src/packlets/config/common.ts +0 -107
  86. package/src/packlets/config/configInitFactory.ts +0 -142
  87. package/src/packlets/config/convert.ts +0 -61
  88. package/src/packlets/config/index.ts +0 -30
  89. package/src/packlets/config/json.ts +0 -43
  90. package/src/packlets/config/predefined/default.ts +0 -150
  91. package/src/packlets/config/predefined/extended.ts +0 -198
  92. package/src/packlets/config/predefined/index.ts +0 -26
  93. package/src/packlets/config/systemConfiguration.ts +0 -217
  94. package/src/packlets/context/contextDecls.ts +0 -76
  95. package/src/packlets/context/contextToken.ts +0 -252
  96. package/src/packlets/context/convert/decls.ts +0 -126
  97. package/src/packlets/context/convert/index.ts +0 -23
  98. package/src/packlets/context/index.ts +0 -27
  99. package/src/packlets/decisions/abstractDecision.ts +0 -87
  100. package/src/packlets/decisions/abstractDecisionCollector.ts +0 -122
  101. package/src/packlets/decisions/candidate.ts +0 -101
  102. package/src/packlets/decisions/common.ts +0 -37
  103. package/src/packlets/decisions/concreteDecision.ts +0 -157
  104. package/src/packlets/decisions/decision.ts +0 -172
  105. package/src/packlets/decisions/index.ts +0 -28
  106. package/src/packlets/import/fsItem.ts +0 -212
  107. package/src/packlets/import/importContext.ts +0 -183
  108. package/src/packlets/import/importManager.ts +0 -187
  109. package/src/packlets/import/importable.ts +0 -107
  110. package/src/packlets/import/importers/collectionImporter.ts +0 -153
  111. package/src/packlets/import/importers/fsItemImporter.ts +0 -154
  112. package/src/packlets/import/importers/importer.ts +0 -58
  113. package/src/packlets/import/importers/index.ts +0 -27
  114. package/src/packlets/import/importers/jsonImporter.ts +0 -133
  115. package/src/packlets/import/importers/pathImporter.ts +0 -134
  116. package/src/packlets/import/index.ts +0 -29
  117. package/src/packlets/qualifier-types/config/convert.ts +0 -108
  118. package/src/packlets/qualifier-types/config/index.ts +0 -27
  119. package/src/packlets/qualifier-types/config/json.ts +0 -126
  120. package/src/packlets/qualifier-types/convert.ts +0 -77
  121. package/src/packlets/qualifier-types/helpers.ts +0 -94
  122. package/src/packlets/qualifier-types/index.ts +0 -34
  123. package/src/packlets/qualifier-types/languageQualifierType.ts +0 -143
  124. package/src/packlets/qualifier-types/literalQualifierType.ts +0 -244
  125. package/src/packlets/qualifier-types/literalValueHierarchy.ts +0 -332
  126. package/src/packlets/qualifier-types/qualifierType.ts +0 -360
  127. package/src/packlets/qualifier-types/qualifierTypeCollector.ts +0 -102
  128. package/src/packlets/qualifier-types/territoryQualifierType.ts +0 -255
  129. package/src/packlets/qualifiers/convert/decls.ts +0 -90
  130. package/src/packlets/qualifiers/convert/index.ts +0 -24
  131. package/src/packlets/qualifiers/convert/qualifier.ts +0 -55
  132. package/src/packlets/qualifiers/index.ts +0 -31
  133. package/src/packlets/qualifiers/qualifier.ts +0 -169
  134. package/src/packlets/qualifiers/qualifierCollector.ts +0 -194
  135. package/src/packlets/qualifiers/qualifierDecl.ts +0 -80
  136. package/src/packlets/qualifiers/qualifierDefaultValueDecls.ts +0 -57
  137. package/src/packlets/qualifiers/qualifierDefaultValueToken.ts +0 -230
  138. package/src/packlets/resource-json/compiled/common.ts +0 -35
  139. package/src/packlets/resource-json/compiled/convert.ts +0 -143
  140. package/src/packlets/resource-json/compiled/index.ts +0 -27
  141. package/src/packlets/resource-json/compiled/json.ts +0 -239
  142. package/src/packlets/resource-json/convert.ts +0 -236
  143. package/src/packlets/resource-json/helpers.ts +0 -284
  144. package/src/packlets/resource-json/index.ts +0 -33
  145. package/src/packlets/resource-json/json.ts +0 -295
  146. package/src/packlets/resource-json/normalized.ts +0 -211
  147. package/src/packlets/resource-json/resourceDeclCollection.ts +0 -117
  148. package/src/packlets/resource-json/resourceDeclContainer.ts +0 -48
  149. package/src/packlets/resource-json/resourceDeclTree.ts +0 -116
  150. package/src/packlets/resource-types/config/convert.ts +0 -38
  151. package/src/packlets/resource-types/config/index.ts +0 -27
  152. package/src/packlets/resource-types/config/json.ts +0 -33
  153. package/src/packlets/resource-types/helpers.ts +0 -44
  154. package/src/packlets/resource-types/index.ts +0 -30
  155. package/src/packlets/resource-types/jsonResourceType.ts +0 -102
  156. package/src/packlets/resource-types/resourceType.ts +0 -292
  157. package/src/packlets/resource-types/resourceTypeCollector.ts +0 -79
  158. package/src/packlets/resources/candidateReducer.ts +0 -331
  159. package/src/packlets/resources/common.ts +0 -114
  160. package/src/packlets/resources/index.ts +0 -28
  161. package/src/packlets/resources/resource.ts +0 -307
  162. package/src/packlets/resources/resourceBuilder.ts +0 -269
  163. package/src/packlets/resources/resourceCandidate.ts +0 -333
  164. package/src/packlets/resources/resourceManagerBuilder.ts +0 -1169
  165. package/src/packlets/runtime/cacheListener.ts +0 -113
  166. package/src/packlets/runtime/cacheMetrics.ts +0 -206
  167. package/src/packlets/runtime/compiledResourceCollection.ts +0 -607
  168. package/src/packlets/runtime/conditionSetResolutionResult.ts +0 -160
  169. package/src/packlets/runtime/context/contextQualifierProvider.ts +0 -117
  170. package/src/packlets/runtime/context/contextQualifierProviderValidator.ts +0 -281
  171. package/src/packlets/runtime/context/index.ts +0 -26
  172. package/src/packlets/runtime/context/simpleContextQualifierProvider.ts +0 -225
  173. package/src/packlets/runtime/context/validatingSimpleContextQualifierProvider.ts +0 -96
  174. package/src/packlets/runtime/iResourceManager.ts +0 -130
  175. package/src/packlets/runtime/index.ts +0 -35
  176. package/src/packlets/runtime/resource-tree/common.ts +0 -126
  177. package/src/packlets/runtime/resource-tree/index.ts +0 -26
  178. package/src/packlets/runtime/resource-tree/readOnlyResourceTree.ts +0 -342
  179. package/src/packlets/runtime/resource-tree/resourceTreeChildren.ts +0 -125
  180. package/src/packlets/runtime/resource-tree/resourceTreeChildrenValidator.ts +0 -187
  181. package/src/packlets/runtime/resourceResolver.ts +0 -712
  182. package/src/packlets/runtime/validate.ts +0 -63
  183. package/src/packlets/zip-archive/convert.ts +0 -121
  184. package/src/packlets/zip-archive/index.ts +0 -76
  185. package/src/packlets/zip-archive/json.ts +0 -91
  186. package/src/packlets/zip-archive/types.ts +0 -140
  187. package/src/packlets/zip-archive/zipArchiveCreator.ts +0 -229
  188. package/src/packlets/zip-archive/zipArchiveFormat.ts +0 -158
  189. package/src/packlets/zip-archive/zipArchiveLoader.ts +0 -370
@@ -1,103 +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, Collections, Result, succeed, ValidatingCollector } from '@fgv/ts-utils';
24
- import { IReadOnlyQualifierCollector } from '../qualifiers';
25
- import { IConditionDecl } from './conditionDecls';
26
- import { Condition } from './condition';
27
- import { Convert as CommonConvert, ConditionKey } from '../common';
28
- import { validatedConditionDecl } from './convert';
29
-
30
- /**
31
- * Parameters for creating a {@link Conditions.ConditionCollector | ConditionCollector}.
32
- * @public
33
- */
34
- export interface IConditionCollectorCreateParams {
35
- /**
36
- * The {@link Qualifiers.IReadOnlyQualifierCollector | ReadOnlyQualifierCollector} used to
37
- * create conditions in this collector.
38
- */
39
- qualifiers: IReadOnlyQualifierCollector;
40
-
41
- /**
42
- * Optional array of condition declarations to add to the collector.
43
- */
44
- conditions?: IConditionDecl[];
45
- }
46
-
47
- /**
48
- * A `ValidatingCollector` for {@link Conditions.Condition | Conditions},
49
- * which collects conditions supplied as either {@link Conditions.Condition | Condition} or
50
- * {@link Conditions.IConditionDecl | IConditionDecl}.
51
- * @public
52
- */
53
- export class ConditionCollector extends ValidatingCollector<Condition> {
54
- /**
55
- * The {@link Qualifiers.IReadOnlyQualifierCollector | ReadOnlyQualifierCollector} used to create conditions
56
- * in this collector.
57
- * @public
58
- */
59
- public qualifiers: IReadOnlyQualifierCollector;
60
-
61
- /**
62
- * Constructor for a {@link Conditions.ConditionCollector | ConditionCollector} object.
63
- * @param params - Required {@link Conditions.IConditionCollectorCreateParams | parameters} for
64
- * creating the collector.
65
- */
66
- protected constructor(params: IConditionCollectorCreateParams) {
67
- super({
68
- converters: new Collections.KeyValueConverters<ConditionKey, Condition>({
69
- key: CommonConvert.conditionKey,
70
- value: (from: unknown) => this._toCondition(from)
71
- })
72
- });
73
- this.qualifiers = params.qualifiers;
74
- /* c8 ignore next 1 - ? is defense in depth */
75
- params.conditions?.forEach((c) => this.validating.add(c).orThrow());
76
- }
77
-
78
- /**
79
- * Creates a new {@link Conditions.ConditionCollector | ConditionCollector} object.
80
- * @param params - Required {@link Conditions.IConditionCollectorCreateParams | parameters} for
81
- * creating the collector.
82
- * @returns `Success` with the new collector if successful, or `Failure` with
83
- * an error message if not.
84
- */
85
- public static create(params: IConditionCollectorCreateParams): Result<ConditionCollector> {
86
- return captureResult(() => new ConditionCollector(params));
87
- }
88
-
89
- private _toCondition(from: unknown): Result<Condition> {
90
- if (from instanceof Condition) {
91
- return succeed(from);
92
- }
93
- return validatedConditionDecl
94
- .convert(from, { qualifiers: this.qualifiers, conditionIndex: this.size })
95
- .onSuccess((c) => Condition.create(c));
96
- }
97
- }
98
-
99
- /**
100
- * Type alias for a read-only {@link Conditions.ConditionCollector | ConditionCollector}.
101
- * @public
102
- */
103
- export type ReadOnlyConditionCollector = Collections.IReadOnlyValidatingCollector<Condition>;
@@ -1,50 +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 {
24
- ConditionIndex,
25
- ConditionOperator,
26
- ConditionPriority,
27
- QualifierConditionValue,
28
- QualifierMatchScore
29
- } from '../common';
30
- import { Qualifier } from '../qualifiers';
31
- import * as ResourceJson from '../resource-json';
32
-
33
- /**
34
- * Non-validated declaration of a {@link Conditions.Condition | condition}.
35
- * @public
36
- */
37
- export type IConditionDecl = ResourceJson.Json.ILooseConditionDecl;
38
-
39
- /**
40
- * Validated declaration of a {@link Conditions.Condition | condition} with all defaults applied.
41
- * @public
42
- */
43
- export interface IValidatedConditionDecl {
44
- qualifier: Qualifier;
45
- value: QualifierConditionValue;
46
- operator: ConditionOperator;
47
- priority: ConditionPriority;
48
- scoreAsDefault?: QualifierMatchScore;
49
- index?: ConditionIndex;
50
- }
@@ -1,318 +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, Collections, Hash, mapResults, Result, succeed } from '@fgv/ts-utils';
24
- import { Condition } from './condition';
25
- import {
26
- Convert as CommonConvert,
27
- ConditionSetIndex,
28
- ConditionSetKey,
29
- QualifierName,
30
- Validate
31
- } from '../common';
32
- import { IValidatedConditionSetDecl } from './conditionSetDecls';
33
- import * as ConditionsConvert from './convert';
34
- import { ConditionCollector } from './conditionCollector';
35
- import * as ResourceJson from '../resource-json';
36
- import * as Context from '../context';
37
-
38
- /**
39
- * Options for creating a {@link Conditions.ConditionSet | ConditionSet} declaration.
40
- * @remarks
41
- * This interface extends the {@link ResourceJson.Helpers.IDeclarationOptions | declaration options}
42
- * interface to include a `reduceQualifiers` option.
43
- * @public
44
- */
45
- export interface IConditionSetDeclOptions extends ResourceJson.Helpers.IDeclarationOptions {
46
- /**
47
- * If provided, reduces the qualifiers of the condition set by removing qualifiers that are made
48
- * irrelevant by the filterForContext.
49
- */
50
- qualifiersToReduce?: ReadonlySet<QualifierName>;
51
- }
52
-
53
- /**
54
- * Represents a set of {@link Conditions.Condition | conditions} that must all be met in some runtime
55
- * context for a resource instance to be valid.
56
- * @public
57
- */
58
- export class ConditionSet implements IValidatedConditionSetDecl {
59
- protected readonly _collectible: Collections.Collectible<ConditionSetKey, ConditionSetIndex>;
60
- /**
61
- * The {@link Conditions.Condition | conditions} that make up this condition
62
- * set.
63
- * @public
64
- */
65
- public readonly conditions: ReadonlyArray<Condition>;
66
-
67
- /**
68
- * The key for this condition set.
69
- */
70
- public get key(): ConditionSetKey {
71
- return this._collectible.key;
72
- }
73
-
74
- /**
75
- * The number of conditions in this condition set.
76
- */
77
- public get size(): number {
78
- return this.conditions.length;
79
- }
80
-
81
- /**
82
- * The index for this condition set.
83
- */
84
- public get index(): ConditionSetIndex | undefined {
85
- return this._collectible.index;
86
- }
87
-
88
- /**
89
- * The key for an unconditional condition set.
90
- */
91
- public static UnconditionalKey: ConditionSetKey = Validate.toConditionSetKey('').orThrow();
92
-
93
- /**
94
- * Constructor for a {@link Conditions.ConditionSet | ConditionSet} object.
95
- * @param params - {@link Conditions.IValidatedConditionSetDecl | Validated declaration}
96
- * used to create the condition set.
97
- */
98
- protected constructor(params: IValidatedConditionSetDecl) {
99
- const qualifiers = new Map<QualifierName, Condition>();
100
- for (const condition of params.conditions) {
101
- /* c8 ignore next 9 - there's a test for this but coverage is having a bad day */
102
- if (qualifiers.has(condition.qualifier.name)) {
103
- const existing = qualifiers.get(condition.qualifier.name)?.toString() ?? 'unknown';
104
- throw new Error(
105
- `${
106
- condition.qualifier.name
107
- }: Duplicate conditions ${existing} and ${condition.toString()} are not supported.`
108
- );
109
- }
110
- qualifiers.set(condition.qualifier.name, condition);
111
- }
112
- this.conditions = Array.from(params.conditions).sort(Condition.compare).reverse();
113
- this._collectible = new Collections.Collectible({
114
- key: this.toKey(),
115
- index: params.index,
116
- indexConverter: CommonConvert.conditionSetIndex
117
- });
118
- }
119
-
120
- /**
121
- * Creates a new {@link Conditions.ConditionSet | ConditionSet} object.
122
- * @param params - {@link Conditions.IValidatedConditionSetDecl | Validated declaration}
123
- * used to create the condition set.
124
- * @returns `Success` with the new {@link Conditions.ConditionSet | ConditionSet} object if successful,
125
- * or `Failure` with an error message if not.
126
- * @public
127
- */
128
- public static create(params: IValidatedConditionSetDecl): Result<ConditionSet> {
129
- return captureResult(() => new ConditionSet(params));
130
- }
131
-
132
- /**
133
- * Compares two {@link Conditions.ConditionSet | ConditionSets} for sorting purposes.
134
- * @param cs1 - The first {@link Conditions.ConditionSet | ConditionSet} to compare.
135
- * @param cs2 - The second {@link Conditions.ConditionSet | ConditionSet} to compare.
136
- * @returns A negative number if `cs1` should come before `cs2`, a positive
137
- * number if `cs1` should come after `cs2`, or zero if they are equivalent.
138
- * @public
139
- */
140
- public static compare(cs1: ConditionSet, cs2: ConditionSet): number {
141
- const len = Math.min(cs1.conditions.length, cs2.conditions.length);
142
- for (let i = 0; i < len; i++) {
143
- const diff = Condition.compare(cs1.conditions[i], cs2.conditions[i]);
144
- if (diff !== 0) {
145
- return diff;
146
- }
147
- }
148
- return cs1.conditions.length - cs2.conditions.length;
149
- }
150
-
151
- /**
152
- * Sets the global index for this condition set. Once set, the index cannot be changed.
153
- * @param index - The index to set for this condition set.
154
- * @returns `Success` with the index if successful, `Failure` otherwise.
155
- */
156
- public setIndex(index: number): Result<ConditionSetIndex> {
157
- return this._collectible.setIndex(index);
158
- }
159
-
160
- /**
161
- * Determines if this condition set can match a supplied context, even if the context is partial.
162
- * Returns true if all conditions in the set can match the context (using canMatchPartialContext).
163
- * Returns false otherwise.
164
- * @param context - The context to match.
165
- * @param options - Options to use when matching.
166
- * @returns `true` if all conditions can match the context, `false` otherwise.
167
- */
168
- public canMatchPartialContext(
169
- context: Context.IValidatedContextDecl,
170
- options?: Context.IContextMatchOptions
171
- ): boolean {
172
- return this.conditions.every((c) => c.canMatchPartialContext(context, options));
173
- }
174
-
175
- /**
176
- * Gets the {@link ConditionSetKey | key} for a supplied {@link Conditions.IValidatedConditionSetDecl | condition set declaration}.
177
- * @param decl - The {@link Conditions.IValidatedConditionSetDecl | condition set declaration} for which to get the key.
178
- * @returns `Success` with the condition set key if successful, `Failure` otherwise.
179
- * @public
180
- */
181
- public static getKeyForDecl(decl: IValidatedConditionSetDecl): Result<ConditionSetKey> {
182
- return Validate.toConditionSetKey(decl.conditions.map((c) => c.toKey()).join('+'));
183
- }
184
-
185
- /**
186
- * Gets a condition set key from a loose condition set declaration.
187
- * @param conditionSet - The loose condition set declaration to convert.
188
- * @param conditionCollector - The condition collector used for validation.
189
- * @returns `Success` with the condition set key if successful, `Failure` otherwise.
190
- * @public
191
- */
192
- public static getKeyFromLooseDecl(
193
- conditionSet: ResourceJson.Json.ConditionSetDecl | undefined,
194
- conditionCollector: ConditionCollector
195
- ): Result<ConditionSetKey> {
196
- if (!conditionSet) {
197
- return succeed(ConditionSet.UnconditionalKey);
198
- }
199
-
200
- // Convert ConditionSetDecl to IConditionSetDecl format
201
- let conditionSetDecl: { conditions: ResourceJson.Json.ILooseConditionDecl[] };
202
-
203
- if (Array.isArray(conditionSet)) {
204
- // ConditionSetDeclAsArray: array of ILooseConditionDecl
205
- conditionSetDecl = { conditions: conditionSet };
206
- } else {
207
- // ConditionSetDeclAsRecord: Record<string, string | IChildConditionDecl>
208
- const conditions = Object.entries(conditionSet).map(([qualifierName, value]) => {
209
- if (typeof value === 'string') {
210
- return { qualifierName, value };
211
- } else {
212
- return { qualifierName, ...value };
213
- }
214
- });
215
- conditionSetDecl = { conditions };
216
- }
217
-
218
- // Validate and convert to IValidatedConditionSetDecl
219
- return ConditionsConvert.validatedConditionSetDecl
220
- .convert(conditionSetDecl, { conditions: conditionCollector })
221
- .onSuccess((validatedDecl) => {
222
- // Use proper ConditionSet.getKeyForDecl method to generate the key
223
- return ConditionSet.getKeyForDecl(validatedDecl);
224
- });
225
- }
226
-
227
- /**
228
- * Gets a {@link ConditionSetToken | condition set token} for this condition set,
229
- * if possible.
230
- * @param terse - If true, the token will be terse, omitting qualifier names where
231
- * possible.
232
- * @returns `Success` with the {@link ConditionSetToken | condition set token} if successful,
233
- * `Failure` with an error message otherwise.
234
- * @public
235
- */
236
- public toToken(terse?: boolean): Result<string> {
237
- return mapResults(this.conditions.map((c) => c.toToken(terse))).onSuccess((tokens) => {
238
- return Validate.toConditionSetToken(tokens.join(','));
239
- });
240
- }
241
-
242
- /**
243
- * Gets the {@link ConditionSetKey | key} for this condition set.
244
- * @returns The key for this condition set.
245
- */
246
- public toKey(): ConditionSetKey {
247
- return ConditionSet.getKeyForDecl(this).orThrow();
248
- }
249
-
250
- /**
251
- * Gets a hash of this condition set.
252
- * @returns A hash of this condition
253
- * set key.
254
- * @public
255
- */
256
- public toHash(): string {
257
- return Hash.Crc32Normalizer.crc32Hash([this.key]).padStart(8, '0');
258
- }
259
-
260
- /**
261
- * Gets a human-readable string representation of this condition set.
262
- * @returns A string representation of this condition set.
263
- */
264
- public toString(): string {
265
- return this.toKey();
266
- }
267
-
268
- /**
269
- * Gets the {@link ResourceJson.Json.ConditionSetDeclAsRecord | condition set declaration as a record} for this condition set.
270
- * @param options - {@link ResourceJson.Helpers.IDeclarationOptions | options} for the condition set declaration.
271
- * @returns The {@link ResourceJson.Json.ConditionSetDeclAsRecord | condition set declaration as a record} for this condition set.
272
- */
273
- public toConditionSetRecordDecl(
274
- options?: IConditionSetDeclOptions
275
- ): ResourceJson.Json.ConditionSetDeclAsRecord {
276
- const qualifiersToReduce = options?.qualifiersToReduce;
277
- const conditions = qualifiersToReduce
278
- ? /* c8 ignore next 1 - coverage intermittently misses the next line */
279
- this.conditions.filter((c) => !qualifiersToReduce.has(c.qualifier.name))
280
- : this.conditions;
281
- return Object.fromEntries(
282
- conditions.map((c): [string, ResourceJson.Json.IChildConditionDecl | string] => {
283
- return [c.qualifier.name, c.toValueOrChildConditionDecl(options)];
284
- })
285
- );
286
- }
287
-
288
- /**
289
- * Gets the {@link ResourceJson.Json.ConditionSetDeclAsArray | condition set declaration as an array} for this condition set.
290
- * @param options - {@link ResourceJson.Helpers.IDeclarationOptions | options} for the condition set declaration.
291
- * @returns The {@link ResourceJson.Json.ConditionSetDeclAsArray | condition set declaration as an array} for this condition set.
292
- */
293
- public toConditionSetArrayDecl(
294
- options?: IConditionSetDeclOptions
295
- ): ResourceJson.Json.ConditionSetDeclAsArray {
296
- /* c8 ignore next 1 - defense in depth */
297
- const qualifiersToReduce = options?.qualifiersToReduce;
298
- const conditions = qualifiersToReduce
299
- ? this.conditions.filter((c) => !qualifiersToReduce.has(c.qualifier.name))
300
- : this.conditions;
301
- return conditions.map((c) => c.toLooseConditionDecl(options));
302
- }
303
-
304
- /**
305
- * Converts this condition set to a compiled condition set representation.
306
- * @param options - Optional compilation options controlling the output format.
307
- * @returns A compiled condition set object that can be used for serialization or runtime processing.
308
- * @public
309
- */
310
- public toCompiled(
311
- options?: ResourceJson.Compiled.ICompiledResourceOptions
312
- ): ResourceJson.Compiled.ICompiledConditionSet {
313
- return {
314
- conditions: this.conditions.map((c) => c.index!),
315
- ...(options?.includeMetadata === true ? { metadata: { key: this.key } } : {})
316
- };
317
- }
318
- }
@@ -1,127 +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, Collections, mapResults, Result, succeed, ValidatingCollector } from '@fgv/ts-utils';
24
- import { ConditionCollector } from './conditionCollector';
25
- import { IConditionSetDecl } from './conditionSetDecls';
26
- import { ConditionSet } from './conditionSet';
27
- import { validatedConditionSetDecl } from './convert';
28
- import { ConditionSetKey, Convert as CommonConvert, ConditionSetIndex, Validate } from '../common';
29
-
30
- /**
31
- * Parameters for creating a {@link Conditions.ConditionSetCollector | ConditionSetCollector}.
32
- * @public
33
- */
34
- export interface IConditionSetCollectorCreateParams {
35
- /**
36
- * The {@link Conditions.ConditionCollector | ConditionCollector} used to create conditions
37
- * for conditions in this collector.
38
- */
39
- conditions: ConditionCollector;
40
-
41
- /**
42
- * Optional array of {@link Conditions.IConditionSetDecl | condition set declarations} to add to
43
- * the collector.
44
- */
45
- conditionSets?: IConditionSetDecl[];
46
- }
47
-
48
- /**
49
- * A `ValidatingCollector` for {@link Conditions.ConditionSet | ConditionSets},
50
- * which collects condition sets supplied as {@link Conditions.ConditionSet | ConditionSet} or
51
- * as {@link Conditions.IConditionSetDecl | IConditionSetDecl} via the methods on the
52
- * `validating` property.
53
- * @public
54
- */
55
- export class ConditionSetCollector extends ValidatingCollector<ConditionSet> {
56
- /**
57
- * Gets the {@link Conditions.ConditionCollector | ConditionCollector} used to create conditions
58
- * for conditions in this collector.
59
- */
60
- public conditions: ConditionCollector;
61
-
62
- /**
63
- * Gets the {@link Conditions.ConditionSet | ConditionSet} at the unconditional
64
- * condition set index.
65
- */
66
- public get unconditionalConditionSet(): ConditionSet {
67
- return this.getAt(ConditionSetCollector.UnconditionalIndex).orThrow();
68
- }
69
-
70
- /**
71
- * The index of the unconditional condition set.
72
- */
73
- public static UnconditionalIndex: ConditionSetIndex = Validate.toConditionSetIndex(0).orThrow();
74
-
75
- /**
76
- * Creates a new {@link Conditions.ConditionSetCollector | ConditionSetCollector}.
77
- * @param params - {@link Conditions.IConditionSetCollectorCreateParams | Parameters} used to create
78
- * the collector.
79
- */
80
- protected constructor(params: IConditionSetCollectorCreateParams) {
81
- super({
82
- converters: new Collections.KeyValueConverters<ConditionSetKey, ConditionSet>({
83
- key: CommonConvert.conditionSetKey,
84
- /* c8 ignore next 1 - there's a test but coverage is having a bad day */
85
- value: (from: unknown) => this._toConditionSet(from)
86
- })
87
- });
88
- this.conditions = params.conditions;
89
- // it's very helpful for debugging if the unconditional condition set is always
90
- // in position 0
91
- this.validating.add(ConditionSet.create({ conditions: [] }).orThrow()).orThrow();
92
- /* c8 ignore next 1 - ? is defense in depth */
93
- params.conditionSets?.forEach((item) => this.validating.add(item).orThrow());
94
- }
95
-
96
- /**
97
- * Creates a new {@link Conditions.ConditionSetCollector | ConditionSetCollector}.
98
- * @param params - {@link Conditions.IConditionSetCollectorCreateParams | Parameters} used to create
99
- * the collector.
100
- * @returns `Success` with the new collector if successful, or `Failure` with an error message
101
- * if not.
102
- */
103
- public static create(params: IConditionSetCollectorCreateParams): Result<ConditionSetCollector> {
104
- return captureResult(() => new ConditionSetCollector(params));
105
- }
106
-
107
- private _toConditionSet(from: unknown): Result<ConditionSet> {
108
- if (from instanceof ConditionSet) {
109
- return succeed(from);
110
- }
111
- if (Array.isArray(from)) {
112
- return mapResults(from.map((item) => this.conditions.validating.getOrAdd(item))).onSuccess(
113
- (conditions) => ConditionSet.create({ conditions })
114
- );
115
- }
116
- /* c8 ignore next 4 - functional code path tested but coverage intermittently missed */
117
- return validatedConditionSetDecl
118
- .convert(from, { conditions: this.conditions, conditionSetIndex: this.size })
119
- .onSuccess((c) => ConditionSet.create(c));
120
- }
121
- }
122
-
123
- /**
124
- * A read-only {@link Conditions.ConditionSetCollector | ConditionSetCollector}.
125
- * @public
126
- */
127
- export type ReadOnlyConditionSetCollector = Collections.IReadOnlyValidatingCollector<ConditionSet>;
@@ -1,41 +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 { Condition } from './condition';
24
- import { IConditionDecl } from './conditionDecls';
25
-
26
- /**
27
- * Non-validated declaration of a {@link Conditions.ConditionSet | set of conditions}.
28
- * @public
29
- */
30
- export interface IConditionSetDecl {
31
- conditions: IConditionDecl[];
32
- }
33
-
34
- /**
35
- * Validated declaration of a {@link Conditions.ConditionSet | set of conditions}.
36
- * @public
37
- */
38
- export interface IValidatedConditionSetDecl {
39
- conditions: ReadonlyArray<Condition>;
40
- index?: number;
41
- }