@fgv/ts-res 5.0.1-1 → 5.0.1-10

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 (196) hide show
  1. package/dist/index.browser.js +53 -0
  2. package/dist/index.js +45 -0
  3. package/dist/packlets/bundle/bundleBuilder.js +130 -0
  4. package/dist/packlets/bundle/bundleLoader.js +101 -0
  5. package/dist/packlets/bundle/bundleNormalizer.js +138 -0
  6. package/dist/packlets/bundle/bundleUtils.js +113 -0
  7. package/dist/packlets/bundle/convert.js +75 -0
  8. package/dist/packlets/bundle/index.js +42 -0
  9. package/dist/packlets/bundle/model.js +23 -0
  10. package/dist/packlets/common/conditions.js +47 -0
  11. package/dist/packlets/common/convert.js +172 -0
  12. package/dist/packlets/common/helpers/conditions.js +66 -0
  13. package/dist/packlets/common/helpers/context.js +70 -0
  14. package/dist/packlets/common/helpers/index.js +26 -0
  15. package/dist/packlets/common/helpers/qualifierDefaultValues.js +66 -0
  16. package/dist/packlets/common/helpers/resources.js +93 -0
  17. package/dist/packlets/common/index.js +28 -0
  18. package/dist/packlets/common/resources.js +27 -0
  19. package/dist/packlets/common/validate/conditions.js +544 -0
  20. package/dist/packlets/common/validate/index.js +26 -0
  21. package/dist/packlets/common/validate/regularExpressions.js +84 -0
  22. package/dist/packlets/common/validate/resources.js +209 -0
  23. package/dist/packlets/conditions/condition.js +252 -0
  24. package/dist/packlets/conditions/conditionCollector.js +69 -0
  25. package/dist/packlets/conditions/conditionDecls.js +23 -0
  26. package/dist/packlets/conditions/conditionSet.js +247 -0
  27. package/dist/packlets/conditions/conditionSetCollector.js +89 -0
  28. package/dist/packlets/conditions/conditionSetDecls.js +23 -0
  29. package/dist/packlets/conditions/conditionToken.js +147 -0
  30. package/dist/packlets/conditions/convert/conditionSetDecls.js +51 -0
  31. package/dist/packlets/conditions/convert/decls.js +75 -0
  32. package/dist/packlets/conditions/convert/index.js +24 -0
  33. package/dist/packlets/conditions/index.js +31 -0
  34. package/dist/packlets/config/common.js +78 -0
  35. package/dist/packlets/config/configInitFactory.js +258 -0
  36. package/dist/packlets/config/convert.js +56 -0
  37. package/dist/packlets/config/index.browser.js +34 -0
  38. package/dist/packlets/config/index.js +28 -0
  39. package/dist/packlets/config/json.js +23 -0
  40. package/dist/packlets/config/predefined/default.js +138 -0
  41. package/dist/packlets/config/predefined/extended.js +190 -0
  42. package/dist/packlets/config/predefined/index.js +25 -0
  43. package/dist/packlets/config/systemConfiguration.js +147 -0
  44. package/dist/packlets/context/contextDecls.js +23 -0
  45. package/dist/packlets/context/contextToken.js +202 -0
  46. package/dist/packlets/context/convert/decls.js +91 -0
  47. package/dist/packlets/context/convert/index.js +23 -0
  48. package/dist/packlets/context/index.js +26 -0
  49. package/dist/packlets/decisions/abstractDecision.js +68 -0
  50. package/dist/packlets/decisions/abstractDecisionCollector.js +90 -0
  51. package/dist/packlets/decisions/candidate.js +76 -0
  52. package/dist/packlets/decisions/common.js +23 -0
  53. package/dist/packlets/decisions/concreteDecision.js +111 -0
  54. package/dist/packlets/decisions/decision.js +126 -0
  55. package/dist/packlets/decisions/index.js +28 -0
  56. package/dist/packlets/import/fsItem.js +134 -0
  57. package/dist/packlets/import/importContext.js +129 -0
  58. package/dist/packlets/import/importManager.js +129 -0
  59. package/dist/packlets/import/importable.js +32 -0
  60. package/dist/packlets/import/importers/collectionImporter.js +118 -0
  61. package/dist/packlets/import/importers/fsItemImporter.js +116 -0
  62. package/dist/packlets/import/importers/importer.js +23 -0
  63. package/dist/packlets/import/importers/index.browser.js +29 -0
  64. package/dist/packlets/import/importers/index.js +27 -0
  65. package/dist/packlets/import/importers/jsonImporter.js +94 -0
  66. package/dist/packlets/import/importers/pathImporter.js +84 -0
  67. package/dist/packlets/import/index.browser.js +34 -0
  68. package/dist/packlets/import/index.js +28 -0
  69. package/dist/packlets/qualifier-types/config/convert.js +124 -0
  70. package/dist/packlets/qualifier-types/config/index.js +25 -0
  71. package/dist/packlets/qualifier-types/config/json.js +32 -0
  72. package/dist/packlets/qualifier-types/convert.js +56 -0
  73. package/dist/packlets/qualifier-types/helpers.js +82 -0
  74. package/dist/packlets/qualifier-types/index.js +32 -0
  75. package/dist/packlets/qualifier-types/languageQualifierType.js +142 -0
  76. package/dist/packlets/qualifier-types/literalQualifierType.js +200 -0
  77. package/dist/packlets/qualifier-types/literalValueHierarchy.js +236 -0
  78. package/dist/packlets/qualifier-types/qualifierType.js +184 -0
  79. package/dist/packlets/qualifier-types/qualifierTypeCollector.js +66 -0
  80. package/dist/packlets/qualifier-types/territoryQualifierType.js +200 -0
  81. package/dist/packlets/qualifiers/convert/decls.js +70 -0
  82. package/dist/packlets/qualifiers/convert/index.js +24 -0
  83. package/dist/packlets/qualifiers/convert/qualifier.js +40 -0
  84. package/dist/packlets/qualifiers/index.js +29 -0
  85. package/dist/packlets/qualifiers/qualifier.js +108 -0
  86. package/dist/packlets/qualifiers/qualifierCollector.js +123 -0
  87. package/dist/packlets/qualifiers/qualifierDecl.js +23 -0
  88. package/dist/packlets/qualifiers/qualifierDefaultValueDecls.js +23 -0
  89. package/dist/packlets/qualifiers/qualifierDefaultValueToken.js +175 -0
  90. package/dist/packlets/resource-json/compiled/common.js +23 -0
  91. package/dist/packlets/resource-json/compiled/convert.js +132 -0
  92. package/dist/packlets/resource-json/compiled/index.js +26 -0
  93. package/dist/packlets/resource-json/compiled/json.js +2 -0
  94. package/dist/packlets/resource-json/convert.js +171 -0
  95. package/dist/packlets/resource-json/helpers.js +241 -0
  96. package/dist/packlets/resource-json/index.js +31 -0
  97. package/dist/packlets/resource-json/json.js +36 -0
  98. package/dist/packlets/resource-json/normalized.js +23 -0
  99. package/dist/packlets/resource-json/resourceDeclCollection.js +88 -0
  100. package/dist/packlets/resource-json/resourceDeclContainer.js +23 -0
  101. package/dist/packlets/resource-json/resourceDeclTree.js +94 -0
  102. package/dist/packlets/resource-types/config/convert.js +35 -0
  103. package/dist/packlets/resource-types/config/index.js +25 -0
  104. package/dist/packlets/resource-types/config/json.js +23 -0
  105. package/dist/packlets/resource-types/helpers.js +42 -0
  106. package/dist/packlets/resource-types/index.js +28 -0
  107. package/dist/packlets/resource-types/jsonResourceType.js +66 -0
  108. package/dist/packlets/resource-types/resourceType.js +95 -0
  109. package/dist/packlets/resource-types/resourceTypeCollector.js +61 -0
  110. package/dist/packlets/resources/candidateReducer.js +246 -0
  111. package/dist/packlets/resources/candidateValue.js +92 -0
  112. package/dist/packlets/resources/candidateValueCollector.js +94 -0
  113. package/dist/packlets/resources/common.js +23 -0
  114. package/dist/packlets/resources/deltaGenerator.js +338 -0
  115. package/dist/packlets/resources/index.js +31 -0
  116. package/dist/packlets/resources/resource.js +207 -0
  117. package/dist/packlets/resources/resourceBuilder.js +183 -0
  118. package/dist/packlets/resources/resourceCandidate.js +216 -0
  119. package/dist/packlets/resources/resourceManagerBuilder.js +890 -0
  120. package/dist/packlets/runtime/cacheListener.js +58 -0
  121. package/dist/packlets/runtime/cacheMetrics.js +149 -0
  122. package/dist/packlets/runtime/compiledResourceCollection.js +473 -0
  123. package/dist/packlets/runtime/conditionSetResolutionResult.js +134 -0
  124. package/dist/packlets/runtime/context/contextQualifierProvider.js +29 -0
  125. package/dist/packlets/runtime/context/contextQualifierProviderValidator.js +146 -0
  126. package/dist/packlets/runtime/context/index.js +26 -0
  127. package/dist/packlets/runtime/context/simpleContextQualifierProvider.js +196 -0
  128. package/dist/packlets/runtime/context/validatingSimpleContextQualifierProvider.js +60 -0
  129. package/dist/packlets/runtime/iResourceManager.js +23 -0
  130. package/dist/packlets/runtime/index.js +34 -0
  131. package/dist/packlets/runtime/resource-tree/common.js +23 -0
  132. package/dist/packlets/runtime/resource-tree/index.js +26 -0
  133. package/dist/packlets/runtime/resource-tree/readOnlyResourceTree.js +216 -0
  134. package/dist/packlets/runtime/resource-tree/resourceTreeChildren.js +97 -0
  135. package/dist/packlets/runtime/resource-tree/resourceTreeChildrenValidator.js +161 -0
  136. package/dist/packlets/runtime/resourceResolver.js +481 -0
  137. package/dist/packlets/runtime/resourceTreeResolver.js +220 -0
  138. package/dist/packlets/runtime/validate.js +47 -0
  139. package/dist/packlets/zip-archive/convert.js +100 -0
  140. package/dist/packlets/zip-archive/index.browser.js +54 -0
  141. package/dist/packlets/zip-archive/index.js +47 -0
  142. package/dist/packlets/zip-archive/json.js +23 -0
  143. package/dist/packlets/zip-archive/types.js +36 -0
  144. package/dist/packlets/zip-archive/zipArchiveCreator.js +191 -0
  145. package/dist/packlets/zip-archive/zipArchiveFormat.js +140 -0
  146. package/dist/packlets/zip-archive/zipArchiveLoader.js +282 -0
  147. package/dist/test/data/sample.json +32 -0
  148. package/dist/test/unit/qualifier-types/testQualifierType.js +76 -0
  149. package/dist/test/unit/resource-types/testDerivedResourceType.js +109 -0
  150. package/dist/test/unit/resources/deltaGenerator.helpers.js +282 -0
  151. package/dist/ts-res.d.ts +15 -15
  152. package/dist/tsdoc-metadata.json +1 -1
  153. package/lib/index.browser.d.ts +24 -0
  154. package/lib/index.browser.js +116 -0
  155. package/lib/packlets/common/helpers/resources.d.ts +1 -1
  156. package/lib/packlets/common/helpers/resources.js +3 -2
  157. package/lib/packlets/common/validate/conditions.js +14 -14
  158. package/lib/packlets/common/validate/resources.js +7 -7
  159. package/lib/packlets/conditions/conditionSet.js +1 -1
  160. package/lib/packlets/config/common.js +1 -0
  161. package/lib/packlets/config/configInitFactory.js +1 -0
  162. package/lib/packlets/config/index.browser.d.ts +7 -0
  163. package/lib/packlets/config/index.browser.js +74 -0
  164. package/lib/packlets/config/systemConfiguration.js +1 -0
  165. package/lib/packlets/context/convert/decls.js +1 -1
  166. package/lib/packlets/import/importers/collectionImporter.js +1 -1
  167. package/lib/packlets/import/importers/index.browser.d.ts +4 -0
  168. package/lib/packlets/import/importers/index.browser.js +45 -0
  169. package/lib/packlets/import/importers/jsonImporter.js +1 -1
  170. package/lib/packlets/import/index.browser.d.ts +7 -0
  171. package/lib/packlets/import/index.browser.js +74 -0
  172. package/lib/packlets/qualifier-types/helpers.js +5 -4
  173. package/lib/packlets/qualifier-types/languageQualifierType.d.ts +4 -4
  174. package/lib/packlets/qualifier-types/literalQualifierType.d.ts +4 -4
  175. package/lib/packlets/qualifier-types/literalQualifierType.js +1 -1
  176. package/lib/packlets/qualifier-types/literalValueHierarchy.js +7 -6
  177. package/lib/packlets/qualifier-types/qualifierType.d.ts +5 -5
  178. package/lib/packlets/qualifier-types/qualifierType.js +1 -0
  179. package/lib/packlets/qualifier-types/territoryQualifierType.d.ts +4 -4
  180. package/lib/packlets/qualifier-types/territoryQualifierType.js +1 -1
  181. package/lib/packlets/resource-json/helpers.js +4 -0
  182. package/lib/packlets/resource-types/helpers.js +1 -0
  183. package/lib/packlets/resource-types/resourceType.js +1 -0
  184. package/lib/packlets/resources/candidateValue.js +1 -0
  185. package/lib/packlets/resources/candidateValueCollector.js +4 -2
  186. package/lib/packlets/resources/deltaGenerator.js +8 -10
  187. package/lib/packlets/resources/resourceBuilder.js +4 -0
  188. package/lib/packlets/resources/resourceManagerBuilder.js +6 -0
  189. package/lib/packlets/runtime/compiledResourceCollection.js +1 -0
  190. package/lib/packlets/runtime/context/simpleContextQualifierProvider.js +4 -3
  191. package/lib/packlets/runtime/resource-tree/resourceTreeChildren.js +3 -0
  192. package/lib/packlets/runtime/resourceResolver.js +2 -4
  193. package/lib/packlets/runtime/resourceTreeResolver.js +2 -0
  194. package/lib/packlets/zip-archive/index.browser.d.ts +27 -0
  195. package/lib/packlets/zip-archive/index.browser.js +102 -0
  196. package/package.json +34 -20
