@fgv/ts-res 5.0.1-8 → 5.0.1
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.
- package/dist/index.browser.js +53 -0
- package/dist/index.js +45 -0
- package/dist/packlets/bundle/bundleBuilder.js +130 -0
- package/dist/packlets/bundle/bundleLoader.js +101 -0
- package/dist/packlets/bundle/bundleNormalizer.js +138 -0
- package/dist/packlets/bundle/bundleUtils.js +113 -0
- package/dist/packlets/bundle/convert.js +75 -0
- package/dist/packlets/bundle/index.js +42 -0
- package/dist/packlets/bundle/model.js +23 -0
- package/dist/packlets/common/conditions.js +47 -0
- package/dist/packlets/common/convert.js +172 -0
- package/dist/packlets/common/helpers/conditions.js +66 -0
- package/dist/packlets/common/helpers/context.js +70 -0
- package/dist/packlets/common/helpers/index.js +26 -0
- package/dist/packlets/common/helpers/qualifierDefaultValues.js +66 -0
- package/dist/packlets/common/helpers/resources.js +93 -0
- package/dist/packlets/common/index.js +28 -0
- package/dist/packlets/common/resources.js +27 -0
- package/dist/packlets/common/validate/conditions.js +544 -0
- package/dist/packlets/common/validate/index.js +26 -0
- package/dist/packlets/common/validate/regularExpressions.js +84 -0
- package/dist/packlets/common/validate/resources.js +209 -0
- package/dist/packlets/conditions/condition.js +252 -0
- package/dist/packlets/conditions/conditionCollector.js +69 -0
- package/dist/packlets/conditions/conditionDecls.js +23 -0
- package/dist/packlets/conditions/conditionSet.js +247 -0
- package/dist/packlets/conditions/conditionSetCollector.js +89 -0
- package/dist/packlets/conditions/conditionSetDecls.js +23 -0
- package/dist/packlets/conditions/conditionToken.js +147 -0
- package/dist/packlets/conditions/convert/conditionSetDecls.js +51 -0
- package/dist/packlets/conditions/convert/decls.js +75 -0
- package/dist/packlets/conditions/convert/index.js +24 -0
- package/dist/packlets/conditions/index.js +31 -0
- package/dist/packlets/config/common.js +78 -0
- package/dist/packlets/config/configInitFactory.js +258 -0
- package/dist/packlets/config/convert.js +56 -0
- package/dist/packlets/config/index.browser.js +34 -0
- package/dist/packlets/config/index.js +28 -0
- package/dist/packlets/config/json.js +23 -0
- package/dist/packlets/config/predefined/default.js +138 -0
- package/dist/packlets/config/predefined/extended.js +190 -0
- package/dist/packlets/config/predefined/index.js +25 -0
- package/dist/packlets/config/systemConfiguration.js +147 -0
- package/dist/packlets/context/contextDecls.js +23 -0
- package/dist/packlets/context/contextToken.js +202 -0
- package/dist/packlets/context/convert/decls.js +91 -0
- package/dist/packlets/context/convert/index.js +23 -0
- package/dist/packlets/context/index.js +26 -0
- package/dist/packlets/decisions/abstractDecision.js +68 -0
- package/dist/packlets/decisions/abstractDecisionCollector.js +90 -0
- package/dist/packlets/decisions/candidate.js +76 -0
- package/dist/packlets/decisions/common.js +23 -0
- package/dist/packlets/decisions/concreteDecision.js +111 -0
- package/dist/packlets/decisions/decision.js +126 -0
- package/dist/packlets/decisions/index.js +28 -0
- package/dist/packlets/import/fsItem.js +134 -0
- package/dist/packlets/import/importContext.js +129 -0
- package/dist/packlets/import/importManager.js +129 -0
- package/dist/packlets/import/importable.js +32 -0
- package/dist/packlets/import/importers/collectionImporter.js +118 -0
- package/dist/packlets/import/importers/fsItemImporter.js +116 -0
- package/dist/packlets/import/importers/importer.js +23 -0
- package/dist/packlets/import/importers/index.browser.js +29 -0
- package/dist/packlets/import/importers/index.js +27 -0
- package/dist/packlets/import/importers/jsonImporter.js +94 -0
- package/dist/packlets/import/importers/pathImporter.js +84 -0
- package/dist/packlets/import/index.browser.js +34 -0
- package/dist/packlets/import/index.js +28 -0
- package/dist/packlets/qualifier-types/config/convert.js +124 -0
- package/dist/packlets/qualifier-types/config/index.js +25 -0
- package/dist/packlets/qualifier-types/config/json.js +32 -0
- package/dist/packlets/qualifier-types/convert.js +56 -0
- package/dist/packlets/qualifier-types/helpers.js +82 -0
- package/dist/packlets/qualifier-types/index.js +32 -0
- package/dist/packlets/qualifier-types/languageQualifierType.js +142 -0
- package/dist/packlets/qualifier-types/literalQualifierType.js +200 -0
- package/dist/packlets/qualifier-types/literalValueHierarchy.js +236 -0
- package/dist/packlets/qualifier-types/qualifierType.js +184 -0
- package/dist/packlets/qualifier-types/qualifierTypeCollector.js +66 -0
- package/dist/packlets/qualifier-types/territoryQualifierType.js +200 -0
- package/dist/packlets/qualifiers/convert/decls.js +70 -0
- package/dist/packlets/qualifiers/convert/index.js +24 -0
- package/dist/packlets/qualifiers/convert/qualifier.js +40 -0
- package/dist/packlets/qualifiers/index.js +29 -0
- package/dist/packlets/qualifiers/qualifier.js +108 -0
- package/dist/packlets/qualifiers/qualifierCollector.js +123 -0
- package/dist/packlets/qualifiers/qualifierDecl.js +23 -0
- package/dist/packlets/qualifiers/qualifierDefaultValueDecls.js +23 -0
- package/dist/packlets/qualifiers/qualifierDefaultValueToken.js +175 -0
- package/dist/packlets/resource-json/compiled/common.js +23 -0
- package/dist/packlets/resource-json/compiled/convert.js +132 -0
- package/dist/packlets/resource-json/compiled/index.js +26 -0
- package/dist/packlets/resource-json/compiled/json.js +2 -0
- package/dist/packlets/resource-json/convert.js +171 -0
- package/dist/packlets/resource-json/helpers.js +241 -0
- package/dist/packlets/resource-json/index.js +31 -0
- package/dist/packlets/resource-json/json.js +36 -0
- package/dist/packlets/resource-json/normalized.js +23 -0
- package/dist/packlets/resource-json/resourceDeclCollection.js +88 -0
- package/dist/packlets/resource-json/resourceDeclContainer.js +23 -0
- package/dist/packlets/resource-json/resourceDeclTree.js +94 -0
- package/dist/packlets/resource-types/config/convert.js +35 -0
- package/dist/packlets/resource-types/config/index.js +25 -0
- package/dist/packlets/resource-types/config/json.js +23 -0
- package/dist/packlets/resource-types/helpers.js +42 -0
- package/dist/packlets/resource-types/index.js +28 -0
- package/dist/packlets/resource-types/jsonResourceType.js +66 -0
- package/dist/packlets/resource-types/resourceType.js +95 -0
- package/dist/packlets/resource-types/resourceTypeCollector.js +61 -0
- package/dist/packlets/resources/candidateReducer.js +246 -0
- package/dist/packlets/resources/candidateValue.js +92 -0
- package/dist/packlets/resources/candidateValueCollector.js +94 -0
- package/dist/packlets/resources/common.js +23 -0
- package/dist/packlets/resources/deltaGenerator.js +338 -0
- package/dist/packlets/resources/index.js +31 -0
- package/dist/packlets/resources/resource.js +207 -0
- package/dist/packlets/resources/resourceBuilder.js +183 -0
- package/dist/packlets/resources/resourceCandidate.js +216 -0
- package/dist/packlets/resources/resourceManagerBuilder.js +890 -0
- package/dist/packlets/runtime/cacheListener.js +58 -0
- package/dist/packlets/runtime/cacheMetrics.js +149 -0
- package/dist/packlets/runtime/compiledResourceCollection.js +473 -0
- package/dist/packlets/runtime/conditionSetResolutionResult.js +134 -0
- package/dist/packlets/runtime/context/contextQualifierProvider.js +29 -0
- package/dist/packlets/runtime/context/contextQualifierProviderValidator.js +146 -0
- package/dist/packlets/runtime/context/index.js +26 -0
- package/dist/packlets/runtime/context/simpleContextQualifierProvider.js +196 -0
- package/dist/packlets/runtime/context/validatingSimpleContextQualifierProvider.js +60 -0
- package/dist/packlets/runtime/iResourceManager.js +23 -0
- package/dist/packlets/runtime/index.js +34 -0
- package/dist/packlets/runtime/resource-tree/common.js +23 -0
- package/dist/packlets/runtime/resource-tree/index.js +26 -0
- package/dist/packlets/runtime/resource-tree/readOnlyResourceTree.js +216 -0
- package/dist/packlets/runtime/resource-tree/resourceTreeChildren.js +97 -0
- package/dist/packlets/runtime/resource-tree/resourceTreeChildrenValidator.js +161 -0
- package/dist/packlets/runtime/resourceResolver.js +481 -0
- package/dist/packlets/runtime/resourceTreeResolver.js +220 -0
- package/dist/packlets/runtime/validate.js +47 -0
- package/dist/packlets/zip-archive/convert.js +100 -0
- package/dist/packlets/zip-archive/index.browser.js +54 -0
- package/dist/packlets/zip-archive/index.js +47 -0
- package/dist/packlets/zip-archive/json.js +23 -0
- package/dist/packlets/zip-archive/types.js +36 -0
- package/dist/packlets/zip-archive/zipArchiveCreator.js +191 -0
- package/dist/packlets/zip-archive/zipArchiveFormat.js +140 -0
- package/dist/packlets/zip-archive/zipArchiveLoader.js +282 -0
- package/dist/test/data/sample.json +32 -0
- package/dist/test/unit/qualifier-types/testQualifierType.js +76 -0
- package/dist/test/unit/resource-types/testDerivedResourceType.js +109 -0
- package/dist/test/unit/resources/deltaGenerator.helpers.js +282 -0
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/index.browser.d.ts +24 -0
- package/lib/index.browser.js +116 -0
- package/lib/packlets/config/index.browser.d.ts +7 -0
- package/lib/packlets/config/index.browser.js +74 -0
- package/lib/packlets/import/importers/index.browser.d.ts +4 -0
- package/lib/packlets/import/importers/index.browser.js +45 -0
- package/lib/packlets/import/index.browser.d.ts +7 -0
- package/lib/packlets/import/index.browser.js +74 -0
- package/lib/packlets/zip-archive/index.browser.d.ts +27 -0
- package/lib/packlets/zip-archive/index.browser.js +102 -0
- package/package.json +21 -7
|
@@ -0,0 +1,209 @@
|
|
|
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 { fail, succeed } from '@fgv/ts-utils';
|
|
23
|
+
import { candidateValueKey, identifier, segmentedIdentifier } from './regularExpressions';
|
|
24
|
+
/**
|
|
25
|
+
* Checks if the given name is a valid resource name.
|
|
26
|
+
*
|
|
27
|
+
* @param name - The name to validate.
|
|
28
|
+
* @returns `true` if the name is a valid resource name, otherwise `false`.
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
export function isValidResourceName(name) {
|
|
32
|
+
return identifier.test(name);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Checks if the given ID is a valid resource ID.
|
|
36
|
+
*
|
|
37
|
+
* @param id - The ID to validate.
|
|
38
|
+
* @returns `true` if the ID is a valid resource ID, otherwise `false`.
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
export function isValidResourceId(id) {
|
|
42
|
+
return segmentedIdentifier.test(id);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Checks if the given index is a valid resource index.
|
|
46
|
+
*
|
|
47
|
+
* @param index - The index to validate.
|
|
48
|
+
* @returns `true` if the index is a valid resource index, otherwise `false`.
|
|
49
|
+
* @public
|
|
50
|
+
*/
|
|
51
|
+
export function isValidResourceIndex(index) {
|
|
52
|
+
return index >= 0;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Checks if the given name is a valid resource type name.
|
|
56
|
+
*
|
|
57
|
+
* @param name - The name to validate.
|
|
58
|
+
* @returns `true` if the name is a valid resource type name, otherwise `false`.
|
|
59
|
+
* @public
|
|
60
|
+
*/
|
|
61
|
+
export function isValidResourceTypeName(name) {
|
|
62
|
+
return identifier.test(name);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Checks if the given index is a valid resource type index.
|
|
66
|
+
*
|
|
67
|
+
* @param index - The index to validate.
|
|
68
|
+
* @returns `true` if the index is a valid resource type index, otherwise `false`.
|
|
69
|
+
* @public
|
|
70
|
+
*/
|
|
71
|
+
export function isValidResourceTypeIndex(index) {
|
|
72
|
+
return index >= 0;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Checks if the given index is a valid candidate value index.
|
|
76
|
+
*
|
|
77
|
+
* @param index - The index to validate.
|
|
78
|
+
* @returns `true` if the index is a valid candidate value index, otherwise `false`.
|
|
79
|
+
* @public
|
|
80
|
+
*/
|
|
81
|
+
export function isValidCandidateValueIndex(index) {
|
|
82
|
+
return index >= 0;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Checks if the given key is a valid candidate value key.
|
|
86
|
+
*
|
|
87
|
+
* @param key - The key to validate.
|
|
88
|
+
* @returns `true` if the key is a valid candidate value key, otherwise `false`.
|
|
89
|
+
* @public
|
|
90
|
+
*/
|
|
91
|
+
export function isValidCandidateValueKey(key) {
|
|
92
|
+
return candidateValueKey.test(key);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Converts a string to a {@link ResourceName | resource name}.
|
|
96
|
+
*
|
|
97
|
+
* @param id - The string to convert.
|
|
98
|
+
* @returns `Success` with the converted name if valid, or `Failure` with an error message
|
|
99
|
+
* if not.
|
|
100
|
+
* @public
|
|
101
|
+
*/
|
|
102
|
+
export function toResourceName(name) {
|
|
103
|
+
/* c8 ignore next 3 - coverage having issues */
|
|
104
|
+
if (!isValidResourceName(name)) {
|
|
105
|
+
return fail(`${name}: invalid resource name.`);
|
|
106
|
+
}
|
|
107
|
+
return succeed(name);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Converts a string to a {@link ResourceId | resource ID}.
|
|
111
|
+
*
|
|
112
|
+
* @param id - The string to convert.
|
|
113
|
+
* @returns `Success` with the converted ID if valid, or `Failure` with an error message
|
|
114
|
+
* if not.
|
|
115
|
+
* @public
|
|
116
|
+
*/
|
|
117
|
+
export function toResourceId(id) {
|
|
118
|
+
/* c8 ignore next 3 - defensive coding: resource ID validation should prevent invalid IDs */
|
|
119
|
+
if (!isValidResourceId(id)) {
|
|
120
|
+
return fail(`${id}: invalid resource ID.`);
|
|
121
|
+
}
|
|
122
|
+
return succeed(id);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Converts an optional string to an optional {@link ResourceId | resource ID}.
|
|
126
|
+
*
|
|
127
|
+
* @param id - The string to convert.
|
|
128
|
+
* @returns `Success` with the converted ID if valid, or `Failure` with an error message
|
|
129
|
+
* if not.
|
|
130
|
+
* @public
|
|
131
|
+
*/
|
|
132
|
+
export function toOptionalResourceId(id) {
|
|
133
|
+
return id ? toResourceId(id) : succeed(undefined);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Converts a number to a {@link ResourceIndex | resource index}.
|
|
137
|
+
*
|
|
138
|
+
* @param index - The number to convert.
|
|
139
|
+
* @returns `Success` with the converted index if valid, or `Failure` with an error message
|
|
140
|
+
* if not.
|
|
141
|
+
* @public
|
|
142
|
+
*/
|
|
143
|
+
export function toResourceIndex(index) {
|
|
144
|
+
if (!isValidResourceIndex(index)) {
|
|
145
|
+
return fail(`${index}: invalid resource index.`);
|
|
146
|
+
}
|
|
147
|
+
return succeed(index);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Converts a string to a {@link ResourceTypeName | resource type name}.
|
|
151
|
+
*
|
|
152
|
+
* @param name - The string to convert.
|
|
153
|
+
* @returns `Success` with the converted name if valid, or `Failure` with an error message
|
|
154
|
+
* if not.
|
|
155
|
+
* @public
|
|
156
|
+
*/
|
|
157
|
+
export function toResourceTypeName(name) {
|
|
158
|
+
/* c8 ignore next 3 - coverage having issues */
|
|
159
|
+
if (!isValidResourceTypeName(name)) {
|
|
160
|
+
return fail(`${name}: invalid resource type name.`);
|
|
161
|
+
}
|
|
162
|
+
return succeed(name);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Converts a number to a {@link ResourceTypeIndex | resource type index}.
|
|
166
|
+
*
|
|
167
|
+
* @param index - The number to convert.
|
|
168
|
+
* @returns `Success` with the converted index if valid, or `Failure` with an error message
|
|
169
|
+
* if not.
|
|
170
|
+
* @public
|
|
171
|
+
*/
|
|
172
|
+
export function toResourceTypeIndex(index) {
|
|
173
|
+
/* c8 ignore next 3 - coverage having issues */
|
|
174
|
+
if (!isValidResourceTypeIndex(index)) {
|
|
175
|
+
return fail(`${index}: invalid resource type index.`);
|
|
176
|
+
}
|
|
177
|
+
return succeed(index);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Converts a string to a {@link CandidateValueKey | candidate value key}.
|
|
181
|
+
*
|
|
182
|
+
* @param key - The key to convert.
|
|
183
|
+
* @returns `Success` with the converted key if valid, or `Failure` with an error message
|
|
184
|
+
* if not.
|
|
185
|
+
* @public
|
|
186
|
+
*/
|
|
187
|
+
export function toCandidateValueKey(key) {
|
|
188
|
+
/* c8 ignore next 3 - coverage having issues */
|
|
189
|
+
if (!isValidCandidateValueKey(key)) {
|
|
190
|
+
return fail(`${key}: invalid candidate value key.`);
|
|
191
|
+
}
|
|
192
|
+
return succeed(key);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Converts a number to a {@link CandidateValueIndex | candidate value index}.
|
|
196
|
+
*
|
|
197
|
+
* @param index - The number to convert.
|
|
198
|
+
* @returns `Success` with the converted index if valid, or `Failure` with an error message
|
|
199
|
+
* if not.
|
|
200
|
+
* @public
|
|
201
|
+
*/
|
|
202
|
+
export function toCandidateValueIndex(index) {
|
|
203
|
+
/* c8 ignore next 3 - coverage having issues */
|
|
204
|
+
if (!isValidCandidateValueIndex(index)) {
|
|
205
|
+
return fail(`${index}: invalid candidate value index.`);
|
|
206
|
+
}
|
|
207
|
+
return succeed(index);
|
|
208
|
+
}
|
|
209
|
+
//# sourceMappingURL=resources.js.map
|
|
@@ -0,0 +1,252 @@
|
|
|
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 { captureResult, Collections, fail } from '@fgv/ts-utils';
|
|
23
|
+
import { Convert as CommonConvert, NoMatch, Validate } from '../common';
|
|
24
|
+
// eslint-disable-next-line @rushstack/typedef-var
|
|
25
|
+
const scoreFormatter = new Intl.NumberFormat('en-US', { minimumFractionDigits: 0, maximumFractionDigits: 3 });
|
|
26
|
+
/**
|
|
27
|
+
* Represents a single condition applied to some resource instance.
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export class Condition {
|
|
31
|
+
/**
|
|
32
|
+
* Constructs a new {@link Conditions.Condition | Condition} object.
|
|
33
|
+
* @param qualifier - The {@link Qualifiers.Qualifier | qualifier} used in this condition.
|
|
34
|
+
* @param value - The value to be matched in this condition.
|
|
35
|
+
* @param operator - The {@link ConditionOperator | operator} used when matching context value to condition value.
|
|
36
|
+
* @param priority - The {@link ConditionPriority | relative priority} of this condition.
|
|
37
|
+
* @param scoreAsDefault - The {@link QualifierMatchScore | score} to be used when this condition is the default.
|
|
38
|
+
* @param index - The index of this condition.
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
constructor({ qualifier, value, operator, priority, scoreAsDefault, index }) {
|
|
42
|
+
if (scoreAsDefault === undefined && qualifier.defaultValue !== undefined) {
|
|
43
|
+
/* c8 ignore next 4 - edge case: default value matching logic rarely triggered */
|
|
44
|
+
const defaultMatch = qualifier.type.matches(value, qualifier.defaultValue, 'matches');
|
|
45
|
+
if (defaultMatch !== NoMatch) {
|
|
46
|
+
scoreAsDefault = defaultMatch;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
this.qualifier = qualifier;
|
|
50
|
+
this.operator = operator;
|
|
51
|
+
this.value = qualifier.validateCondition(value, operator).orThrow();
|
|
52
|
+
this.priority = priority;
|
|
53
|
+
this.scoreAsDefault = scoreAsDefault;
|
|
54
|
+
this._collectible = new Collections.Collectible({
|
|
55
|
+
key: this.toKey(),
|
|
56
|
+
index,
|
|
57
|
+
indexConverter: CommonConvert.conditionIndex
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
get key() {
|
|
61
|
+
return this._collectible.key;
|
|
62
|
+
}
|
|
63
|
+
get index() {
|
|
64
|
+
return this._collectible.index;
|
|
65
|
+
}
|
|
66
|
+
setIndex(index) {
|
|
67
|
+
return this._collectible.setIndex(index);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Creates a new {@link Conditions.Condition | Condition} object from the supplied
|
|
71
|
+
* {@link Conditions.IValidatedConditionDecl | validated condition declaration}.
|
|
72
|
+
* @param decl - The {@link Conditions.IValidatedConditionDecl | validated condition declaration}
|
|
73
|
+
* describing the condition to create.
|
|
74
|
+
* @returns `Success` with the new {@link Conditions.Condition | Condition} if successful,
|
|
75
|
+
* `Failure` otherwise.
|
|
76
|
+
* @public
|
|
77
|
+
*/
|
|
78
|
+
static create(decl) {
|
|
79
|
+
return captureResult(() => new Condition(decl));
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Determines if this condition matches the supplied {@link Context.IValidatedContextDecl | validated context}.
|
|
83
|
+
* @param context - The {@link Context.IValidatedContextDecl | context} to match against.
|
|
84
|
+
* @param options - The {@link Context.IContextMatchOptions | options} to use when matching the context.
|
|
85
|
+
* @returns A {@link QualifierMatchScore | match score} indicating match quality if the condition is present
|
|
86
|
+
* in the context to be matched, `undefined` otherwise.
|
|
87
|
+
* @remarks
|
|
88
|
+
* If {@link Context.IContextMatchOptions.partialContextMatch | `options.partialContextMatch``} is `true`, then
|
|
89
|
+
* the method will return `undefined` if the corresponding qualifier is not present in the context.
|
|
90
|
+
*/
|
|
91
|
+
getContextMatch(context, options) {
|
|
92
|
+
options = options !== null && options !== void 0 ? options : {};
|
|
93
|
+
if (this.qualifier.name in context) {
|
|
94
|
+
const contextValue = context[this.qualifier.name];
|
|
95
|
+
const match = this.qualifier.type.matches(this.value, contextValue, this.operator);
|
|
96
|
+
if (match === NoMatch && options.acceptDefaultScore === true && this.scoreAsDefault !== undefined) {
|
|
97
|
+
return this.scoreAsDefault;
|
|
98
|
+
}
|
|
99
|
+
return match;
|
|
100
|
+
}
|
|
101
|
+
return options.partialContextMatch === true ? undefined : NoMatch;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Determines if this condition can match the supplied context, even if the context is partial.
|
|
105
|
+
*
|
|
106
|
+
* Returns true if:
|
|
107
|
+
* - The qualifier specified in the condition is not present in the context
|
|
108
|
+
* - The qualifier is present and matches the condition
|
|
109
|
+
*
|
|
110
|
+
* Returns false if:
|
|
111
|
+
* - The qualifier is present in the context and does not match the condition
|
|
112
|
+
*
|
|
113
|
+
* @param context - The context to match against.
|
|
114
|
+
* @param options - Options to use when matching the context.
|
|
115
|
+
* @returns `true` if the condition can match the (possibly partial) context, `false` otherwise.
|
|
116
|
+
* @public
|
|
117
|
+
*/
|
|
118
|
+
canMatchPartialContext(context, options) {
|
|
119
|
+
var _a;
|
|
120
|
+
/* c8 ignore next 1 - defense in depth */
|
|
121
|
+
const scoreAsDefault = (_a = this.scoreAsDefault) !== null && _a !== void 0 ? _a : NoMatch;
|
|
122
|
+
if (!(this.qualifier.name in context) || scoreAsDefault > NoMatch) {
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
return this.qualifier.type.isPotentialMatch(this.value, context[this.qualifier.name]);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Compares two conditions for sorting purposes.
|
|
129
|
+
* @param c1 - The first {@link Conditions.Condition | condition} to compare.
|
|
130
|
+
* @param c2 - The second {@link Conditions.Condition | condition} to compare.
|
|
131
|
+
* @returns A negative number if c1 should come before c2, a positive number
|
|
132
|
+
* if c2 should come before c1, or zero if they are equivalent.
|
|
133
|
+
* @public
|
|
134
|
+
*/
|
|
135
|
+
static compare(c1, c2) {
|
|
136
|
+
var _a, _b;
|
|
137
|
+
let diff = c1.priority - c2.priority;
|
|
138
|
+
diff = diff === 0 ? ((_a = c1.scoreAsDefault) !== null && _a !== void 0 ? _a : 0) - ((_b = c2.scoreAsDefault) !== null && _b !== void 0 ? _b : 0) : diff;
|
|
139
|
+
diff = diff === 0 ? c1.qualifier.name.localeCompare(c2.qualifier.name) : diff;
|
|
140
|
+
diff = diff === 0 ? c1.value.localeCompare(c2.value) : diff;
|
|
141
|
+
return diff;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Gets a {@link ConditionToken | condition token} for this condition, if possible.
|
|
145
|
+
* It is not possible to get a token for a condition with an operator other than `matches`,
|
|
146
|
+
* with other-than-default priority, or with a name or value that contains other than alphanumeric
|
|
147
|
+
* characters, underscore or non-leading hyphen.
|
|
148
|
+
* @param terse - if `true` and if the qualifier token is optional, the token will be omitted
|
|
149
|
+
* from the generated {@link ConditionToken | condition token}.
|
|
150
|
+
* @returns
|
|
151
|
+
*/
|
|
152
|
+
toToken(terse) {
|
|
153
|
+
var _a;
|
|
154
|
+
/* c8 ignore next 3 - defense in depth very difficult to induce */
|
|
155
|
+
if (this.operator !== 'matches') {
|
|
156
|
+
return fail(`${this.operator}: cannot create condition token for operator other than 'matches'`);
|
|
157
|
+
}
|
|
158
|
+
if (this.priority !== this.qualifier.defaultPriority) {
|
|
159
|
+
return fail(`${this.priority}: cannot create condition token for non-default priority`);
|
|
160
|
+
}
|
|
161
|
+
if (terse && this.qualifier.tokenIsOptional) {
|
|
162
|
+
return Validate.toConditionToken(this.value);
|
|
163
|
+
}
|
|
164
|
+
/* c8 ignore next 1 - coverage having a bad day */
|
|
165
|
+
const name = (_a = this.qualifier.token) !== null && _a !== void 0 ? _a : this.qualifier.name;
|
|
166
|
+
return Validate.toConditionToken(`${name}=${this.value}`);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Gets the {@link ConditionKey | key} for this condition.
|
|
170
|
+
* @returns -
|
|
171
|
+
*/
|
|
172
|
+
toKey() {
|
|
173
|
+
return Condition.getKeyForDecl(this).orThrow();
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Get a human-readable string representation of the condition.
|
|
177
|
+
* @returns A string representation of the condition.
|
|
178
|
+
* @public
|
|
179
|
+
*/
|
|
180
|
+
toString() {
|
|
181
|
+
return this.toKey();
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Gets the {@link ResourceJson.Json.IChildConditionDecl | child condition declaration} for this condition.
|
|
185
|
+
* @param options - The {@link ResourceJson.Helpers.IDeclarationOptions | options} to use when creating the child
|
|
186
|
+
* condition declaration.
|
|
187
|
+
* @returns The {@link ResourceJson.Json.IChildConditionDecl | child condition declaration} for this condition.
|
|
188
|
+
* @public
|
|
189
|
+
*/
|
|
190
|
+
toChildConditionDecl(options) {
|
|
191
|
+
const showDefaults = (options === null || options === void 0 ? void 0 : options.showDefaults) === true;
|
|
192
|
+
return Object.assign(Object.assign(Object.assign({ value: this.value }, (showDefaults || this.operator !== 'matches' ? { operator: this.operator } : {})), (showDefaults || this.priority !== this.qualifier.defaultPriority
|
|
193
|
+
? { priority: this.priority }
|
|
194
|
+
: {})), (this.scoreAsDefault ? { scoreAsDefault: this.scoreAsDefault } : {}));
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Gets the value for this condition, or the {@link ResourceJson.Json.IChildConditionDecl | child condition declaration}
|
|
198
|
+
* if the condition has non-default operator, priority or a score as default.
|
|
199
|
+
* @param options - The {@link ResourceJson.Helpers.IDeclarationOptions | options} to use when creating the child
|
|
200
|
+
* condition declaration.
|
|
201
|
+
* @returns A string value for this condition, or the {@link ResourceJson.Json.IChildConditionDecl | child condition declaration}
|
|
202
|
+
* if the condition has non-default operator, priority or a score as default.
|
|
203
|
+
*/
|
|
204
|
+
toValueOrChildConditionDecl(options) {
|
|
205
|
+
if ((options === null || options === void 0 ? void 0 : options.showDefaults) !== true &&
|
|
206
|
+
this.operator === 'matches' &&
|
|
207
|
+
this.priority === this.qualifier.defaultPriority &&
|
|
208
|
+
this.scoreAsDefault === undefined) {
|
|
209
|
+
return this.value;
|
|
210
|
+
}
|
|
211
|
+
/* c8 ignore next 2 - unreachable: matches is the only real operator in practice */
|
|
212
|
+
return this.toChildConditionDecl(options);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Gets the {@link ResourceJson.Json.ILooseConditionDecl | loose condition declaration} for this condition.
|
|
216
|
+
* @param options - The {@link ResourceJson.Helpers.IDeclarationOptions | options} to use when creating the loose
|
|
217
|
+
* condition declaration.
|
|
218
|
+
* @returns The {@link ResourceJson.Json.ILooseConditionDecl | loose condition declaration} for this condition.
|
|
219
|
+
* @public
|
|
220
|
+
*/
|
|
221
|
+
toLooseConditionDecl(options) {
|
|
222
|
+
const showDefaults = (options === null || options === void 0 ? void 0 : options.showDefaults) === true;
|
|
223
|
+
return Object.assign(Object.assign(Object.assign({ qualifierName: this.qualifier.name, value: this.value }, (showDefaults || this.operator !== 'matches' ? { operator: this.operator } : {})), (showDefaults || this.priority !== this.qualifier.defaultPriority
|
|
224
|
+
? { priority: this.priority }
|
|
225
|
+
: {})), (this.scoreAsDefault ? { scoreAsDefault: this.scoreAsDefault } : {}));
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Converts this condition to a compiled condition representation.
|
|
229
|
+
* @param options - Optional compilation options controlling the output format.
|
|
230
|
+
* @returns A compiled condition object that can be used for serialization or runtime processing.
|
|
231
|
+
* @public
|
|
232
|
+
*/
|
|
233
|
+
toCompiled(options) {
|
|
234
|
+
/* c8 ignore next 1 - defense in depth */
|
|
235
|
+
const operator = this.operator === 'matches' ? undefined : this.operator;
|
|
236
|
+
return Object.assign({ qualifierIndex: this.qualifier.index, operator, value: this.value, priority: this.priority, scoreAsDefault: this.scoreAsDefault }, ((options === null || options === void 0 ? void 0 : options.includeMetadata) === true ? { metadata: { key: this.key } } : {}));
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Gets the {@link ConditionKey | condition key} for a supplied {@link Conditions.IValidatedConditionDecl | condition declaration}.
|
|
240
|
+
* @param decl - The {@link Conditions.IValidatedConditionDecl | condition declaration} for which to get the key.
|
|
241
|
+
* @returns `Success` with the condition key if successful, `Failure` otherwise.
|
|
242
|
+
* @public
|
|
243
|
+
*/
|
|
244
|
+
static getKeyForDecl(decl) {
|
|
245
|
+
const scoreAsDefault = decl.scoreAsDefault ? `(${scoreFormatter.format(decl.scoreAsDefault)})` : '';
|
|
246
|
+
const key = decl.operator === 'matches'
|
|
247
|
+
? `${decl.qualifier.name}-[${decl.value}]@${decl.priority}${scoreAsDefault}`
|
|
248
|
+
: `${decl.qualifier.name}-${decl.operator}-[${decl.value}]@${decl.priority}${scoreAsDefault}`;
|
|
249
|
+
return Validate.toConditionKey(key);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
//# sourceMappingURL=condition.js.map
|
|
@@ -0,0 +1,69 @@
|
|
|
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 { captureResult, Collections, succeed, ValidatingCollector } from '@fgv/ts-utils';
|
|
23
|
+
import { Condition } from './condition';
|
|
24
|
+
import { Convert as CommonConvert } from '../common';
|
|
25
|
+
import { validatedConditionDecl } from './convert';
|
|
26
|
+
/**
|
|
27
|
+
* A `ValidatingCollector` for {@link Conditions.Condition | Conditions},
|
|
28
|
+
* which collects conditions supplied as either {@link Conditions.Condition | Condition} or
|
|
29
|
+
* {@link Conditions.IConditionDecl | IConditionDecl}.
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
export class ConditionCollector extends ValidatingCollector {
|
|
33
|
+
/**
|
|
34
|
+
* Constructor for a {@link Conditions.ConditionCollector | ConditionCollector} object.
|
|
35
|
+
* @param params - Required {@link Conditions.IConditionCollectorCreateParams | parameters} for
|
|
36
|
+
* creating the collector.
|
|
37
|
+
*/
|
|
38
|
+
constructor(params) {
|
|
39
|
+
var _a;
|
|
40
|
+
super({
|
|
41
|
+
converters: new Collections.KeyValueConverters({
|
|
42
|
+
key: CommonConvert.conditionKey,
|
|
43
|
+
value: (from) => this._toCondition(from)
|
|
44
|
+
})
|
|
45
|
+
});
|
|
46
|
+
this.qualifiers = params.qualifiers;
|
|
47
|
+
/* c8 ignore next 1 - ? is defense in depth */
|
|
48
|
+
(_a = params.conditions) === null || _a === void 0 ? void 0 : _a.forEach((c) => this.validating.add(c).orThrow());
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Creates a new {@link Conditions.ConditionCollector | ConditionCollector} object.
|
|
52
|
+
* @param params - Required {@link Conditions.IConditionCollectorCreateParams | parameters} for
|
|
53
|
+
* creating the collector.
|
|
54
|
+
* @returns `Success` with the new collector if successful, or `Failure` with
|
|
55
|
+
* an error message if not.
|
|
56
|
+
*/
|
|
57
|
+
static create(params) {
|
|
58
|
+
return captureResult(() => new ConditionCollector(params));
|
|
59
|
+
}
|
|
60
|
+
_toCondition(from) {
|
|
61
|
+
if (from instanceof Condition) {
|
|
62
|
+
return succeed(from);
|
|
63
|
+
}
|
|
64
|
+
return validatedConditionDecl
|
|
65
|
+
.convert(from, { qualifiers: this.qualifiers, conditionIndex: this.size })
|
|
66
|
+
.onSuccess((c) => Condition.create(c));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=conditionCollector.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=conditionDecls.js.map
|