@@ -0,0 +1,246 @@
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
+ import { ResourceCandidate } from './resourceCandidate';
23
+ import { JsonEditor } from '@fgv/ts-json';
24
+ import { succeed, fail, mapResults } from '@fgv/ts-utils';
25
+ /**
26
+ * Manages candidate reduction logic for filtering and qualifier reduction operations.
27
+ * Encapsulates the state and logic needed to consistently process candidates for reduction.
28
+ * @public
29
+ */
30
+ export class CandidateReducer {
31
+ /**
32
+ * Constructor for CandidateReducer.
33
+ * @param candidates - The set of candidates to potentially reduce
34
+ * @param filterForContext - Context to filter candidates against
35
+ */
36
+ constructor(candidates, filterForContext) {
37
+ var _a;
38
+ this._filterForContext = filterForContext;
39
+ // Use existing logic to determine reducible qualifiers
40
+ this._qualifiersToReduce =
41
+ (_a = ResourceCandidate.findReducibleQualifiers(candidates, filterForContext)) !== null && _a !== void 0 ? _a : new Set();
42
+ // Initialize candidate info array with initial reduction attempts
43
+ this._candidateInfos = candidates.map((candidate) => {
44
+ const filteredConditions = candidate.conditions.conditions.filter((c) => !this._qualifiersToReduce.has(c.qualifier.name));
45
+ const reducedConditions = filteredConditions.map((c) => c.toLooseConditionDecl());
46
+ const conditionSetKey = CandidateReducer._computeConditionSetKey(reducedConditions);
47
+ const action = filteredConditions.length < candidate.conditions.conditions.length ? 'reduced' : 'unchanged';
48
+ return {
49
+ originalCandidate: candidate,
50
+ action,
51
+ conditions: reducedConditions,
52
+ conditionSetKey,
53
+ json: undefined // Will be set later when needed
54
+ };
55
+ });
56
+ // Resolve collisions by reverting reduced candidates to unchanged when they collide
57
+ this._resolveCollisions();
58
+ }
59
+ /**
60
+ * Computes a string key for a set of condition declarations for collision detection.
61
+ * @param conditions - The conditions to compute a key for
62
+ * @returns A string key representing the condition set
63
+ */
64
+ static _computeConditionSetKey(conditions) {
65
+ if (conditions.length === 0) {
66
+ return '{}';
67
+ }
68
+ // Sort conditions by qualifier name for consistent keys
69
+ const sortedConditions = [...conditions].sort((a, b) => a.qualifierName.localeCompare(b.qualifierName));
70
+ const pairs = sortedConditions.map((c) => `${c.qualifierName}:${c.value}`);
71
+ return `{${pairs.join(',')}}`;
72
+ }
73
+ /**
74
+ * Merges a partial candidate's JSON value into a base candidate's JSON value.
75
+ * @param baseJson - The base JSON value to merge into
76
+ * @param partialJson - The partial JSON value to merge from
77
+ * @returns The merged JSON value
78
+ * @internal
79
+ */
80
+ static _mergeJsonValues(baseJson, partialJson) {
81
+ // Create JsonEditor with array replacement behavior for candidate merging
82
+ const editor = JsonEditor.create({
83
+ merge: {
84
+ arrayMergeBehavior: 'replace'
85
+ }
86
+ }).orThrow(); // Should never fail with valid options
87
+ return editor.mergeObjectsInPlace({}, [baseJson, partialJson]).orThrow(); // Internal method should throw on error - this indicates a serious bug
88
+ }
89
+ /**
90
+ * Resolves collisions using smart collision resolution rules.
91
+ * - If a reduced candidate collides with an unchanged candidate and only one reduced candidate collides:
92
+ * - If reduced candidate is complete: suppress unchanged, keep reduced
93
+ * - If reduced candidate is partial: merge into unchanged, suppress reduced
94
+ * - Otherwise: revert reduced candidates to unchanged to avoid collisions
95
+ * @internal
96
+ */
97
+ _resolveCollisions() {
98
+ let hasCollisions = true;
99
+ let iterationCount = 0;
100
+ const maxIterations = this._candidateInfos.length; // Safety limit
101
+ while (hasCollisions && iterationCount < maxIterations) {
102
+ hasCollisions = false;
103
+ iterationCount++;
104
+ // Group candidates by their condition set keys (excluding suppressed)
105
+ const conditionKeyGroups = new Map();
106
+ for (const candidateInfo of this._candidateInfos) {
107
+ if (candidateInfo.action !== 'suppressed') {
108
+ // Don't include suppressed candidates
109
+ const key = candidateInfo.conditionSetKey;
110
+ if (!conditionKeyGroups.has(key)) {
111
+ conditionKeyGroups.set(key, []);
112
+ }
113
+ conditionKeyGroups.get(key).push(candidateInfo);
114
+ }
115
+ }
116
+ // Apply smart collision resolution
117
+ for (const [, candidatesWithSameKey] of conditionKeyGroups) {
118
+ if (candidatesWithSameKey.length > 1) {
119
+ // Categorize candidates
120
+ const unchangedCandidates = candidatesWithSameKey.filter((c) => c.action === 'unchanged');
121
+ const reducedCandidates = candidatesWithSameKey.filter((c) => c.action === 'reduced');
122
+ if (unchangedCandidates.length === 1 && reducedCandidates.length === 1) {
123
+ // Simple case: one unchanged vs one reduced - apply smart resolution
124
+ const unchangedCandidate = unchangedCandidates[0];
125
+ const reducedCandidate = reducedCandidates[0];
126
+ if (!reducedCandidate.originalCandidate.isPartial) {
127
+ // Complete reduced candidate wins - suppress unchanged
128
+ unchangedCandidate.action = 'suppressed';
129
+ hasCollisions = true;
130
+ }
131
+ else {
132
+ /* c8 ignore next 12 - complex collision handling for partial candidates rarely triggered */
133
+ // Partial reduced candidate - merge into unchanged and suppress reduced
134
+ const mergedJson = CandidateReducer._mergeJsonValues(unchangedCandidate.originalCandidate.json, reducedCandidate.originalCandidate.json);
135
+ // Update unchanged candidate with merged JSON, keeping its isPartial status
136
+ unchangedCandidate.json = mergedJson;
137
+ // Suppress the reduced candidate
138
+ reducedCandidate.action = 'suppressed';
139
+ hasCollisions = true;
140
+ }
141
+ }
142
+ else {
143
+ /* c8 ignore next 15 - complex collision reversion logic rarely triggered in practice */
144
+ // Complex collision case - revert all reduced candidates to unchanged
145
+ for (const candidateInfo of reducedCandidates) {
146
+ // Revert to original conditions and mark as unchanged
147
+ const originalConditions = candidateInfo.originalCandidate.conditions.conditions.map((c) => c.toLooseConditionDecl());
148
+ const originalKey = CandidateReducer._computeConditionSetKey(originalConditions);
149
+ // Update the candidate info to revert to original state
150
+ candidateInfo.action = 'unchanged';
151
+ candidateInfo.conditions = originalConditions;
152
+ candidateInfo.conditionSetKey = originalKey;
153
+ hasCollisions = true;
154
+ }
155
+ }
156
+ }
157
+ }
158
+ }
159
+ }
160
+ /**
161
+ * Static convenience method to construct an array of properly reduced
162
+ * {@link ResourceJson.Json.IChildResourceCandidateDecl | child resource candidate declarations}
163
+ * from a set of {@link Resources.ResourceCandidate | resource candidates}.
164
+ * @param candidates - The candidates to reduce
165
+ * @param filterForContext - Optional context to filter against
166
+ * @returns Result with array of reduced candidate declarations, or Failure if reduction fails
167
+ */
168
+ static reduceToChildResourceCandidateDecls(candidates, filterForContext) {
169
+ if (!filterForContext || Object.keys(filterForContext).length === 0) {
170
+ return succeed(candidates.map((candidate) => candidate.toChildResourceCandidateDecl()));
171
+ }
172
+ const reducer = new CandidateReducer(candidates, filterForContext);
173
+ const reductionResults = mapResults(candidates.map((candidate) => reducer.reduceCandidate(candidate)));
174
+ return reductionResults.onSuccess((reductions) => {
175
+ const validReductions = reductions.filter((reduction) => reduction !== undefined);
176
+ return succeed(validReductions.map((reduction) => {
177
+ var _a, _b, _c;
178
+ return ({
179
+ /* c8 ignore next 3 -- defense in depth */
180
+ json: (_a = reduction.json) !== null && _a !== void 0 ? _a : reduction.candidate.json,
181
+ isPartial: (_b = reduction.isPartial) !== null && _b !== void 0 ? _b : reduction.candidate.isPartial,
182
+ mergeMethod: (_c = reduction.mergeMethod) !== null && _c !== void 0 ? _c : reduction.candidate.mergeMethod,
183
+ conditions: reduction.conditions
184
+ });
185
+ }));
186
+ });
187
+ }
188
+ /**
189
+ * Static convenience method to construct an array of properly reduced
190
+ * {@link ResourceJson.Json.ILooseResourceCandidateDecl | loose resource candidate declarations}
191
+ * from a set of {@link Resources.ResourceCandidate | resource candidates}.
192
+ * @param id - The id of the resource
193
+ * @param candidates - The candidates to reduce
194
+ * @param filterForContext - Optional context to filter against
195
+ * @returns Result with array of reduced candidate declarations, or Failure if reduction fails
196
+ */
197
+ static reduceToLooseResourceCandidateDecls(id, candidates, filterForContext) {
198
+ if (!filterForContext || Object.keys(filterForContext).length === 0) {
199
+ return succeed(candidates.map((candidate) => candidate.toLooseResourceCandidateDecl()));
200
+ }
201
+ const reducer = new CandidateReducer(candidates, filterForContext);
202
+ const reductionResults = mapResults(candidates.map((candidate) => reducer.reduceCandidate(candidate)));
203
+ return reductionResults.onSuccess((reductions) => {
204
+ const validReductions = reductions.filter((reduction) => reduction !== undefined);
205
+ return succeed(validReductions.map((reduction) => {
206
+ var _a, _b, _c;
207
+ return ({
208
+ id,
209
+ /* c8 ignore next 3 -- defense in depth */
210
+ json: (_a = reduction.json) !== null && _a !== void 0 ? _a : reduction.candidate.json,
211
+ isPartial: (_b = reduction.isPartial) !== null && _b !== void 0 ? _b : reduction.candidate.isPartial,
212
+ mergeMethod: (_c = reduction.mergeMethod) !== null && _c !== void 0 ? _c : reduction.candidate.mergeMethod,
213
+ conditions: reduction.conditions
214
+ });
215
+ }));
216
+ });
217
+ }
218
+ /**
219
+ * Reduces a single candidate according to the configured reduction rules.
220
+ * @param candidate - The candidate to reduce
221
+ * @returns Either a reduced candidate declaration or an error if the candidate is not found
222
+ */
223
+ reduceCandidate(candidate) {
224
+ var _a;
225
+ const candidateInfo = this._candidateInfos.find((info) => info.originalCandidate === candidate);
226
+ /* c8 ignore next 3 - defensive coding: candidate should always be found if properly managed */
227
+ if (!candidateInfo) {
228
+ return fail(`Candidate not found in reducer state`);
229
+ }
230
+ if (candidateInfo.action === 'suppressed') {
231
+ return succeed(undefined);
232
+ }
233
+ // Convert array of conditions back to ConditionSetDecl for compatibility
234
+ const conditionsAsRecord = candidateInfo.conditions.reduce((acc, condition) => {
235
+ return Object.assign(Object.assign({}, acc), { [condition.qualifierName]: condition.value });
236
+ }, {});
237
+ return succeed({
238
+ candidate,
239
+ conditions: conditionsAsRecord,
240
+ json: (_a = candidateInfo.json) !== null && _a !== void 0 ? _a : candidate.json,
241
+ isPartial: candidate.isPartial,
242
+ mergeMethod: candidate.mergeMethod
243
+ });
244
+ }
245
+ }
246
+ //# sourceMappingURL=candidateReducer.js.map
@@ -0,0 +1,92 @@
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
+ import { Collections, Hash, captureResult } from '@fgv/ts-utils';
23
+ import { Convert as CommonConverters } from '../common';
24
+ import * as Common from '../common';
25
+ /**
26
+ * Implementation of a candidate value that stores normalized JSON data.
27
+ * The value is normalized on creation and a hash-based key is generated
28
+ * for efficient deduplication.
29
+ * @public
30
+ */
31
+ export class CandidateValue {
32
+ /**
33
+ * The unique key for this candidate value.
34
+ */
35
+ get key() {
36
+ return this._collectible.key;
37
+ }
38
+ /**
39
+ * The index of this candidate value in the collection.
40
+ */
41
+ get index() {
42
+ return this._collectible.index;
43
+ }
44
+ /**
45
+ * The normalized JSON value.
46
+ */
47
+ get json() {
48
+ return this._json;
49
+ }
50
+ /**
51
+ * Constructor for a {@link Resources.CandidateValue} object.
52
+ * @param params - Parameters to create the candidate value.
53
+ * @internal
54
+ */
55
+ constructor(params) {
56
+ var _a;
57
+ /* c8 ignore next 1 - defense in depth */
58
+ const normalizer = (_a = params.normalizer) !== null && _a !== void 0 ? _a : new Hash.Crc32Normalizer();
59
+ // Normalize the JSON value
60
+ const normalizedResult = normalizer.normalize(params.json);
61
+ this._json = normalizedResult.orThrow();
62
+ // Generate key from the normalized value using computeHash
63
+ const keyResult = normalizer.computeHash(this._json);
64
+ const key = CommonConverters.candidateValueKey.convert(keyResult.orThrow()).orThrow();
65
+ // Create the collectible with the key and optional index
66
+ this._collectible = new Collections.Collectible({
67
+ key,
68
+ index: params.index,
69
+ indexConverter: Common.Convert.candidateValueIndex
70
+ });
71
+ }
72
+ /**
73
+ * Creates a new {@link Resources.CandidateValue} object.
74
+ * @param params - Parameters to create the candidate value.
75
+ * @returns `Success` with the new candidate value if successful,
76
+ * or `Failure` with an error message if not.
77
+ * @public
78
+ */
79
+ static create(params) {
80
+ return captureResult(() => new CandidateValue(params));
81
+ }
82
+ /**
83
+ * Sets the index of this candidate value.
84
+ * @param index - The index to set.
85
+ * @returns `Success` with the index if successful, or `Failure` with an error message if not.
86
+ * @public
87
+ */
88
+ setIndex(index) {
89
+ return this._collectible.setIndex(index);
90
+ }
91
+ }
92
+ //# sourceMappingURL=candidateValue.js.map
@@ -0,0 +1,94 @@
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
+ import { Validators as JsonValidators } from '@fgv/ts-json-base';
23
+ import { Collections, Hash, captureResult, succeed, ValidatingCollector } from '@fgv/ts-utils';
24
+ import { Convert as CommonConvert } from '../common';
25
+ import { CandidateValue } from './candidateValue';
26
+ /**
27
+ * A `ValidatingCollector` for {@link Resources.CandidateValue | CandidateValues},
28
+ * which collects candidate values supplied as either {@link Resources.CandidateValue | CandidateValue} or
29
+ * `JsonValue`.
30
+ * @public
31
+ */
32
+ export class CandidateValueCollector extends ValidatingCollector {
33
+ /**
34
+ * Constructor for a {@link Resources.CandidateValueCollector} object.
35
+ * @param params - Parameters to create the collector.
36
+ * @internal
37
+ */
38
+ constructor(params) {
39
+ var _a, _b;
40
+ super({
41
+ converters: new Collections.KeyValueConverters({
42
+ key: CommonConvert.candidateValueKey,
43
+ value: (from) => this._toCandidateValue(from)
44
+ })
45
+ });
46
+ /* c8 ignore next 3 - defense in depth */
47
+ params = params !== null && params !== void 0 ? params : {};
48
+ this.normalizer = (_a = params.normalizer) !== null && _a !== void 0 ? _a : new Hash.Crc32Normalizer();
49
+ (_b = params.candidateValues) === null || _b === void 0 ? void 0 : _b.forEach((candidateValue) => this.validating.add(candidateValue));
50
+ }
51
+ /**
52
+ * Creates a new {@link Resources.CandidateValueCollector} object.
53
+ * @param params - Parameters to create the collector.
54
+ * @returns `Success` with the new collector if successful,
55
+ * or `Failure` with an error message if not.
56
+ * @public
57
+ */
58
+ static create(params) {
59
+ return captureResult(() => new CandidateValueCollector(params));
60
+ }
61
+ /**
62
+ * Returns an array of JSON values ordered by their indices.
63
+ * @returns Array of JsonValue objects in index order.
64
+ * @public
65
+ */
66
+ getValuesByIndex() {
67
+ // Use the base class valuesByIndex() method to get CandidateValues in index order,
68
+ // then extract their JSON values
69
+ return this.valuesByIndex().map((candidateValue) => candidateValue.json);
70
+ }
71
+ /**
72
+ * Converter method to handle CandidateValue | JsonValue.
73
+ * @param from - The value to convert.
74
+ * @returns `Success` with the CandidateValue if successful, or `Failure` with an error message if not.
75
+ * @internal
76
+ */
77
+ _toCandidateValue(from) {
78
+ /* c8 ignore next 3 - functional code tested but coverage intermittently missed */
79
+ if (from instanceof CandidateValue) {
80
+ return succeed(from);
81
+ }
82
+ // Validate as JsonValue and create new CandidateValue
83
+ return JsonValidators.jsonValue
84
+ .validate(from)
85
+ .withErrorFormat((msg) => `Invalid JSON value: ${msg}`)
86
+ .onSuccess((jsonValue) => {
87
+ return CandidateValue.create({
88
+ json: jsonValue,
89
+ normalizer: this.normalizer
90
+ });
91
+ });
92
+ }
93
+ }
94
+ //# sourceMappingURL=candidateValueCollector.js.map
@@ -0,0 +1,23 @@
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
+ export {};
23
+ //# sourceMappingURL=common.js.map