@fgv/ts-res 5.0.0-0 → 5.0.0-2
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/ts-res.d.ts +22 -0
- package/lib/packlets/conditions/condition.js +1 -1
- package/lib/packlets/conditions/condition.js.map +1 -1
- package/lib/packlets/qualifier-types/literalQualifierType.d.ts +4 -0
- package/lib/packlets/qualifier-types/literalQualifierType.d.ts.map +1 -1
- package/lib/packlets/qualifier-types/literalQualifierType.js +14 -0
- package/lib/packlets/qualifier-types/literalQualifierType.js.map +1 -1
- package/lib/packlets/qualifier-types/literalValueHierarchy.d.ts +7 -0
- package/lib/packlets/qualifier-types/literalValueHierarchy.d.ts.map +1 -1
- package/lib/packlets/qualifier-types/literalValueHierarchy.js +13 -0
- package/lib/packlets/qualifier-types/literalValueHierarchy.js.map +1 -1
- package/lib/packlets/qualifier-types/qualifierType.d.ts +11 -0
- package/lib/packlets/qualifier-types/qualifierType.d.ts.map +1 -1
- package/lib/packlets/qualifier-types/qualifierType.js +9 -0
- package/lib/packlets/qualifier-types/qualifierType.js.map +1 -1
- package/package.json +12 -12
package/dist/ts-res.d.ts
CHANGED
|
@@ -3784,6 +3784,13 @@ declare interface IQualifierType extends ICollectible<QualifierTypeName, Qualifi
|
|
|
3784
3784
|
* details otherwise.
|
|
3785
3785
|
*/
|
|
3786
3786
|
isValidContextValue(value: string): value is QualifierContextValue;
|
|
3787
|
+
/**
|
|
3788
|
+
* Determines if a supplied condition value is a potential match for a possible context value.
|
|
3789
|
+
* @param conditionValue - The condition value.
|
|
3790
|
+
* @param contextValue - The context value.
|
|
3791
|
+
* @returns `true` if the condition value is a potential match for the context value, `false` otherwise.
|
|
3792
|
+
*/
|
|
3793
|
+
isPotentialMatch(conditionValue: string, contextValue: string): boolean;
|
|
3787
3794
|
/**
|
|
3788
3795
|
* Validates that a value and optional operator are valid for use in a condition
|
|
3789
3796
|
* for qualifiers of this type.
|
|
@@ -5178,6 +5185,10 @@ declare class LiteralQualifierType extends QualifierType {
|
|
|
5178
5185
|
* @returns `true` if the value is a valid condition value, `false` otherwise.
|
|
5179
5186
|
*/
|
|
5180
5187
|
isValidConditionValue(value: string): value is QualifierConditionValue;
|
|
5188
|
+
/**
|
|
5189
|
+
* {@inheritdoc QualifierTypes.IQualifierType.isPotentialMatch}
|
|
5190
|
+
*/
|
|
5191
|
+
isPotentialMatch(conditionValue: string, contextValue: string): boolean;
|
|
5181
5192
|
/**
|
|
5182
5193
|
* {@inheritdoc QualifierTypes.QualifierType._matchOne}
|
|
5183
5194
|
*/
|
|
@@ -5277,6 +5288,13 @@ declare class LiteralValueHierarchy<T extends string = string> {
|
|
|
5277
5288
|
* to root, or `Failure` if the value is not in the hierarchy.
|
|
5278
5289
|
*/
|
|
5279
5290
|
getAncestors(value: T): Result<T[]>;
|
|
5291
|
+
/**
|
|
5292
|
+
* Determines if a value is an ancestor of a possible ancestor value.
|
|
5293
|
+
* @param value - The value to check.
|
|
5294
|
+
* @param possibleAncestor - The possible ancestor value.
|
|
5295
|
+
* @returns `true` if the value is an ancestor of the possible ancestor, `false` otherwise.
|
|
5296
|
+
*/
|
|
5297
|
+
isAncestor(value: T, possibleAncestor: T): boolean;
|
|
5280
5298
|
/**
|
|
5281
5299
|
* Gets all descendants of a value in the hierarchy.
|
|
5282
5300
|
* @param value - The value to get descendants for.
|
|
@@ -6068,6 +6086,10 @@ declare abstract class QualifierType implements IQualifierType {
|
|
|
6068
6086
|
* {@inheritdoc QualifierTypes.IQualifierType.isValidContextValue}
|
|
6069
6087
|
*/
|
|
6070
6088
|
isValidContextValue(value: string): value is QualifierContextValue;
|
|
6089
|
+
/**
|
|
6090
|
+
* {@inheritdoc QualifierTypes.IQualifierType.isPotentialMatch}
|
|
6091
|
+
*/
|
|
6092
|
+
isPotentialMatch(conditionValue: string, contextValue: string): boolean;
|
|
6071
6093
|
/**
|
|
6072
6094
|
* {@inheritdoc QualifierTypes.IQualifierType.validateCondition}
|
|
6073
6095
|
*/
|
|
@@ -124,7 +124,7 @@ class Condition {
|
|
|
124
124
|
if (!(this.qualifier.name in context) || scoreAsDefault > common_1.NoMatch) {
|
|
125
125
|
return true;
|
|
126
126
|
}
|
|
127
|
-
return this.
|
|
127
|
+
return this.qualifier.type.isPotentialMatch(this.value, context[this.qualifier.name]);
|
|
128
128
|
}
|
|
129
129
|
/**
|
|
130
130
|
* Compares two conditions for sorting purposes.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"condition.js","sourceRoot":"","sources":["../../../src/packlets/conditions/condition.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,4CAAyE;AACzE,sCAWmB;AAMnB,kDAAkD;AAClD,MAAM,cAAc,GAAG,IAAI,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE,EAAE,qBAAqB,EAAE,CAAC,EAAE,qBAAqB,EAAE,CAAC,EAAE,CAAC,CAAC;AAE9G;;;GAGG;AACH,MAAa,SAAS;IA4BpB;;;;;;;;;OASG;IACH,YAAsB,EACpB,SAAS,EACT,KAAK,EACL,QAAQ,EACR,QAAQ,EACR,cAAc,EACd,KAAK,EACmB;QACxB,IAAI,cAAc,KAAK,SAAS,IAAI,SAAS,CAAC,YAAY,KAAK,SAAS,EAAE,CAAC;YACzE,MAAM,YAAY,GAAG,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,SAAS,CAAC,YAAY,EAAE,SAAS,CAAC,CAAC;YACtF,IAAI,YAAY,KAAK,gBAAO,EAAE,CAAC;gBAC7B,cAAc,GAAG,YAAY,CAAC;YAChC,CAAC;QACH,CAAC;QACD,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC,iBAAiB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,OAAO,EAAE,CAAC;QACpE,IAAI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,KAAK,IAAI,CAAC,QAAQ,GAAG,cAAc,EAAE,CAAC;QAClG,OAAO,iBAAQ,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC;IACtC,CAAC;CACF;AA/SD,8BA+SC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { captureResult, Collections, Result, fail } from '@fgv/ts-utils';\nimport {\n Convert as CommonConvert,\n ConditionIndex,\n ConditionKey,\n ConditionOperator,\n ConditionPriority,\n ConditionToken,\n NoMatch,\n QualifierConditionValue,\n QualifierMatchScore,\n Validate\n} from '../common';\nimport { Qualifier } from '../qualifiers';\nimport { IValidatedConditionDecl } from './conditionDecls';\nimport * as ResourceJson from '../resource-json';\nimport * as Context from '../context';\n\n// eslint-disable-next-line @rushstack/typedef-var\nconst scoreFormatter = new Intl.NumberFormat('en-US', { minimumFractionDigits: 0, maximumFractionDigits: 3 });\n\n/**\n * Represents a single condition applied to some resource instance.\n * @public\n */\nexport class Condition implements IValidatedConditionDecl {\n /**\n * The {@link Qualifiers.Qualifier | qualifier} used in this condition.\n */\n public readonly qualifier: Qualifier;\n\n /**\n * The value to be matched in this condition.\n */\n public readonly value: QualifierConditionValue;\n\n /**\n * The {@link ConditionOperator | operator} used when matching context value to condition value.\n */\n public readonly operator: ConditionOperator;\n\n /**\n * The {@link ConditionPriority | relative priority} of this condition.\n */\n public readonly priority: ConditionPriority;\n\n /**\n * The {@link QualifierMatchScore | score} to be used when this condition is the default.\n */\n public readonly scoreAsDefault?: QualifierMatchScore;\n\n protected _collectible: Collections.Collectible<ConditionKey, ConditionIndex>;\n\n /**\n * Constructs a new {@link Conditions.Condition | Condition} object.\n * @param qualifier - The {@link Qualifiers.Qualifier | qualifier} used in this condition.\n * @param value - The value to be matched in this condition.\n * @param operator - The {@link ConditionOperator | operator} used when matching context value to condition value.\n * @param priority - The {@link ConditionPriority | relative priority} of this condition.\n * @param scoreAsDefault - The {@link QualifierMatchScore | score} to be used when this condition is the default.\n * @param index - The index of this condition.\n * @public\n */\n protected constructor({\n qualifier,\n value,\n operator,\n priority,\n scoreAsDefault,\n index\n }: IValidatedConditionDecl) {\n if (scoreAsDefault === undefined && qualifier.defaultValue !== undefined) {\n const defaultMatch = qualifier.type.matches(value, qualifier.defaultValue, 'matches');\n if (defaultMatch !== NoMatch) {\n scoreAsDefault = defaultMatch;\n }\n }\n this.qualifier = qualifier;\n this.operator = operator;\n this.value = qualifier.validateCondition(value, operator).orThrow();\n this.priority = priority;\n this.scoreAsDefault = scoreAsDefault;\n this._collectible = new Collections.Collectible({\n key: this.toKey(),\n index,\n indexConverter: CommonConvert.conditionIndex\n });\n }\n\n public get key(): ConditionKey {\n return this._collectible.key;\n }\n\n public get index(): ConditionIndex | undefined {\n return this._collectible.index;\n }\n\n public setIndex(index: ConditionIndex): Result<ConditionIndex> {\n return this._collectible.setIndex(index);\n }\n\n /**\n * Creates a new {@link Conditions.Condition | Condition} object from the supplied\n * {@link Conditions.IValidatedConditionDecl | validated condition declaration}.\n * @param decl - The {@link Conditions.IValidatedConditionDecl | validated condition declaration}\n * describing the condition to create.\n * @returns `Success` with the new {@link Conditions.Condition | Condition} if successful,\n * `Failure` otherwise.\n * @public\n */\n public static create(decl: IValidatedConditionDecl): Result<Condition> {\n return captureResult(() => new Condition(decl));\n }\n\n /**\n * Determines if this condition matches the supplied {@link Context.IValidatedContextDecl | validated context}.\n * @param context - The {@link Context.IValidatedContextDecl | context} to match against.\n * @param options - The {@link Context.IContextMatchOptions | options} to use when matching the context.\n * @returns A {@link QualifierMatchScore | match score} indicating match quality if the condition is present\n * in the context to be matched, `undefined` otherwise.\n * @remarks\n * If {@link Context.IContextMatchOptions.partialContextMatch | `options.partialContextMatch``} is `true`, then\n * the method will return `undefined` if the corresponding qualifier is not present in the context.\n */\n public getContextMatch(\n context: Context.IValidatedContextDecl,\n options?: Context.IContextMatchOptions\n ): QualifierMatchScore | undefined {\n options = options ?? {};\n if (this.qualifier.name in context) {\n const contextValue = context[this.qualifier.name];\n const match = this.qualifier.type.matches(this.value, contextValue, this.operator);\n if (match === NoMatch && options.acceptDefaultScore === true && this.scoreAsDefault !== undefined) {\n return this.scoreAsDefault;\n }\n return match;\n }\n return options.partialContextMatch === true ? undefined : NoMatch;\n }\n\n /**\n * Determines if this condition can match the supplied context, even if the context is partial.\n *\n * Returns true if:\n * - The qualifier specified in the condition is not present in the context\n * - The qualifier is present and matches the condition\n *\n * Returns false if:\n * - The qualifier is present in the context and does not match the condition\n *\n * @param context - The context to match against.\n * @param options - Options to use when matching the context.\n * @returns `true` if the condition can match the (possibly partial) context, `false` otherwise.\n * @public\n */\n public canMatchPartialContext(\n context: Context.IValidatedContextDecl,\n options?: Context.IContextMatchOptions\n ): boolean {\n /* c8 ignore next 1 - defense in depth */\n const scoreAsDefault = this.scoreAsDefault ?? NoMatch;\n if (!(this.qualifier.name in context) || scoreAsDefault > NoMatch) {\n return true;\n }\n return this.getContextMatch(context, options) !== NoMatch;\n }\n\n /**\n * Compares two conditions for sorting purposes.\n * @param c1 - The first {@link Conditions.Condition | condition} to compare.\n * @param c2 - The second {@link Conditions.Condition | condition} to compare.\n * @returns A negative number if c1 should come before c2, a positive number\n * if c2 should come before c1, or zero if they are equivalent.\n * @public\n */\n public static compare(c1: Condition, c2: Condition): number {\n let diff = c1.priority - c2.priority;\n diff = diff === 0 ? (c1.scoreAsDefault ?? 0) - (c2.scoreAsDefault ?? 0) : diff;\n diff = diff === 0 ? c1.qualifier.name.localeCompare(c2.qualifier.name) : diff;\n diff = diff === 0 ? c1.value.localeCompare(c2.value) : diff;\n return diff;\n }\n\n /**\n * Gets a {@link ConditionToken | condition token} for this condition, if possible.\n * It is not possible to get a token for a condition with an operator other than `matches`,\n * with other-than-default priority, or with a name or value that contains other than alphanumeric\n * characters, underscore or non-leading hyphen.\n * @param terse - if `true` and if the qualifier token is optional, the token will be omitted\n * from the generated {@link ConditionToken | condition token}.\n * @returns\n */\n public toToken(terse?: boolean): Result<ConditionToken> {\n /* c8 ignore next 3 - defense in depth very difficult to induce */\n if (this.operator !== 'matches') {\n return fail(`${this.operator}: cannot create condition token for operator other than 'matches'`);\n }\n if (this.priority !== this.qualifier.defaultPriority) {\n return fail(`${this.priority}: cannot create condition token for non-default priority`);\n }\n if (terse && this.qualifier.tokenIsOptional) {\n return Validate.toConditionToken(this.value);\n }\n /* c8 ignore next 1 - coverage having a bad day */\n const name = this.qualifier.token ?? this.qualifier.name;\n return Validate.toConditionToken(`${name}=${this.value}`);\n }\n\n /**\n * Gets the {@link ConditionKey | key} for this condition.\n * @returns -\n */\n public toKey(): ConditionKey {\n return Condition.getKeyForDecl(this).orThrow();\n }\n\n /**\n * Get a human-readable string representation of the condition.\n * @returns A string representation of the condition.\n * @public\n */\n public toString(): string {\n return this.toKey();\n }\n\n /**\n * Gets the {@link ResourceJson.Json.IChildConditionDecl | child condition declaration} for this condition.\n * @param options - The {@link ResourceJson.Helpers.IDeclarationOptions | options} to use when creating the child\n * condition declaration.\n * @returns The {@link ResourceJson.Json.IChildConditionDecl | child condition declaration} for this condition.\n * @public\n */\n public toChildConditionDecl(\n options?: ResourceJson.Helpers.IDeclarationOptions\n ): ResourceJson.Json.IChildConditionDecl {\n const showDefaults = options?.showDefaults === true;\n return {\n value: this.value,\n /* c8 ignore next 1 - not really possible to reproduce right now */\n ...(showDefaults || this.operator !== 'matches' ? { operator: this.operator } : {}),\n ...(showDefaults || this.priority !== this.qualifier.defaultPriority\n ? { priority: this.priority }\n : {}),\n ...(this.scoreAsDefault ? { scoreAsDefault: this.scoreAsDefault } : {})\n };\n }\n\n /**\n * Gets the value for this condition, or the {@link ResourceJson.Json.IChildConditionDecl | child condition declaration}\n * if the condition has non-default operator, priority or a score as default.\n * @param options - The {@link ResourceJson.Helpers.IDeclarationOptions | options} to use when creating the child\n * condition declaration.\n * @returns A string value for this condition, or the {@link ResourceJson.Json.IChildConditionDecl | child condition declaration}\n * if the condition has non-default operator, priority or a score as default.\n */\n public toValueOrChildConditionDecl(\n options?: ResourceJson.Helpers.IDeclarationOptions\n ): string | ResourceJson.Json.IChildConditionDecl {\n if (\n options?.showDefaults !== true &&\n this.operator === 'matches' &&\n this.priority === this.qualifier.defaultPriority &&\n this.scoreAsDefault === undefined\n ) {\n return this.value;\n }\n /* c8 ignore next 2 - unreachable: matches is the only real operator in practice */\n return this.toChildConditionDecl(options);\n }\n\n /**\n * Gets the {@link ResourceJson.Json.ILooseConditionDecl | loose condition declaration} for this condition.\n * @param options - The {@link ResourceJson.Helpers.IDeclarationOptions | options} to use when creating the loose\n * condition declaration.\n * @returns The {@link ResourceJson.Json.ILooseConditionDecl | loose condition declaration} for this condition.\n * @public\n */\n public toLooseConditionDecl(\n options?: ResourceJson.Helpers.IDeclarationOptions\n ): ResourceJson.Json.ILooseConditionDecl {\n const showDefaults = options?.showDefaults === true;\n return {\n qualifierName: this.qualifier.name,\n value: this.value,\n /* c8 ignore next 1 - not really possible to reproduce right now */\n ...(showDefaults || this.operator !== 'matches' ? { operator: this.operator } : {}),\n ...(showDefaults || this.priority !== this.qualifier.defaultPriority\n ? { priority: this.priority }\n : {}),\n ...(this.scoreAsDefault ? { scoreAsDefault: this.scoreAsDefault } : {})\n };\n }\n\n /**\n * Converts this condition to a compiled condition representation.\n * @param options - Optional compilation options controlling the output format.\n * @returns A compiled condition object that can be used for serialization or runtime processing.\n * @public\n */\n public toCompiled(\n options?: ResourceJson.Compiled.ICompiledResourceOptions\n ): ResourceJson.Compiled.ICompiledCondition {\n /* c8 ignore next 1 - defense in depth */\n const operator = this.operator === 'matches' ? undefined : this.operator;\n return {\n qualifierIndex: this.qualifier.index!,\n operator,\n value: this.value,\n priority: this.priority,\n scoreAsDefault: this.scoreAsDefault,\n ...(options?.includeMetadata === true ? { metadata: { key: this.key } } : {})\n };\n }\n\n /**\n * Gets the {@link ConditionKey | condition key} for a supplied {@link Conditions.IValidatedConditionDecl | condition declaration}.\n * @param decl - The {@link Conditions.IValidatedConditionDecl | condition declaration} for which to get the key.\n * @returns `Success` with the condition key if successful, `Failure` otherwise.\n * @public\n */\n public static getKeyForDecl(decl: IValidatedConditionDecl): Result<ConditionKey> {\n const scoreAsDefault = decl.scoreAsDefault ? `(${scoreFormatter.format(decl.scoreAsDefault)})` : '';\n const key =\n decl.operator === 'matches'\n ? `${decl.qualifier.name}-[${decl.value}]@${decl.priority}${scoreAsDefault}`\n : `${decl.qualifier.name}-${decl.operator}-[${decl.value}]@${decl.priority}${scoreAsDefault}`;\n return Validate.toConditionKey(key);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"condition.js","sourceRoot":"","sources":["../../../src/packlets/conditions/condition.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,4CAAyE;AACzE,sCAWmB;AAMnB,kDAAkD;AAClD,MAAM,cAAc,GAAG,IAAI,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE,EAAE,qBAAqB,EAAE,CAAC,EAAE,qBAAqB,EAAE,CAAC,EAAE,C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EAAE,IAAI,CAAC,cAAc,IAChC,CAAC,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,eAAe,MAAK,IAAI,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAC7E;IACJ,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,aAAa,CAAC,IAA6B;QACvD,MAAM,cAAc,GAAG,IAAI,CAAC,cAAc,CAAC,CAAC,CAAC,IAAI,cAAc,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;QACpG,MAAM,GAAG,GACP,IAAI,CAAC,QAAQ,KAAK,SAAS;YACzB,CAAC,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,KAAK,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC,QAAQ,GAAG,cAAc,EAAE;YAC5E,CAAC,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,IAAI,IAAI,CAAC,QAAQ,KAAK,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC,QAAQ,GAAG,cAAc,EAAE,CAAC;QAClG,OAAO,iBAAQ,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC;IACtC,CAAC;CACF;AA/SD,8BA+SC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { captureResult, Collections, Result, fail } from '@fgv/ts-utils';\nimport {\n Convert as CommonConvert,\n ConditionIndex,\n ConditionKey,\n ConditionOperator,\n ConditionPriority,\n ConditionToken,\n NoMatch,\n QualifierConditionValue,\n QualifierMatchScore,\n Validate\n} from '../common';\nimport { Qualifier } from '../qualifiers';\nimport { IValidatedConditionDecl } from './conditionDecls';\nimport * as ResourceJson from '../resource-json';\nimport * as Context from '../context';\n\n// eslint-disable-next-line @rushstack/typedef-var\nconst scoreFormatter = new Intl.NumberFormat('en-US', { minimumFractionDigits: 0, maximumFractionDigits: 3 });\n\n/**\n * Represents a single condition applied to some resource instance.\n * @public\n */\nexport class Condition implements IValidatedConditionDecl {\n /**\n * The {@link Qualifiers.Qualifier | qualifier} used in this condition.\n */\n public readonly qualifier: Qualifier;\n\n /**\n * The value to be matched in this condition.\n */\n public readonly value: QualifierConditionValue;\n\n /**\n * The {@link ConditionOperator | operator} used when matching context value to condition value.\n */\n public readonly operator: ConditionOperator;\n\n /**\n * The {@link ConditionPriority | relative priority} of this condition.\n */\n public readonly priority: ConditionPriority;\n\n /**\n * The {@link QualifierMatchScore | score} to be used when this condition is the default.\n */\n public readonly scoreAsDefault?: QualifierMatchScore;\n\n protected _collectible: Collections.Collectible<ConditionKey, ConditionIndex>;\n\n /**\n * Constructs a new {@link Conditions.Condition | Condition} object.\n * @param qualifier - The {@link Qualifiers.Qualifier | qualifier} used in this condition.\n * @param value - The value to be matched in this condition.\n * @param operator - The {@link ConditionOperator | operator} used when matching context value to condition value.\n * @param priority - The {@link ConditionPriority | relative priority} of this condition.\n * @param scoreAsDefault - The {@link QualifierMatchScore | score} to be used when this condition is the default.\n * @param index - The index of this condition.\n * @public\n */\n protected constructor({\n qualifier,\n value,\n operator,\n priority,\n scoreAsDefault,\n index\n }: IValidatedConditionDecl) {\n if (scoreAsDefault === undefined && qualifier.defaultValue !== undefined) {\n const defaultMatch = qualifier.type.matches(value, qualifier.defaultValue, 'matches');\n if (defaultMatch !== NoMatch) {\n scoreAsDefault = defaultMatch;\n }\n }\n this.qualifier = qualifier;\n this.operator = operator;\n this.value = qualifier.validateCondition(value, operator).orThrow();\n this.priority = priority;\n this.scoreAsDefault = scoreAsDefault;\n this._collectible = new Collections.Collectible({\n key: this.toKey(),\n index,\n indexConverter: CommonConvert.conditionIndex\n });\n }\n\n public get key(): ConditionKey {\n return this._collectible.key;\n }\n\n public get index(): ConditionIndex | undefined {\n return this._collectible.index;\n }\n\n public setIndex(index: ConditionIndex): Result<ConditionIndex> {\n return this._collectible.setIndex(index);\n }\n\n /**\n * Creates a new {@link Conditions.Condition | Condition} object from the supplied\n * {@link Conditions.IValidatedConditionDecl | validated condition declaration}.\n * @param decl - The {@link Conditions.IValidatedConditionDecl | validated condition declaration}\n * describing the condition to create.\n * @returns `Success` with the new {@link Conditions.Condition | Condition} if successful,\n * `Failure` otherwise.\n * @public\n */\n public static create(decl: IValidatedConditionDecl): Result<Condition> {\n return captureResult(() => new Condition(decl));\n }\n\n /**\n * Determines if this condition matches the supplied {@link Context.IValidatedContextDecl | validated context}.\n * @param context - The {@link Context.IValidatedContextDecl | context} to match against.\n * @param options - The {@link Context.IContextMatchOptions | options} to use when matching the context.\n * @returns A {@link QualifierMatchScore | match score} indicating match quality if the condition is present\n * in the context to be matched, `undefined` otherwise.\n * @remarks\n * If {@link Context.IContextMatchOptions.partialContextMatch | `options.partialContextMatch``} is `true`, then\n * the method will return `undefined` if the corresponding qualifier is not present in the context.\n */\n public getContextMatch(\n context: Context.IValidatedContextDecl,\n options?: Context.IContextMatchOptions\n ): QualifierMatchScore | undefined {\n options = options ?? {};\n if (this.qualifier.name in context) {\n const contextValue = context[this.qualifier.name];\n const match = this.qualifier.type.matches(this.value, contextValue, this.operator);\n if (match === NoMatch && options.acceptDefaultScore === true && this.scoreAsDefault !== undefined) {\n return this.scoreAsDefault;\n }\n return match;\n }\n return options.partialContextMatch === true ? undefined : NoMatch;\n }\n\n /**\n * Determines if this condition can match the supplied context, even if the context is partial.\n *\n * Returns true if:\n * - The qualifier specified in the condition is not present in the context\n * - The qualifier is present and matches the condition\n *\n * Returns false if:\n * - The qualifier is present in the context and does not match the condition\n *\n * @param context - The context to match against.\n * @param options - Options to use when matching the context.\n * @returns `true` if the condition can match the (possibly partial) context, `false` otherwise.\n * @public\n */\n public canMatchPartialContext(\n context: Context.IValidatedContextDecl,\n options?: Context.IContextMatchOptions\n ): boolean {\n /* c8 ignore next 1 - defense in depth */\n const scoreAsDefault = this.scoreAsDefault ?? NoMatch;\n if (!(this.qualifier.name in context) || scoreAsDefault > NoMatch) {\n return true;\n }\n return this.qualifier.type.isPotentialMatch(this.value, context[this.qualifier.name]);\n }\n\n /**\n * Compares two conditions for sorting purposes.\n * @param c1 - The first {@link Conditions.Condition | condition} to compare.\n * @param c2 - The second {@link Conditions.Condition | condition} to compare.\n * @returns A negative number if c1 should come before c2, a positive number\n * if c2 should come before c1, or zero if they are equivalent.\n * @public\n */\n public static compare(c1: Condition, c2: Condition): number {\n let diff = c1.priority - c2.priority;\n diff = diff === 0 ? (c1.scoreAsDefault ?? 0) - (c2.scoreAsDefault ?? 0) : diff;\n diff = diff === 0 ? c1.qualifier.name.localeCompare(c2.qualifier.name) : diff;\n diff = diff === 0 ? c1.value.localeCompare(c2.value) : diff;\n return diff;\n }\n\n /**\n * Gets a {@link ConditionToken | condition token} for this condition, if possible.\n * It is not possible to get a token for a condition with an operator other than `matches`,\n * with other-than-default priority, or with a name or value that contains other than alphanumeric\n * characters, underscore or non-leading hyphen.\n * @param terse - if `true` and if the qualifier token is optional, the token will be omitted\n * from the generated {@link ConditionToken | condition token}.\n * @returns\n */\n public toToken(terse?: boolean): Result<ConditionToken> {\n /* c8 ignore next 3 - defense in depth very difficult to induce */\n if (this.operator !== 'matches') {\n return fail(`${this.operator}: cannot create condition token for operator other than 'matches'`);\n }\n if (this.priority !== this.qualifier.defaultPriority) {\n return fail(`${this.priority}: cannot create condition token for non-default priority`);\n }\n if (terse && this.qualifier.tokenIsOptional) {\n return Validate.toConditionToken(this.value);\n }\n /* c8 ignore next 1 - coverage having a bad day */\n const name = this.qualifier.token ?? this.qualifier.name;\n return Validate.toConditionToken(`${name}=${this.value}`);\n }\n\n /**\n * Gets the {@link ConditionKey | key} for this condition.\n * @returns -\n */\n public toKey(): ConditionKey {\n return Condition.getKeyForDecl(this).orThrow();\n }\n\n /**\n * Get a human-readable string representation of the condition.\n * @returns A string representation of the condition.\n * @public\n */\n public toString(): string {\n return this.toKey();\n }\n\n /**\n * Gets the {@link ResourceJson.Json.IChildConditionDecl | child condition declaration} for this condition.\n * @param options - The {@link ResourceJson.Helpers.IDeclarationOptions | options} to use when creating the child\n * condition declaration.\n * @returns The {@link ResourceJson.Json.IChildConditionDecl | child condition declaration} for this condition.\n * @public\n */\n public toChildConditionDecl(\n options?: ResourceJson.Helpers.IDeclarationOptions\n ): ResourceJson.Json.IChildConditionDecl {\n const showDefaults = options?.showDefaults === true;\n return {\n value: this.value,\n /* c8 ignore next 1 - not really possible to reproduce right now */\n ...(showDefaults || this.operator !== 'matches' ? { operator: this.operator } : {}),\n ...(showDefaults || this.priority !== this.qualifier.defaultPriority\n ? { priority: this.priority }\n : {}),\n ...(this.scoreAsDefault ? { scoreAsDefault: this.scoreAsDefault } : {})\n };\n }\n\n /**\n * Gets the value for this condition, or the {@link ResourceJson.Json.IChildConditionDecl | child condition declaration}\n * if the condition has non-default operator, priority or a score as default.\n * @param options - The {@link ResourceJson.Helpers.IDeclarationOptions | options} to use when creating the child\n * condition declaration.\n * @returns A string value for this condition, or the {@link ResourceJson.Json.IChildConditionDecl | child condition declaration}\n * if the condition has non-default operator, priority or a score as default.\n */\n public toValueOrChildConditionDecl(\n options?: ResourceJson.Helpers.IDeclarationOptions\n ): string | ResourceJson.Json.IChildConditionDecl {\n if (\n options?.showDefaults !== true &&\n this.operator === 'matches' &&\n this.priority === this.qualifier.defaultPriority &&\n this.scoreAsDefault === undefined\n ) {\n return this.value;\n }\n /* c8 ignore next 2 - unreachable: matches is the only real operator in practice */\n return this.toChildConditionDecl(options);\n }\n\n /**\n * Gets the {@link ResourceJson.Json.ILooseConditionDecl | loose condition declaration} for this condition.\n * @param options - The {@link ResourceJson.Helpers.IDeclarationOptions | options} to use when creating the loose\n * condition declaration.\n * @returns The {@link ResourceJson.Json.ILooseConditionDecl | loose condition declaration} for this condition.\n * @public\n */\n public toLooseConditionDecl(\n options?: ResourceJson.Helpers.IDeclarationOptions\n ): ResourceJson.Json.ILooseConditionDecl {\n const showDefaults = options?.showDefaults === true;\n return {\n qualifierName: this.qualifier.name,\n value: this.value,\n /* c8 ignore next 1 - not really possible to reproduce right now */\n ...(showDefaults || this.operator !== 'matches' ? { operator: this.operator } : {}),\n ...(showDefaults || this.priority !== this.qualifier.defaultPriority\n ? { priority: this.priority }\n : {}),\n ...(this.scoreAsDefault ? { scoreAsDefault: this.scoreAsDefault } : {})\n };\n }\n\n /**\n * Converts this condition to a compiled condition representation.\n * @param options - Optional compilation options controlling the output format.\n * @returns A compiled condition object that can be used for serialization or runtime processing.\n * @public\n */\n public toCompiled(\n options?: ResourceJson.Compiled.ICompiledResourceOptions\n ): ResourceJson.Compiled.ICompiledCondition {\n /* c8 ignore next 1 - defense in depth */\n const operator = this.operator === 'matches' ? undefined : this.operator;\n return {\n qualifierIndex: this.qualifier.index!,\n operator,\n value: this.value,\n priority: this.priority,\n scoreAsDefault: this.scoreAsDefault,\n ...(options?.includeMetadata === true ? { metadata: { key: this.key } } : {})\n };\n }\n\n /**\n * Gets the {@link ConditionKey | condition key} for a supplied {@link Conditions.IValidatedConditionDecl | condition declaration}.\n * @param decl - The {@link Conditions.IValidatedConditionDecl | condition declaration} for which to get the key.\n * @returns `Success` with the condition key if successful, `Failure` otherwise.\n * @public\n */\n public static getKeyForDecl(decl: IValidatedConditionDecl): Result<ConditionKey> {\n const scoreAsDefault = decl.scoreAsDefault ? `(${scoreFormatter.format(decl.scoreAsDefault)})` : '';\n const key =\n decl.operator === 'matches'\n ? `${decl.qualifier.name}-[${decl.value}]@${decl.priority}${scoreAsDefault}`\n : `${decl.qualifier.name}-${decl.operator}-[${decl.value}]@${decl.priority}${scoreAsDefault}`;\n return Validate.toConditionKey(key);\n }\n}\n"]}
|
|
@@ -75,6 +75,10 @@ export declare class LiteralQualifierType extends QualifierType {
|
|
|
75
75
|
* @returns `true` if the value is a valid condition value, `false` otherwise.
|
|
76
76
|
*/
|
|
77
77
|
isValidConditionValue(value: string): value is QualifierConditionValue;
|
|
78
|
+
/**
|
|
79
|
+
* {@inheritdoc QualifierTypes.IQualifierType.isPotentialMatch}
|
|
80
|
+
*/
|
|
81
|
+
isPotentialMatch(conditionValue: string, contextValue: string): boolean;
|
|
78
82
|
/**
|
|
79
83
|
* {@inheritdoc QualifierTypes.QualifierType._matchOne}
|
|
80
84
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"literalQualifierType.d.ts","sourceRoot":"","sources":["../../../src/packlets/qualifier-types/literalQualifierType.ts"],"names":[],"mappings":"AAsBA,OAAO,EAA6B,MAAM,EAAiB,MAAM,eAAe,CAAC;AACjF,OAAO,EACL,iBAAiB,EAIjB,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,EAEpB,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,qBAAqB,EAAE,yBAAyB,EAAE,MAAM,yBAAyB,CAAC;AAC3F,OAAO,KAAK,MAAM,MAAM,UAAU,CAA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IAW/B;;;;;OAKG;WACW,4BAA4B,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,IAAI,uBAAuB;IAIzF;;;;;;OAMG;WACW,uBAAuB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,uBAAuB,CAAC;CAKrF"}
|
|
1
|
+
{"version":3,"file":"literalQualifierType.d.ts","sourceRoot":"","sources":["../../../src/packlets/qualifier-types/literalQualifierType.ts"],"names":[],"mappings":"AAsBA,OAAO,EAA6B,MAAM,EAAiB,MAAM,eAAe,CAAC;AACjF,OAAO,EACL,iBAAiB,EAIjB,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,EAEpB,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,qBAAqB,EAAE,yBAAyB,EAAE,MAAM,yBAAyB,CAAC;AAC3F,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AAGnC;;;;GAIG;AACH,MAAM,WAAW,iCAAiC;IAChD;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;OAGG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAE3B;;;OAGG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IAExB;;;OAGG;IACH,gBAAgB,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IAEzC;;;OAGG;IACH,SAAS,CAAC,EAAE,yBAAyB,CAAC,MAAM,CAAC,CAAC;IAE9C;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;;GAIG;AACH,qBAAa,oBAAqB,SAAQ,aAAa;IACrD;;OAEG;IACH,SAAgB,aAAa,EAAE,OAAO,CAAC;IAEvC;;OAEG;IACH,SAAgB,gBAAgB,CAAC,EAAE,aAAa,CAAC,uBAAuB,CAAC,CAAC;IAE1E;;;OAGG;IACH,SAAgB,SAAS,CAAC,EAAE,qBAAqB,CAAC,MAAM,CAAC,CAAC;IAE1D;;;;;;;;OAQG;IACH,SAAS,aAAa,EACpB,IAAI,EACJ,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EAChB,SAAS,EACT,KAAK,EACN,EAAE,iCAAiC;IAoBpC;;;;;;OAMG;IACI,qBAAqB,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,uBAAuB;IAU7E;;OAEG;IACI,gBAAgB,CAAC,cAAc,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO;IAY9E;;OAEG;IACH,SAAS,CAAC,SAAS,CACjB,SAAS,EAAE,uBAAuB,EAClC,OAAO,EAAE,qBAAqB,EAC9B,UAAU,EAAE,iBAAiB,GAC5B,mBAAmB;IAWtB;;;;;;;OAOG;WACW,MAAM,CAAC,MAAM,CAAC,EAAE,iCAAiC,GAAG,MAAM,CAAC,oBAAoB,CAAC;IAI9F;;;;;;;;OAQG;WACW,gBAAgB,CAC5B,MAAM,EAAE,MAAM,CAAC,oBAAoB,CAAC,MAAM,CAAC,2BAA2B,CAAC,GACtE,MAAM,CAAC,oBAAoB,CAAC;IAW/B;;;;;OAKG;WACW,4BAA4B,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,IAAI,uBAAuB;IAIzF;;;;;;OAMG;WACW,uBAAuB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,uBAAuB,CAAC;CAKrF"}
|
|
@@ -77,6 +77,20 @@ class LiteralQualifierType extends qualifierType_1.QualifierType {
|
|
|
77
77
|
}
|
|
78
78
|
return LiteralQualifierType.isValidLiteralConditionValue(value);
|
|
79
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* {@inheritdoc QualifierTypes.IQualifierType.isPotentialMatch}
|
|
82
|
+
*/
|
|
83
|
+
isPotentialMatch(conditionValue, contextValue) {
|
|
84
|
+
if (this.isValidConditionValue(conditionValue) && this.isValidContextValue(contextValue)) {
|
|
85
|
+
if (this._matchOne(conditionValue, contextValue, 'matches') !== common_1.NoMatch) {
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
if (this.hierarchy) {
|
|
89
|
+
return this.hierarchy.isAncestor(conditionValue, contextValue);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
80
94
|
/**
|
|
81
95
|
* {@inheritdoc QualifierTypes.QualifierType._matchOne}
|
|
82
96
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"literalQualifierType.js","sourceRoot":"","sources":["../../../src/packlets/qualifier-types/literalQualifierType.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,4CAAiF;AACjF,sCASmB;AACnB,mDAAgD;AAChD,mEAA2F;AAE3F,oDAAuD;AA2CvD;;;;GAIG;AACH,MAAa,oBAAqB,SAAQ,6BAAa;IAiBrD;;;;;;;;OAQG;IACH,YAAsB,EACpB,IAAI,EACJ,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EAChB,SAAS,EACT,KAAK,EAC6B;QAClC,gBAAgB,GAAG,gBAAgB,KAAK,KAAK,CAAC;QAC9C,KAAK,CAAC;YACJ,IAAI,EAAE,IAAI,aAAJ,IAAI,cAAJ,IAAI,GAAI,SAAS;YACvB,gBAAgB;YAChB,KAAK,EAAE,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,gBAAO,CAAC,kBAAkB,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,SAAS;SAC7F,CAAC,CAAC;QACH,IAAI,CAAC,aAAa,GAAG,aAAa,KAAK,IAAI,CAAC;QAC5C,IAAI,CAAC,gBAAgB,GAAG,gBAAgB;YACtC,CAAC,CAAC,IAAA,qBAAU,EAAC,KAAK,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC,GAAG,CAAC,oBAAoB,CAAC,uBAAuB,CAAC,CAAC,CAAC,OAAO,EAAE;YACtG,CAAC,CAAC,SAAS,CAAC;QACd,IAAI,SA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,CAAC,MAA0C;QAC7D,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,oBAAoB,CAAC,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,EAAE,CAAC,CAAC,CAAC;IACrE,CAAC;IAED;;;;;;;;OAQG;IACI,MAAM,CAAC,gBAAgB,CAC5B,MAAuE;;QAEvE,MAAM,aAAa,GAAG,MAAA,MAAM,CAAC,aAAa,mCAAI,EAAE,CAAC;QACjD,OAAO,IAAA,iCAAkB,EAAoC;YAC3D,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,gBAAgB,EAAE,aAAa,CAAC,gBAAgB,KAAK,IAAI;YACzD,aAAa,EAAE,aAAa,CAAC,aAAa;YAC1C,gBAAgB,EAAE,aAAa,CAAC,gBAAgB;YAChD,SAAS,EAAE,aAAa,CAAC,SAAS;SACnC,CAAC,CAAC,SAAS,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;IAC5C,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,4BAA4B,CAAC,IAAY;QACrD,OAAO,iBAAQ,CAAC,kBAAkB,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC3D,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,uBAAuB,CAAC,IAAY;QAChD,OAAO,oBAAoB,CAAC,4BAA4B,CAAC,IAAI,CAAC;YAC5D,CAAC,CAAC,IAAA,kBAAO,EAAC,IAAI,CAAC;YACf,CAAC,CAAC,IAAA,eAAI,EAAC,GAAG,IAAI,wCAAwC,CAAC,CAAC;IAC5D,CAAC;CACF;AAhJD,oDAgJC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { captureResult, mapResults, Result, fail, succeed } from '@fgv/ts-utils';\nimport {\n ConditionOperator,\n Convert,\n NoMatch,\n PerfectMatch,\n QualifierConditionValue,\n QualifierContextValue,\n QualifierMatchScore,\n Validate\n} from '../common';\nimport { QualifierType } from './qualifierType';\nimport { LiteralValueHierarchy, LiteralValueHierarchyDecl } from './literalValueHierarchy';\nimport * as Config from './config';\nimport { sanitizeJsonObject } from '@fgv/ts-json-base';\n\n/**\n * Interface defining the parameters that can be used to create a new\n * {@link QualifierTypes.LiteralQualifierType | LiteralQualifierType}.\n * @public\n */\nexport interface ILiteralQualifierTypeCreateParams {\n /**\n * Optional name for the qualifier type. Defaults to 'literal'.\n */\n name?: string;\n\n /**\n * Optional flag indicating whether the context can be a list of values.\n * Defaults to `true`.\n */\n allowContextList?: boolean;\n\n /**\n * Optional flag indicating whether the match should be case-sensitive.\n * Defaults to `false`.\n */\n caseSensitive?: boolean;\n\n /**\n * Optional array of enumerated values to further constrain the type.\n * Defaults to no constraint.\n */\n enumeratedValues?: ReadonlyArray<string>;\n\n /**\n * Optional {@link QualifierTypes.LiteralValueHierarchyDecl | hierarchy declaration}\n * of literal values to use for matching. If not provided, no hierarchy will be used.\n */\n hierarchy?: LiteralValueHierarchyDecl<string>;\n\n /**\n * Global index for this qualifier type.\n */\n index?: number;\n}\n\n/**\n * A {@link QualifierTypes.QualifierType | qualifier} that matches a literal value,\n * optionally case-sensitive or matching against an ordered list of values at runtime.\n * @public\n */\nexport class LiteralQualifierType extends QualifierType {\n /**\n * Indicates whether the qualifier match is case-sensitive.\n */\n public readonly caseSensitive: boolean;\n\n /**\n * Optional array of enumerated values to further constrain the type.\n */\n public readonly enumeratedValues?: ReadonlyArray<QualifierConditionValue>;\n\n /**\n * Optional {@link QualifierTypes.LiteralValueHierarchy | hierarchy} of literal\n * values to use for matching. If not provided, no hierarchy will be used.\n */\n public readonly hierarchy?: LiteralValueHierarchy<string>;\n\n /**\n * Constructs a new {@link QualifierTypes.LiteralQualifierType | LiteralQualifierType}.\n * @param name - Optional name for the qualifier type. Defaults to 'literal'.\n * @param caseSensitive - Optional flag indicating whether the match should be\n * case-sensitive. Defaults to `false`.\n * @param allowContextList - Optional flag indicating whether the context can be a\n * list of values. Defaults to `false`.\n * @public\n */\n protected constructor({\n name,\n caseSensitive,\n allowContextList,\n enumeratedValues,\n hierarchy,\n index\n }: ILiteralQualifierTypeCreateParams) {\n allowContextList = allowContextList !== false;\n super({\n name: name ?? 'literal',\n allowContextList,\n index: index !== undefined ? Convert.qualifierTypeIndex.convert(index).orThrow() : undefined\n });\n this.caseSensitive = caseSensitive === true;\n this.enumeratedValues = enumeratedValues\n ? mapResults(Array.from(enumeratedValues).map(LiteralQualifierType.toLiteralConditionValue)).orThrow()\n : undefined;\n if (hierarchy) {\n /* c8 ignore next 5 - defensive coding: enumeratedValues ?? [] fallback enables open values mode */\n this.hierarchy = LiteralValueHierarchy.create({\n values: enumeratedValues ?? [],\n hierarchy: hierarchy\n }).orThrow();\n }\n }\n\n /**\n * Determines whether a value is a valid condition value for a literal qualifier.\n * The {@link QualifierTypes.LiteralQualifierType | LiteralQualifierType} accepts\n * any identifier as a valid condition value.\n * @param value - The value to validate.\n * @returns `true` if the value is a valid condition value, `false` otherwise.\n */\n public isValidConditionValue(value: string): value is QualifierConditionValue {\n if (this.enumeratedValues) {\n if (this.caseSensitive) {\n return this.enumeratedValues.includes(value as QualifierConditionValue);\n }\n return this.enumeratedValues.some((v) => v.toLowerCase() === value.toLowerCase());\n }\n return LiteralQualifierType.isValidLiteralConditionValue(value);\n }\n\n /**\n * {@inheritdoc QualifierTypes.QualifierType._matchOne}\n */\n protected _matchOne(\n condition: QualifierConditionValue,\n context: QualifierContextValue,\n __operator: ConditionOperator\n ): QualifierMatchScore {\n if (this.hierarchy) {\n return this.hierarchy.match(condition, context, __operator);\n }\n if (this.caseSensitive) {\n return condition === (context as string) ? PerfectMatch : NoMatch;\n } else {\n return condition.toLowerCase() === context.toLowerCase() ? PerfectMatch : NoMatch;\n }\n }\n\n /**\n * Creates a new {@link QualifierTypes.LiteralQualifierType | LiteralQualifierType}.\n * @param params - Optional {@link QualifierTypes.ILiteralQualifierTypeCreateParams | parameters}\n * to use when creating the new instance.\n * @returns `Success` with the new {@link QualifierTypes.LiteralQualifierType | LiteralQualifierType}\n * if successful, `Failure` with an error message otherwise.\n * @public\n */\n public static create(params?: ILiteralQualifierTypeCreateParams): Result<LiteralQualifierType> {\n return captureResult(() => new LiteralQualifierType(params ?? {}));\n }\n\n /**\n * Creates a new {@link QualifierTypes.LiteralQualifierType | LiteralQualifierType} from a configuration object.\n * @param config - The {@link QualifierTypes.Config.IQualifierTypeConfig | configuration object} containing\n * the name, systemType, and optional literal-specific configuration including case sensitivity,\n * enumerated values, and hierarchy information.\n * @returns `Success` with the new {@link QualifierTypes.LiteralQualifierType | LiteralQualifierType}\n * if successful, `Failure` with an error message otherwise.\n * @public\n */\n public static createFromConfig(\n config: Config.IQualifierTypeConfig<Config.ILiteralQualifierTypeConfig>\n ): Result<LiteralQualifierType> {\n const literalConfig = config.configuration ?? {};\n return sanitizeJsonObject<ILiteralQualifierTypeCreateParams>({\n name: config.name,\n allowContextList: literalConfig.allowContextList === true,\n caseSensitive: literalConfig.caseSensitive,\n enumeratedValues: literalConfig.enumeratedValues,\n hierarchy: literalConfig.hierarchy\n }).onSuccess(LiteralQualifierType.create);\n }\n\n /**\n * Checks if the given value is a valid literal condition value.\n * @param from - The value to validate.\n * @returns `true` if the value is a valid literal condition value, otherwise `false`.\n * @public\n */\n public static isValidLiteralConditionValue(from: string): from is QualifierConditionValue {\n return Validate.RegularExpressions.identifier.test(from);\n }\n\n /**\n * Converts a string to a {@link QualifierConditionValue | literal condition value}.\n * @param from - The string to convert.\n * @returns `Success` with the converted value if valid, or `Failure` with an error\n * message if not.\n * @public\n */\n public static toLiteralConditionValue(from: string): Result<QualifierConditionValue> {\n return LiteralQualifierType.isValidLiteralConditionValue(from)\n ? succeed(from)\n : fail(`${from}: not a valid literal condition value.`);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"literalQualifierType.js","sourceRoot":"","sources":["../../../src/packlets/qualifier-types/literalQualifierType.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,4CAAiF;AACjF,sCASmB;AACnB,mDAAgD;AAChD,mEAA2F;AAE3F,oDAAuD;AA2CvD;;;;GAIG;AACH,MAAa,oBAAqB,SAAQ,6BAAa;IAiBrD;;;;;;;;OAQG;IACH,YAAsB,EACpB,IAAI,EACJ,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EAChB,SAAS,EACT,KAAK,EAC6B;QAClC,gBAAgB,GAAG,gBAAgB,KAAK,KAAK,CAAC;QAC9C,KAAK,CAAC;YACJ,IAAI,EAAE,IAAI,aAAJ,IAAI,cAAJ,IAAI,GAAI,SAAS;YACvB,gBAAgB;YAChB,KAAK,EAAE,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,gBAAO,CAAC,kBAAkB,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,SAAS;SAC7F,CAAC,CAAC;QACH,IAAI,CAAC,aAAa,GAAG,aAAa,KAAK,IAAI,CAAC;QAC5C,IA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C,SAAS,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;IAC5C,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,4BAA4B,CAAC,IAAY;QACrD,OAAO,iBAAQ,CAAC,kBAAkB,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC3D,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,uBAAuB,CAAC,IAAY;QAChD,OAAO,oBAAoB,CAAC,4BAA4B,CAAC,IAAI,CAAC;YAC5D,CAAC,CAAC,IAAA,kBAAO,EAAC,IAAI,CAAC;YACf,CAAC,CAAC,IAAA,eAAI,EAAC,GAAG,IAAI,wCAAwC,CAAC,CAAC;IAC5D,CAAC;CACF;AA/JD,oDA+JC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { captureResult, mapResults, Result, fail, succeed } from '@fgv/ts-utils';\nimport {\n ConditionOperator,\n Convert,\n NoMatch,\n PerfectMatch,\n QualifierConditionValue,\n QualifierContextValue,\n QualifierMatchScore,\n Validate\n} from '../common';\nimport { QualifierType } from './qualifierType';\nimport { LiteralValueHierarchy, LiteralValueHierarchyDecl } from './literalValueHierarchy';\nimport * as Config from './config';\nimport { sanitizeJsonObject } from '@fgv/ts-json-base';\n\n/**\n * Interface defining the parameters that can be used to create a new\n * {@link QualifierTypes.LiteralQualifierType | LiteralQualifierType}.\n * @public\n */\nexport interface ILiteralQualifierTypeCreateParams {\n /**\n * Optional name for the qualifier type. Defaults to 'literal'.\n */\n name?: string;\n\n /**\n * Optional flag indicating whether the context can be a list of values.\n * Defaults to `true`.\n */\n allowContextList?: boolean;\n\n /**\n * Optional flag indicating whether the match should be case-sensitive.\n * Defaults to `false`.\n */\n caseSensitive?: boolean;\n\n /**\n * Optional array of enumerated values to further constrain the type.\n * Defaults to no constraint.\n */\n enumeratedValues?: ReadonlyArray<string>;\n\n /**\n * Optional {@link QualifierTypes.LiteralValueHierarchyDecl | hierarchy declaration}\n * of literal values to use for matching. If not provided, no hierarchy will be used.\n */\n hierarchy?: LiteralValueHierarchyDecl<string>;\n\n /**\n * Global index for this qualifier type.\n */\n index?: number;\n}\n\n/**\n * A {@link QualifierTypes.QualifierType | qualifier} that matches a literal value,\n * optionally case-sensitive or matching against an ordered list of values at runtime.\n * @public\n */\nexport class LiteralQualifierType extends QualifierType {\n /**\n * Indicates whether the qualifier match is case-sensitive.\n */\n public readonly caseSensitive: boolean;\n\n /**\n * Optional array of enumerated values to further constrain the type.\n */\n public readonly enumeratedValues?: ReadonlyArray<QualifierConditionValue>;\n\n /**\n * Optional {@link QualifierTypes.LiteralValueHierarchy | hierarchy} of literal\n * values to use for matching. If not provided, no hierarchy will be used.\n */\n public readonly hierarchy?: LiteralValueHierarchy<string>;\n\n /**\n * Constructs a new {@link QualifierTypes.LiteralQualifierType | LiteralQualifierType}.\n * @param name - Optional name for the qualifier type. Defaults to 'literal'.\n * @param caseSensitive - Optional flag indicating whether the match should be\n * case-sensitive. Defaults to `false`.\n * @param allowContextList - Optional flag indicating whether the context can be a\n * list of values. Defaults to `false`.\n * @public\n */\n protected constructor({\n name,\n caseSensitive,\n allowContextList,\n enumeratedValues,\n hierarchy,\n index\n }: ILiteralQualifierTypeCreateParams) {\n allowContextList = allowContextList !== false;\n super({\n name: name ?? 'literal',\n allowContextList,\n index: index !== undefined ? Convert.qualifierTypeIndex.convert(index).orThrow() : undefined\n });\n this.caseSensitive = caseSensitive === true;\n this.enumeratedValues = enumeratedValues\n ? mapResults(Array.from(enumeratedValues).map(LiteralQualifierType.toLiteralConditionValue)).orThrow()\n : undefined;\n if (hierarchy) {\n /* c8 ignore next 5 - defensive coding: enumeratedValues ?? [] fallback enables open values mode */\n this.hierarchy = LiteralValueHierarchy.create({\n values: enumeratedValues ?? [],\n hierarchy: hierarchy\n }).orThrow();\n }\n }\n\n /**\n * Determines whether a value is a valid condition value for a literal qualifier.\n * The {@link QualifierTypes.LiteralQualifierType | LiteralQualifierType} accepts\n * any identifier as a valid condition value.\n * @param value - The value to validate.\n * @returns `true` if the value is a valid condition value, `false` otherwise.\n */\n public isValidConditionValue(value: string): value is QualifierConditionValue {\n if (this.enumeratedValues) {\n if (this.caseSensitive) {\n return this.enumeratedValues.includes(value as QualifierConditionValue);\n }\n return this.enumeratedValues.some((v) => v.toLowerCase() === value.toLowerCase());\n }\n return LiteralQualifierType.isValidLiteralConditionValue(value);\n }\n\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.isPotentialMatch}\n */\n public isPotentialMatch(conditionValue: string, contextValue: string): boolean {\n if (this.isValidConditionValue(conditionValue) && this.isValidContextValue(contextValue)) {\n if (this._matchOne(conditionValue, contextValue, 'matches') !== NoMatch) {\n return true;\n }\n if (this.hierarchy) {\n return this.hierarchy.isAncestor(conditionValue, contextValue);\n }\n }\n return false;\n }\n\n /**\n * {@inheritdoc QualifierTypes.QualifierType._matchOne}\n */\n protected _matchOne(\n condition: QualifierConditionValue,\n context: QualifierContextValue,\n __operator: ConditionOperator\n ): QualifierMatchScore {\n if (this.hierarchy) {\n return this.hierarchy.match(condition, context, __operator);\n }\n if (this.caseSensitive) {\n return condition === (context as string) ? PerfectMatch : NoMatch;\n } else {\n return condition.toLowerCase() === context.toLowerCase() ? PerfectMatch : NoMatch;\n }\n }\n\n /**\n * Creates a new {@link QualifierTypes.LiteralQualifierType | LiteralQualifierType}.\n * @param params - Optional {@link QualifierTypes.ILiteralQualifierTypeCreateParams | parameters}\n * to use when creating the new instance.\n * @returns `Success` with the new {@link QualifierTypes.LiteralQualifierType | LiteralQualifierType}\n * if successful, `Failure` with an error message otherwise.\n * @public\n */\n public static create(params?: ILiteralQualifierTypeCreateParams): Result<LiteralQualifierType> {\n return captureResult(() => new LiteralQualifierType(params ?? {}));\n }\n\n /**\n * Creates a new {@link QualifierTypes.LiteralQualifierType | LiteralQualifierType} from a configuration object.\n * @param config - The {@link QualifierTypes.Config.IQualifierTypeConfig | configuration object} containing\n * the name, systemType, and optional literal-specific configuration including case sensitivity,\n * enumerated values, and hierarchy information.\n * @returns `Success` with the new {@link QualifierTypes.LiteralQualifierType | LiteralQualifierType}\n * if successful, `Failure` with an error message otherwise.\n * @public\n */\n public static createFromConfig(\n config: Config.IQualifierTypeConfig<Config.ILiteralQualifierTypeConfig>\n ): Result<LiteralQualifierType> {\n const literalConfig = config.configuration ?? {};\n return sanitizeJsonObject<ILiteralQualifierTypeCreateParams>({\n name: config.name,\n allowContextList: literalConfig.allowContextList === true,\n caseSensitive: literalConfig.caseSensitive,\n enumeratedValues: literalConfig.enumeratedValues,\n hierarchy: literalConfig.hierarchy\n }).onSuccess(LiteralQualifierType.create);\n }\n\n /**\n * Checks if the given value is a valid literal condition value.\n * @param from - The value to validate.\n * @returns `true` if the value is a valid literal condition value, otherwise `false`.\n * @public\n */\n public static isValidLiteralConditionValue(from: string): from is QualifierConditionValue {\n return Validate.RegularExpressions.identifier.test(from);\n }\n\n /**\n * Converts a string to a {@link QualifierConditionValue | literal condition value}.\n * @param from - The string to convert.\n * @returns `Success` with the converted value if valid, or `Failure` with an error\n * message if not.\n * @public\n */\n public static toLiteralConditionValue(from: string): Result<QualifierConditionValue> {\n return LiteralQualifierType.isValidLiteralConditionValue(from)\n ? succeed(from)\n : fail(`${from}: not a valid literal condition value.`);\n }\n}\n"]}
|
|
@@ -80,6 +80,13 @@ export declare class LiteralValueHierarchy<T extends string = string> {
|
|
|
80
80
|
* to root, or `Failure` if the value is not in the hierarchy.
|
|
81
81
|
*/
|
|
82
82
|
getAncestors(value: T): Result<T[]>;
|
|
83
|
+
/**
|
|
84
|
+
* Determines if a value is an ancestor of a possible ancestor value.
|
|
85
|
+
* @param value - The value to check.
|
|
86
|
+
* @param possibleAncestor - The possible ancestor value.
|
|
87
|
+
* @returns `true` if the value is an ancestor of the possible ancestor, `false` otherwise.
|
|
88
|
+
*/
|
|
89
|
+
isAncestor(value: T, possibleAncestor: T): boolean;
|
|
83
90
|
/**
|
|
84
91
|
* Gets all descendants of a value in the hierarchy.
|
|
85
92
|
* @param value - The value to get descendants for.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"literalValueHierarchy.d.ts","sourceRoot":"","sources":["../../../src/packlets/qualifier-types/literalValueHierarchy.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAoC,MAAM,EAAiB,MAAM,eAAe,CAAC;AACxF,OAAO,EACL,iBAAiB,EAGjB,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,EAEpB,MAAM,WAAW,CAAC;AAEnB;;;;;;;;GAQG;AACH,MAAM,MAAM,yBAAyB,CAAC,CAAC,SAAS,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAEhF;;;GAGG;AACH,MAAM,WAAW,aAAa,CAAC,CAAC,SAAS,MAAM;IAC7C,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;IACjB,QAAQ,CAAC,MAAM,CAAC,EAAE,aAAa,CAAC,CAAC,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OAGG;IACH,SAAgB,YAAY,EAAE,OAAO,CAAC;IAEtC,SAAS,aAAa,MAAM,EAAE,kCAAkC,CAAC,CAAC,CAAC;IAMnE;;;;;OAKG;WACW,MAAM,CAAC,CAAC,SAAS,MAAM,EACnC,MAAM,EAAE,kCAAkC,CAAC,CAAC,CAAC,GAC5C,MAAM,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;IAInC;;;;OAIG;IACI,QAAQ,CAAC,KAAK,EAAE,CAAC,GAAG,OAAO;IAIlC;;;OAGG;IACI,QAAQ,IAAI,MAAM,CAAC,CAAC,EAAE,CAAC;IAQ9B;;;;;OAKG;IACI,YAAY,CAAC,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC;IAkB1C;;;;;OAKG;IACI,UAAU,CAAC,KAAK,EAAE,CAAC,EAAE,gBAAgB,EAAE,CAAC,GAAG,OAAO;IAQzD;;;;;OAKG;IACI,cAAc,CAAC,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC;IAoBrC,KAAK,CAAC,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,GAAG,mBAAmB;IAC3D;;;;;;;;;;;;OAYG;IACI,KAAK,CACV,SAAS,EAAE,uBAAuB,EAClC,OAAO,EAAE,qBAAqB,EAC9B,UAAU,CAAC,EAAE,iBAAiB,GAC7B,mBAAmB;IAuCtB,SAAS,CAAC,MAAM,CAAC,yBAAyB,CAAC,CAAC,SAAS,MAAM,EACzD,MAAM,EAAE,kCAAkC,CAAC,CAAC,CAAC,GAC5C,MAAM,CAAC,WAAW,CAAC,CAAC,EAAE,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC;CAkE5C"}
|
|
@@ -91,6 +91,19 @@ class LiteralValueHierarchy {
|
|
|
91
91
|
}
|
|
92
92
|
return (0, ts_utils_1.succeed)(ancestors);
|
|
93
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* Determines if a value is an ancestor of a possible ancestor value.
|
|
96
|
+
* @param value - The value to check.
|
|
97
|
+
* @param possibleAncestor - The possible ancestor value.
|
|
98
|
+
* @returns `true` if the value is an ancestor of the possible ancestor, `false` otherwise.
|
|
99
|
+
*/
|
|
100
|
+
isAncestor(value, possibleAncestor) {
|
|
101
|
+
const ancestors = this.getAncestors(value);
|
|
102
|
+
if (ancestors.isSuccess()) {
|
|
103
|
+
return ancestors.value.includes(possibleAncestor);
|
|
104
|
+
}
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
94
107
|
/**
|
|
95
108
|
* Gets all descendants of a value in the hierarchy.
|
|
96
109
|
* @param value - The value to get descendants for.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"literalValueHierarchy.js","sourceRoot":"","sources":["../../../src/packlets/qualifier-types/literalValueHierarchy.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,4CAAwF;AACxF,sCAQmB;AA4CnB;;;;;;;;;;;;;;GAcG;AACH,MAAa,qBAAqB;IAahC,YAAsB,MAA6C;QACjE,MAAM,MAAM,GAAG,qBAAqB,CAAC,yBAAyB,CAAC,MAAM,CAAC,CAAC;QACvE,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC;QAC/B,IAAI,CAAC,YAAY,GAAG,CAAC,MAAM,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,CAAC;IACnE,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAClB,MAA6C;QAE7C,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,qBAAqB,CAAC,MAAM,CAAC,CAAC,CAAC;IAChE,CAAC;IAED;;;;OAIG;IACI,QAAQ,CAAC,KAAQ;QACtB,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAChC,CAAC;IAED;;;OAGG;IACI,QAAQ;QACb,OAAO,IAAA,kBAAO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,CAAC;gBACnC,CAAC;qBAAM,CAAC;oBACN,MAAM,CAAC,QAAQ,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;gBACjC,CAAC;YACH,CAAC;QACH,CAAC;QAED,OAAO,MAAM,CAAC,cAAc,CAAC,IAAA,kBAAO,EAAC,QAAQ,CAAC,CAAC,CAAC;IAClD,CAAC;CACF;AAnOD,sDAmOC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { captureResult, MessageAggregator, Result, succeed, fail } from '@fgv/ts-utils';\nimport {\n ConditionOperator,\n NoMatch,\n PerfectMatch,\n QualifierConditionValue,\n QualifierContextValue,\n QualifierMatchScore,\n Convert as CommonConverters\n} from '../common';\n\n/**\n * Declares a hierarchy of literal values. The keys are the names of the values, and the\n * values are the names of their parents.\n * @remarks\n * The hierarchy is defined as a tree, where each value can have multiple children but\n * only one parent. The root of the tree has no parent. The hierarchy is used to\n * determine the relationship between values when matching conditions and contexts.\n * @public\n */\nexport type LiteralValueHierarchyDecl<T extends string> = Partial<Record<T, T>>;\n\n/**\n * Describes a single valid literal value including optional parent and child values.\n * @public\n */\nexport interface ILiteralValue<T extends string> {\n readonly name: T;\n readonly parent?: ILiteralValue<T>;\n readonly children?: ReadonlyArray<T>;\n}\n\n/**\n * Describes the parameters used to create a new\n * {@link QualifierTypes.LiteralValueHierarchy | LiteralValueHierarchy}.\n * @public\n */\nexport interface ILiteralValueHierarchyCreateParams<T extends string = string> {\n values: ReadonlyArray<T>;\n hierarchy?: LiteralValueHierarchyDecl<T>;\n}\n\n/**\n * Internal interface used during hierarchy construction to build\n * {@link QualifierTypes.ILiteralValue | ILiteralValue} objects.\n * @public\n */\ninterface ILiteralValueBuilder<T extends string> {\n readonly name: T;\n parent?: ILiteralValue<T>;\n children?: T[];\n}\n\n/**\n * A class that implements a hierarchy of literal values. The hierarchy is defined as a\n * tree, where each value can have multiple children but only one parent. The root of the\n * tree has no parent. The hierarchy is used to determine the relationship between values\n * when matching conditions and contexts.\n *\n * @remarks\n * The hierarchy can be created in two modes:\n * - **Constrained mode**: When enumerated values are provided, only those values are allowed\n * in the hierarchy and matching operations.\n * - **Open values mode**: When no enumerated values are provided, all values referenced in\n * the hierarchy are automatically collected and used. This allows for flexible hierarchies\n * where any value can be used in matching operations.\n * @public\n */\nexport class LiteralValueHierarchy<T extends string = string> {\n /**\n * A map of all allowed literal values to the corresponding\n * {@link QualifierTypes.ILiteralValue | ILiteralValue} validated definition.\n */\n public readonly values: ReadonlyMap<T, ILiteralValue<T>>;\n\n /**\n * Indicates whether this hierarchy was created with open values (no enumerated values\n * provided), allowing any value to be used in matching operations.\n */\n public readonly isOpenValues: boolean;\n\n protected constructor(params: ILiteralValueHierarchyCreateParams<T>) {\n const result = LiteralValueHierarchy._buildValuesFromHierarchy(params);\n this.values = result.orThrow();\n this.isOpenValues = !params.values || params.values.length === 0;\n }\n\n /**\n * Creates a new {@link QualifierTypes.LiteralValueHierarchy | LiteralValueHierarchy} instance.\n * @param params - The {@link QualifierTypes.ILiteralValueHierarchyCreateParams | parameters}\n * used to create the hierarchy.\n * @returns `Success` with the new hierarchy or `Failure` with an error message.\n */\n public static create<T extends string>(\n params: ILiteralValueHierarchyCreateParams<T>\n ): Result<LiteralValueHierarchy<T>> {\n return captureResult(() => new LiteralValueHierarchy(params));\n }\n\n /**\n * Checks if a value exists in the hierarchy.\n * @param value - The value to check.\n * @returns `true` if the value exists in the hierarchy, `false` otherwise.\n */\n public hasValue(value: T): boolean {\n return this.values.has(value);\n }\n\n /**\n * Gets all root values (values with no parent) in the hierarchy.\n * @returns `Success` with an array of root values.\n */\n public getRoots(): Result<T[]> {\n return succeed(\n Array.from(this.values.values())\n .filter((v) => !v.parent)\n .map((v) => v.name)\n );\n }\n\n /**\n * Gets all ancestors of a value in the hierarchy.\n * @param value - The value to get ancestors for.\n * @returns `Success` with an array of ancestor values, ordered from immediate parent\n * to root, or `Failure` if the value is not in the hierarchy.\n */\n public getAncestors(value: T): Result<T[]> {\n const current = this.values.get(value);\n /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */\n if (!current) {\n return fail(`${value}: not found in hierarchy`);\n }\n\n const ancestors: T[] = [];\n let ancestor = current.parent;\n\n while (ancestor) {\n ancestors.push(ancestor.name);\n ancestor = ancestor.parent;\n }\n\n return succeed(ancestors);\n }\n\n /**\n * Gets all descendants of a value in the hierarchy.\n * @param value - The value to get descendants for.\n * @returns `Success` with an array of descendant values, or `Failure` if the value\n * is not in the hierarchy.\n */\n public getDescendants(value: T): Result<T[]> {\n const current = this.values.get(value);\n if (!current) {\n return fail(`${value}: not found in hierarchy`);\n }\n\n const descendants: T[] = [];\n if (current.children) {\n for (const childName of current.children) {\n descendants.push(childName);\n const childDescendants = this.getDescendants(childName);\n if (childDescendants.isSuccess()) {\n descendants.push(...childDescendants.value);\n }\n }\n }\n\n return succeed(descendants);\n }\n\n public match(condition: T, context: T): QualifierMatchScore;\n /**\n * Matches a condition value against a context value, where an exact match of the\n * condition and context returns {@link PerfectMatch | PerfectMatch}, a condition\n * value that does not match the context value or any of its ancestors returns\n * {@link NoMatch | NoMatch}, and a condition value that matches the context value\n * or any of its ancestors returns a positive score that is less than\n * {@link PerfectMatch | PerfectMatch}, with the score decreasing with each ancestor\n * in the hierarchy.\n * @param condition - The condition value to match.\n * @param context - The context value to match against.\n * @param __operator - The operator used for matching (not used in this implementation).\n * @returns A {@link QualifierMatchScore | QualifierMatchScore} indicating the match score.\n */\n public match(\n condition: QualifierConditionValue,\n context: QualifierContextValue,\n __operator?: ConditionOperator\n ): QualifierMatchScore;\n public match(condition: string, context: string, __operator?: ConditionOperator): QualifierMatchScore {\n // For open hierarchies, skip validation and allow any values\n if (!this.isOpenValues) {\n // Validate that both condition and context exist in the hierarchy\n /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */\n if (!this.values.has(condition as T)) {\n return NoMatch;\n }\n /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */\n if (!this.values.has(context as T)) {\n return NoMatch;\n }\n }\n\n if ((condition as string) === (context as string)) {\n return PerfectMatch;\n }\n\n // For open hierarchies, if values aren't in the hierarchy, treat as no match\n // but don't fail validation\n const values: ReadonlyMap<string, ILiteralValue<string>> = this.values;\n /* c8 ignore next 1 - ? is defense in depth */\n let value = values.get(context)?.parent;\n /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */\n if (!value) {\n return NoMatch;\n }\n let score = PerfectMatch as number;\n while (value) {\n score *= 0.9;\n if (value.name === condition) {\n return CommonConverters.qualifierMatchScore.convert(score).orDefault(NoMatch);\n }\n value = value.parent;\n }\n return NoMatch;\n }\n\n protected static _buildValuesFromHierarchy<T extends string>(\n params: ILiteralValueHierarchyCreateParams<T>\n ): Result<ReadonlyMap<T, ILiteralValue<T>>> {\n const errors = new MessageAggregator();\n const values = params.values;\n const hierarchy = params.hierarchy ?? {};\n\n // If no values are provided, collect all values from the hierarchy\n let allValues: T[];\n if (!values || values.length === 0) {\n const hierarchyValues = new Set<T>();\n // Add all keys and values from the hierarchy\n Object.keys(hierarchy).forEach((key) => hierarchyValues.add(key as T));\n Object.values(hierarchy).forEach((value) => hierarchyValues.add(value as T));\n allValues = Array.from(hierarchyValues);\n } else {\n allValues = [...values];\n }\n\n const valueMap = new Map<T, ILiteralValueBuilder<T>>(allValues.map((name) => [name, { name }]));\n\n if (hierarchy) {\n for (const [valueName, parentName] of Object.entries(hierarchy)) {\n const value = valueMap.get(valueName as T);\n if (!value) {\n errors.addMessage(`${valueName}: invalid literal value.`);\n continue;\n }\n\n const parent = valueMap.get(parentName as T);\n if (!parent) {\n errors.addMessage(`${valueName}: parent ${parentName} is not a valid literal value.`);\n continue;\n }\n\n let ancestor = parent.parent;\n let circular = false;\n const ancestors: T[] = [];\n\n while (ancestor) {\n /* c8 ignore next 1 - defensive coding: complex circular reference detection intermittently missed */\n ancestors.push(ancestor.name);\n /* c8 ignore next 1 - defensive coding: ancestor.parent === value is very difficult to reach */\n if (ancestor.parent === parent || ancestor.parent === value) {\n /* c8 ignore next 3 - defensive coding: circular reference error reporting intermittently missed */\n errors.addMessage(`${valueName}: Circular reference detected: ${ancestors.join('->')}`);\n circular = true;\n break;\n }\n ancestor = ancestor.parent;\n }\n\n /* c8 ignore next 2 - defensive coding: circular reference handling intermittently missed */\n if (circular) {\n continue;\n }\n\n value.parent = parent;\n if (parent.children) {\n parent.children.push(value.name);\n } else {\n parent.children = [value.name];\n }\n }\n }\n\n return errors.returnOrReport(succeed(valueMap));\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"literalValueHierarchy.js","sourceRoot":"","sources":["../../../src/packlets/qualifier-types/literalValueHierarchy.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,4CAAwF;AACxF,sCAQmB;AA4CnB;;;;;;;;;;;;;;GAcG;AACH,MAAa,qBAAqB;IAahC,YAAsB,MAA6C;QACjE,MAAM,MAAM,GAAG,qBAAqB,CAAC,yBAAyB,CAAC,M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IAAI,QAAQ,GAAG,MAAM,CAAC,MAAM,CAAC;gBAC7B,IAAI,QAAQ,GAAG,KAAK,CAAC;gBACrB,MAAM,SAAS,GAAQ,EAAE,CAAC;gBAE1B,OAAO,QAAQ,EAAE,CAAC;oBAChB,qGAAqG;oBACrG,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;oBAC9B,+FAA+F;oBAC/F,IAAI,QAAQ,CAAC,MAAM,KAAK,MAAM,IAAI,QAAQ,CAAC,MAAM,KAAK,KAAK,EAAE,CAAC;wBAC5D,mGAAmG;wBACnG,MAAM,CAAC,UAAU,CAAC,GAAG,SAAS,kCAAkC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;wBACxF,QAAQ,GAAG,IAAI,CAAC;wBAChB,MAAM;oBACR,CAAC;oBACD,QAAQ,GAAG,QAAQ,CAAC,MAAM,CAAC;gBAC7B,CAAC;gBAED,4FAA4F;gBAC5F,IAAI,QAAQ,EAAE,CAAC;oBACb,SAAS;gBACX,CAAC;gBAED,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC;gBACtB,IAAI,MAAM,CAAC,QAAQ,EAAE,CAAC;oBACpB,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;gBACnC,CAAC;qBAAM,CAAC;oBACN,MAAM,CAAC,QAAQ,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;gBACjC,CAAC;YACH,CAAC;QACH,CAAC;QAED,OAAO,MAAM,CAAC,cAAc,CAAC,IAAA,kBAAO,EAAC,QAAQ,CAAC,CAAC,CAAC;IAClD,CAAC;CACF;AAjPD,sDAiPC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { captureResult, MessageAggregator, Result, succeed, fail } from '@fgv/ts-utils';\nimport {\n ConditionOperator,\n NoMatch,\n PerfectMatch,\n QualifierConditionValue,\n QualifierContextValue,\n QualifierMatchScore,\n Convert as CommonConverters\n} from '../common';\n\n/**\n * Declares a hierarchy of literal values. The keys are the names of the values, and the\n * values are the names of their parents.\n * @remarks\n * The hierarchy is defined as a tree, where each value can have multiple children but\n * only one parent. The root of the tree has no parent. The hierarchy is used to\n * determine the relationship between values when matching conditions and contexts.\n * @public\n */\nexport type LiteralValueHierarchyDecl<T extends string> = Partial<Record<T, T>>;\n\n/**\n * Describes a single valid literal value including optional parent and child values.\n * @public\n */\nexport interface ILiteralValue<T extends string> {\n readonly name: T;\n readonly parent?: ILiteralValue<T>;\n readonly children?: ReadonlyArray<T>;\n}\n\n/**\n * Describes the parameters used to create a new\n * {@link QualifierTypes.LiteralValueHierarchy | LiteralValueHierarchy}.\n * @public\n */\nexport interface ILiteralValueHierarchyCreateParams<T extends string = string> {\n values: ReadonlyArray<T>;\n hierarchy?: LiteralValueHierarchyDecl<T>;\n}\n\n/**\n * Internal interface used during hierarchy construction to build\n * {@link QualifierTypes.ILiteralValue | ILiteralValue} objects.\n * @public\n */\ninterface ILiteralValueBuilder<T extends string> {\n readonly name: T;\n parent?: ILiteralValue<T>;\n children?: T[];\n}\n\n/**\n * A class that implements a hierarchy of literal values. The hierarchy is defined as a\n * tree, where each value can have multiple children but only one parent. The root of the\n * tree has no parent. The hierarchy is used to determine the relationship between values\n * when matching conditions and contexts.\n *\n * @remarks\n * The hierarchy can be created in two modes:\n * - **Constrained mode**: When enumerated values are provided, only those values are allowed\n * in the hierarchy and matching operations.\n * - **Open values mode**: When no enumerated values are provided, all values referenced in\n * the hierarchy are automatically collected and used. This allows for flexible hierarchies\n * where any value can be used in matching operations.\n * @public\n */\nexport class LiteralValueHierarchy<T extends string = string> {\n /**\n * A map of all allowed literal values to the corresponding\n * {@link QualifierTypes.ILiteralValue | ILiteralValue} validated definition.\n */\n public readonly values: ReadonlyMap<T, ILiteralValue<T>>;\n\n /**\n * Indicates whether this hierarchy was created with open values (no enumerated values\n * provided), allowing any value to be used in matching operations.\n */\n public readonly isOpenValues: boolean;\n\n protected constructor(params: ILiteralValueHierarchyCreateParams<T>) {\n const result = LiteralValueHierarchy._buildValuesFromHierarchy(params);\n this.values = result.orThrow();\n this.isOpenValues = !params.values || params.values.length === 0;\n }\n\n /**\n * Creates a new {@link QualifierTypes.LiteralValueHierarchy | LiteralValueHierarchy} instance.\n * @param params - The {@link QualifierTypes.ILiteralValueHierarchyCreateParams | parameters}\n * used to create the hierarchy.\n * @returns `Success` with the new hierarchy or `Failure` with an error message.\n */\n public static create<T extends string>(\n params: ILiteralValueHierarchyCreateParams<T>\n ): Result<LiteralValueHierarchy<T>> {\n return captureResult(() => new LiteralValueHierarchy(params));\n }\n\n /**\n * Checks if a value exists in the hierarchy.\n * @param value - The value to check.\n * @returns `true` if the value exists in the hierarchy, `false` otherwise.\n */\n public hasValue(value: T): boolean {\n return this.values.has(value);\n }\n\n /**\n * Gets all root values (values with no parent) in the hierarchy.\n * @returns `Success` with an array of root values.\n */\n public getRoots(): Result<T[]> {\n return succeed(\n Array.from(this.values.values())\n .filter((v) => !v.parent)\n .map((v) => v.name)\n );\n }\n\n /**\n * Gets all ancestors of a value in the hierarchy.\n * @param value - The value to get ancestors for.\n * @returns `Success` with an array of ancestor values, ordered from immediate parent\n * to root, or `Failure` if the value is not in the hierarchy.\n */\n public getAncestors(value: T): Result<T[]> {\n const current = this.values.get(value);\n /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */\n if (!current) {\n return fail(`${value}: not found in hierarchy`);\n }\n\n const ancestors: T[] = [];\n let ancestor = current.parent;\n\n while (ancestor) {\n ancestors.push(ancestor.name);\n ancestor = ancestor.parent;\n }\n\n return succeed(ancestors);\n }\n\n /**\n * Determines if a value is an ancestor of a possible ancestor value.\n * @param value - The value to check.\n * @param possibleAncestor - The possible ancestor value.\n * @returns `true` if the value is an ancestor of the possible ancestor, `false` otherwise.\n */\n public isAncestor(value: T, possibleAncestor: T): boolean {\n const ancestors = this.getAncestors(value);\n if (ancestors.isSuccess()) {\n return ancestors.value.includes(possibleAncestor);\n }\n return false;\n }\n\n /**\n * Gets all descendants of a value in the hierarchy.\n * @param value - The value to get descendants for.\n * @returns `Success` with an array of descendant values, or `Failure` if the value\n * is not in the hierarchy.\n */\n public getDescendants(value: T): Result<T[]> {\n const current = this.values.get(value);\n if (!current) {\n return fail(`${value}: not found in hierarchy`);\n }\n\n const descendants: T[] = [];\n if (current.children) {\n for (const childName of current.children) {\n descendants.push(childName);\n const childDescendants = this.getDescendants(childName);\n if (childDescendants.isSuccess()) {\n descendants.push(...childDescendants.value);\n }\n }\n }\n\n return succeed(descendants);\n }\n\n public match(condition: T, context: T): QualifierMatchScore;\n /**\n * Matches a condition value against a context value, where an exact match of the\n * condition and context returns {@link PerfectMatch | PerfectMatch}, a condition\n * value that does not match the context value or any of its ancestors returns\n * {@link NoMatch | NoMatch}, and a condition value that matches the context value\n * or any of its ancestors returns a positive score that is less than\n * {@link PerfectMatch | PerfectMatch}, with the score decreasing with each ancestor\n * in the hierarchy.\n * @param condition - The condition value to match.\n * @param context - The context value to match against.\n * @param __operator - The operator used for matching (not used in this implementation).\n * @returns A {@link QualifierMatchScore | QualifierMatchScore} indicating the match score.\n */\n public match(\n condition: QualifierConditionValue,\n context: QualifierContextValue,\n __operator?: ConditionOperator\n ): QualifierMatchScore;\n public match(condition: string, context: string, __operator?: ConditionOperator): QualifierMatchScore {\n // For open hierarchies, skip validation and allow any values\n if (!this.isOpenValues) {\n // Validate that both condition and context exist in the hierarchy\n /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */\n if (!this.values.has(condition as T)) {\n return NoMatch;\n }\n /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */\n if (!this.values.has(context as T)) {\n return NoMatch;\n }\n }\n\n if ((condition as string) === (context as string)) {\n return PerfectMatch;\n }\n\n // For open hierarchies, if values aren't in the hierarchy, treat as no match\n // but don't fail validation\n const values: ReadonlyMap<string, ILiteralValue<string>> = this.values;\n /* c8 ignore next 1 - ? is defense in depth */\n let value = values.get(context)?.parent;\n /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */\n if (!value) {\n return NoMatch;\n }\n let score = PerfectMatch as number;\n while (value) {\n score *= 0.9;\n if (value.name === condition) {\n return CommonConverters.qualifierMatchScore.convert(score).orDefault(NoMatch);\n }\n value = value.parent;\n }\n return NoMatch;\n }\n\n protected static _buildValuesFromHierarchy<T extends string>(\n params: ILiteralValueHierarchyCreateParams<T>\n ): Result<ReadonlyMap<T, ILiteralValue<T>>> {\n const errors = new MessageAggregator();\n const values = params.values;\n const hierarchy = params.hierarchy ?? {};\n\n // If no values are provided, collect all values from the hierarchy\n let allValues: T[];\n if (!values || values.length === 0) {\n const hierarchyValues = new Set<T>();\n // Add all keys and values from the hierarchy\n Object.keys(hierarchy).forEach((key) => hierarchyValues.add(key as T));\n Object.values(hierarchy).forEach((value) => hierarchyValues.add(value as T));\n allValues = Array.from(hierarchyValues);\n } else {\n allValues = [...values];\n }\n\n const valueMap = new Map<T, ILiteralValueBuilder<T>>(allValues.map((name) => [name, { name }]));\n\n if (hierarchy) {\n for (const [valueName, parentName] of Object.entries(hierarchy)) {\n const value = valueMap.get(valueName as T);\n if (!value) {\n errors.addMessage(`${valueName}: invalid literal value.`);\n continue;\n }\n\n const parent = valueMap.get(parentName as T);\n if (!parent) {\n errors.addMessage(`${valueName}: parent ${parentName} is not a valid literal value.`);\n continue;\n }\n\n let ancestor = parent.parent;\n let circular = false;\n const ancestors: T[] = [];\n\n while (ancestor) {\n /* c8 ignore next 1 - defensive coding: complex circular reference detection intermittently missed */\n ancestors.push(ancestor.name);\n /* c8 ignore next 1 - defensive coding: ancestor.parent === value is very difficult to reach */\n if (ancestor.parent === parent || ancestor.parent === value) {\n /* c8 ignore next 3 - defensive coding: circular reference error reporting intermittently missed */\n errors.addMessage(`${valueName}: Circular reference detected: ${ancestors.join('->')}`);\n circular = true;\n break;\n }\n ancestor = ancestor.parent;\n }\n\n /* c8 ignore next 2 - defensive coding: circular reference handling intermittently missed */\n if (circular) {\n continue;\n }\n\n value.parent = parent;\n if (parent.children) {\n parent.children.push(value.name);\n } else {\n parent.children = [value.name];\n }\n }\n }\n\n return errors.returnOrReport(succeed(valueMap));\n }\n}\n"]}
|
|
@@ -36,6 +36,13 @@ export interface IQualifierType extends ICollectible<QualifierTypeName, Qualifie
|
|
|
36
36
|
* details otherwise.
|
|
37
37
|
*/
|
|
38
38
|
isValidContextValue(value: string): value is QualifierContextValue;
|
|
39
|
+
/**
|
|
40
|
+
* Determines if a supplied condition value is a potential match for a possible context value.
|
|
41
|
+
* @param conditionValue - The condition value.
|
|
42
|
+
* @param contextValue - The context value.
|
|
43
|
+
* @returns `true` if the condition value is a potential match for the context value, `false` otherwise.
|
|
44
|
+
*/
|
|
45
|
+
isPotentialMatch(conditionValue: string, contextValue: string): boolean;
|
|
39
46
|
/**
|
|
40
47
|
* Validates that a value and optional operator are valid for use in a condition
|
|
41
48
|
* for qualifiers of this type.
|
|
@@ -125,6 +132,10 @@ export declare abstract class QualifierType implements IQualifierType {
|
|
|
125
132
|
* {@inheritdoc QualifierTypes.IQualifierType.isValidContextValue}
|
|
126
133
|
*/
|
|
127
134
|
isValidContextValue(value: string): value is QualifierContextValue;
|
|
135
|
+
/**
|
|
136
|
+
* {@inheritdoc QualifierTypes.IQualifierType.isPotentialMatch}
|
|
137
|
+
*/
|
|
138
|
+
isPotentialMatch(conditionValue: string, contextValue: string): boolean;
|
|
128
139
|
/**
|
|
129
140
|
* {@inheritdoc QualifierTypes.IQualifierType.validateCondition}
|
|
130
141
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"qualifierType.d.ts","sourceRoot":"","sources":["../../../src/packlets/qualifier-types/qualifierType.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,EAAiB,MAAM,eAAe,CAAC;AACjF,OAAO,EACL,iBAAiB,EAIjB,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,EACnB,kBAAkB,EAClB,iBAAiB,EAElB,MAAM,WAAW,CAAC;AAEnB;;;;;GAKG;AACH,MAAM,WAAW,cAAe,SAAQ,YAAY,CAAC,iBAAiB,EAAE,kBAAkB,CAAC;IACzF;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,iBAAiB,CAAC;IAEjC;;OAEG;IACH,QAAQ,CAAC,GAAG,EAAE,iBAAiB,CAAC;IAEhC;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,kBAAkB,GAAG,SAAS,CAAC;IAE/C;;;;;;OAMG;IACH,qBAAqB,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,uBAAuB,CAAC;IAEvE;;;;;;OAMG;IACH,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,qBAAqB,CAAC;IAEnE;;;;;OAKG;IACH,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,iBAAiB,GAAG,MAAM,CAAC,uBAAuB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AAM,GAAG,KAAK,IAAI,kBAAkB;IAItE;;;;;OAKG;WACW,OAAO,CAAC,EAAE,EAAE,aAAa,EAAE,EAAE,EAAE,aAAa,GAAG,MAAM;IAYnE;;;;;;;;OAQG;IACH,SAAS,CAAC,QAAQ,CAAC,SAAS,CAC1B,SAAS,EAAE,uBAAuB,EAClC,OAAO,EAAE,qBAAqB,EAC9B,QAAQ,EAAE,iBAAiB,GAC1B,mBAAmB;IAEtB;;;;;;;;OAQG;IACH,SAAS,CAAC,UAAU,CAClB,SAAS,EAAE,uBAAuB,EAClC,OAAO,EAAE,qBAAqB,EAAE,EAChC,QAAQ,EAAE,iBAAiB,GAC1B,mBAAmB;IAiBtB;;;;;;OAMG;IACH,SAAS,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,EAAE,qBAAqB,GAAG,qBAAqB,EAAE;CAGtF"}
|
|
@@ -71,6 +71,15 @@ class QualifierType {
|
|
|
71
71
|
/* c8 ignore next 1 - functional code tested but coverage intermittently missed */
|
|
72
72
|
return false;
|
|
73
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* {@inheritdoc QualifierTypes.IQualifierType.isPotentialMatch}
|
|
76
|
+
*/
|
|
77
|
+
isPotentialMatch(conditionValue, contextValue) {
|
|
78
|
+
if (this.isValidConditionValue(conditionValue) && this.isValidContextValue(contextValue)) {
|
|
79
|
+
return this._matchOne(conditionValue, contextValue, 'matches') !== common_1.NoMatch;
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
74
83
|
/**
|
|
75
84
|
* {@inheritdoc QualifierTypes.IQualifierType.validateCondition}
|
|
76
85
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"qualifierType.js","sourceRoot":"","sources":["../../../src/packlets/qualifier-types/qualifierType.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,4CAAiF;AACjF,sCAWmB;AAwGnB;;;;GAIG;AACH,MAAsB,aAAa;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AAC,KAA4B;QACzD,OAAO,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAA2B,CAAC,CAAC;IACxE,CAAC;CACF;AAtMD,sCAsMC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Collections, ICollectible, Result, fail, succeed } from '@fgv/ts-utils';\nimport {\n ConditionOperator,\n Convert,\n NoMatch,\n PerfectMatch,\n QualifierConditionValue,\n QualifierContextValue,\n QualifierMatchScore,\n QualifierTypeIndex,\n QualifierTypeName,\n Validate\n} from '../common';\n\n/**\n * Interface for a qualifier type. A qualifier type implements the build and\n * runtime semantics for some class of related qualifiers (e.g. language,\n * territories, etc).\n * @public\n */\nexport interface IQualifierType extends ICollectible<QualifierTypeName, QualifierTypeIndex> {\n /**\n * The name of the qualifier type.\n */\n readonly name: QualifierTypeName;\n\n /**\n * Unique key for this qualifier.\n */\n readonly key: QualifierTypeName;\n\n /**\n * Global index for this qualifier type. Immutable once set, either at\n * construction or using {@link QualifierTypes.IQualifierType.setIndex | setIndex}.\n */\n readonly index: QualifierTypeIndex | undefined;\n\n /**\n * Validates a condition value for this qualifier type.\n * @param value - The string value to validate.\n * @returns `Success` with the {@link QualifierConditionValue | validated value}\n * if the value is valid for use in a condition, `Failure` with error details\n * otherwise.\n */\n isValidConditionValue(value: string): value is QualifierConditionValue;\n\n /**\n * Validates a context value for this qualifier type.\n * @param value - The string value to validate.\n * @returns `Success` with the {@link QualifierContextValue | validated value}\n * if the value is valid for use in a runtime context, `Failure` with error\n * details otherwise.\n */\n isValidContextValue(value: string): value is QualifierContextValue;\n\n /**\n * Validates that a value and optional operator are valid for use in a condition\n * for qualifiers of this type.\n * @param value - The string value to validate.\n * @param operator - An optional operator to validate. Defaults to 'matches'.\n */\n validateCondition(value: string, operator?: ConditionOperator): Result<QualifierConditionValue>;\n\n /**\n * Validates that a value is valid for use in a runtime context for qualifiers\n * of this type.\n * @param value - The string value to validate.\n * @returns `Success` with the {@link QualifierContextValue | validated value}\n * if the value is valid for use in a runtime context, `Failure` with error\n * details otherwise.\n */\n validateContextValue(value: string): Result<QualifierContextValue>;\n\n /**\n * Determines the extent to which a condition matches a context value for this\n * qualifier type.\n * @param condition - The condition value to evaluate.\n * @param context - The context value to evaluate.\n * @param operator - The operator to use in evaluating the match.\n * @returns a {@link QualifierMatchScore | score} indicating the extent to which\n * the condition matches the context value.\n */\n matches(\n condition: QualifierConditionValue,\n context: QualifierContextValue,\n operator: ConditionOperator\n ): QualifierMatchScore;\n\n /**\n * Sets the index for this qualifier type. Once set, index is immutable.\n */\n setIndex(index: number): Result<QualifierTypeIndex>;\n}\n\n/**\n * Parameters used to create a base {@link QualifierTypes.QualifierType | qualifier type}.\n * @public\n */\nexport interface IQualifierTypeCreateParams {\n /**\n * The name of the qualifier type. No default value.\n */\n name: string;\n\n /**\n * Global index for this qualifier type.\n */\n index?: number;\n\n /**\n * Flag indicating whether this qualifier type allows a list of values in a context.\n * Defaults to `false`.\n */\n allowContextList?: boolean;\n}\n\n/**\n * Abstract base class for qualifier types. Provides default implementations for\n * the {@link QualifierTypes.IQualifierType | IQualifierType} interface.\n * @public\n */\nexport abstract class QualifierType implements IQualifierType {\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.name}\n */\n public readonly name: QualifierTypeName;\n\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.key}\n */\n public get key(): QualifierTypeName {\n return this._collectible.key;\n }\n\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.index}\n */\n public get index(): QualifierTypeIndex | undefined {\n return this._collectible.index;\n }\n\n protected readonly _collectible: Collections.Collectible<QualifierTypeName, QualifierTypeIndex>;\n\n /**\n * Flag indicating whether this qualifier type allows a list of values in a context.\n * @public\n */\n public readonly allowContextList: boolean;\n\n /**\n * Constructor for use by derived classes.\n * @param name - The name of the qualifier type.\n * @param allowContextList - Flag indicating whether this qualifier type allows a\n * comma-separated list of runtime values in the context. Defaults to `false`.\n */\n protected constructor({ name, index, allowContextList }: IQualifierTypeCreateParams) {\n this.name = Convert.qualifierTypeName.convert(name).orThrow();\n this.allowContextList = allowContextList === true;\n this._collectible = new Collections.Collectible<QualifierTypeName, QualifierTypeIndex>({\n key: this.name,\n index: index,\n indexConverter: Convert.qualifierTypeIndex\n });\n }\n\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.isValidConditionValue}\n */\n public abstract isValidConditionValue(value: string): value is QualifierConditionValue;\n\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.isValidContextValue}\n */\n public isValidContextValue(value: string): value is QualifierContextValue {\n if (this.isValidConditionValue(value)) {\n return true;\n }\n if (this.allowContextList) {\n /* c8 ignore next 1 - functional code tested but coverage intermittently missed */\n return value.split(',').every((v) => this.isValidConditionValue(v.trim()));\n }\n /* c8 ignore next 1 - functional code tested but coverage intermittently missed */\n return false;\n }\n\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.validateCondition}\n */\n public validateCondition(value: string, operator?: ConditionOperator): Result<QualifierConditionValue> {\n operator = operator ?? 'matches';\n /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */\n if (operator !== 'matches') {\n return fail(`${operator}: invalid condition operator`);\n /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */\n } else if (!this.isValidConditionValue(value)) {\n return fail(`${value}: invalid condition value for qualifierType ${this.name}`);\n }\n return succeed(value);\n }\n\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.validateContextValue}\n */\n public validateContextValue(value: string): Result<QualifierContextValue> {\n /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */\n if (!this.isValidContextValue(value)) {\n return fail(`${value}: invalid context value for qualifierType ${this.name}`);\n }\n return succeed(value);\n }\n\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.matches}\n */\n public matches(\n condition: QualifierConditionValue,\n context: QualifierContextValue,\n operator: ConditionOperator\n ): QualifierMatchScore {\n if (this.allowContextList) {\n return this._matchList(condition, QualifierType._splitContext(context), operator);\n }\n return this._matchOne(condition, context, operator);\n }\n\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.setIndex}\n */\n public setIndex(index: number): Result<QualifierTypeIndex> {\n return this._collectible.setIndex(index);\n }\n\n /**\n * {@inheritdoc Validate.isValidQualifierTypeName}\n */\n public static isValidName(name: string): name is QualifierTypeName {\n return Validate.isValidQualifierTypeName(name);\n }\n\n /**\n * {@inheritdoc Validate.isValidQualifierTypeIndex}\n */\n public static isValidIndex(index: number): index is QualifierTypeIndex {\n return Validate.isValidQualifierTypeIndex(index);\n }\n\n /**\n * Compares two qualifier types by index.\n * @param t1 - The first qualifier type to compare.\n * @param t2 - The second qualifier type to compare.\n * @returns a number indicating the relative order of the two qualifier types.\n */\n public static compare(t1: QualifierType, t2: QualifierType): number {\n const i1 = t1._collectible.index ?? -1;\n const i2 = t2._collectible.index ?? -1;\n let diff = i1 - i2;\n\n if (diff === 0) {\n diff = t1.name.localeCompare(t2.name);\n }\n\n return diff;\n }\n\n /**\n * Matches a single condition value against a single context value.\n * @param condition - The {@link QualifierConditionValue | condition value} to match.\n * @param context - The {@link QualifierContextValue | context value} to match.\n * @param operator - The {@link ConditionOperator | operator} to use in the match.\n * @returns a {@link QualifierMatchScore | score} indicating the extent to which\n * the condition matches the context value.\n * @public\n */\n protected abstract _matchOne(\n condition: QualifierConditionValue,\n context: QualifierContextValue,\n operator: ConditionOperator\n ): QualifierMatchScore;\n\n /**\n * Matches a single condition value against a list of context values.\n * @param condition - The {@link QualifierConditionValue | condition value} to match.\n * @param context - The comma-separated list of {@link QualifierContextValue | context values} to match.\n * @param operator - The {@link ConditionOperator | operator} to use in the match.\n * @returns a {@link QualifierMatchScore | score} indicating the extent to which\n * the condition matches the context value.\n * @public\n */\n protected _matchList(\n condition: QualifierConditionValue,\n context: QualifierContextValue[],\n operator: ConditionOperator\n ): QualifierMatchScore {\n const scorePerPosition = 1 / context.length;\n let baseScore = PerfectMatch - scorePerPosition;\n\n for (let i = 0; i < context.length; i++) {\n const score = this._matchOne(condition, context[i], operator);\n if (score > NoMatch) {\n const adjusted = baseScore + scorePerPosition * score;\n if (Validate.isValidQualifierMatchScore(adjusted)) {\n return adjusted;\n }\n }\n baseScore -= scorePerPosition;\n }\n return NoMatch;\n }\n\n /**\n * Splits a comma-separated {@link QualifierContextValue | context value} into an\n * array of individual values.\n * @param value - The value to split.\n * @returns an array of individual context values.\n * @public\n */\n protected static _splitContext(value: QualifierContextValue): QualifierContextValue[] {\n return value.split(',').map((s) => s.trim() as QualifierContextValue);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"qualifierType.js","sourceRoot":"","sources":["../../../src/packlets/qualifier-types/qualifierType.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,4CAAiF;AACjF,sCAWmB;AAgHnB;;;;GAIG;AACH,MAAsB,aAAa;IAMjC;;OAEG;IACH,IAAW,GAAG;QACZ,OAAO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AAG,KAAK,CAAC;gBACtD,IAAI,iBAAQ,CAAC,0BAA0B,CAAC,QAAQ,CAAC,EAAE,CAAC;oBAClD,OAAO,QAAQ,CAAC;gBAClB,CAAC;YACH,CAAC;YACD,SAAS,IAAI,gBAAgB,CAAC;QAChC,CAAC;QACD,OAAO,gBAAO,CAAC;IACjB,CAAC;IAED;;;;;;OAMG;IACO,MAAM,CAAC,aAAa,CAAC,KAA4B;QACzD,OAAO,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAA2B,CAAC,CAAC;IACxE,CAAC;CACF;AAhND,sCAgNC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Collections, ICollectible, Result, fail, succeed } from '@fgv/ts-utils';\nimport {\n ConditionOperator,\n Convert,\n NoMatch,\n PerfectMatch,\n QualifierConditionValue,\n QualifierContextValue,\n QualifierMatchScore,\n QualifierTypeIndex,\n QualifierTypeName,\n Validate\n} from '../common';\n\n/**\n * Interface for a qualifier type. A qualifier type implements the build and\n * runtime semantics for some class of related qualifiers (e.g. language,\n * territories, etc).\n * @public\n */\nexport interface IQualifierType extends ICollectible<QualifierTypeName, QualifierTypeIndex> {\n /**\n * The name of the qualifier type.\n */\n readonly name: QualifierTypeName;\n\n /**\n * Unique key for this qualifier.\n */\n readonly key: QualifierTypeName;\n\n /**\n * Global index for this qualifier type. Immutable once set, either at\n * construction or using {@link QualifierTypes.IQualifierType.setIndex | setIndex}.\n */\n readonly index: QualifierTypeIndex | undefined;\n\n /**\n * Validates a condition value for this qualifier type.\n * @param value - The string value to validate.\n * @returns `Success` with the {@link QualifierConditionValue | validated value}\n * if the value is valid for use in a condition, `Failure` with error details\n * otherwise.\n */\n isValidConditionValue(value: string): value is QualifierConditionValue;\n\n /**\n * Validates a context value for this qualifier type.\n * @param value - The string value to validate.\n * @returns `Success` with the {@link QualifierContextValue | validated value}\n * if the value is valid for use in a runtime context, `Failure` with error\n * details otherwise.\n */\n isValidContextValue(value: string): value is QualifierContextValue;\n\n /**\n * Determines if a supplied condition value is a potential match for a possible context value.\n * @param conditionValue - The condition value.\n * @param contextValue - The context value.\n * @returns `true` if the condition value is a potential match for the context value, `false` otherwise.\n */\n isPotentialMatch(conditionValue: string, contextValue: string): boolean;\n\n /**\n * Validates that a value and optional operator are valid for use in a condition\n * for qualifiers of this type.\n * @param value - The string value to validate.\n * @param operator - An optional operator to validate. Defaults to 'matches'.\n */\n validateCondition(value: string, operator?: ConditionOperator): Result<QualifierConditionValue>;\n\n /**\n * Validates that a value is valid for use in a runtime context for qualifiers\n * of this type.\n * @param value - The string value to validate.\n * @returns `Success` with the {@link QualifierContextValue | validated value}\n * if the value is valid for use in a runtime context, `Failure` with error\n * details otherwise.\n */\n validateContextValue(value: string): Result<QualifierContextValue>;\n\n /**\n * Determines the extent to which a condition matches a context value for this\n * qualifier type.\n * @param condition - The condition value to evaluate.\n * @param context - The context value to evaluate.\n * @param operator - The operator to use in evaluating the match.\n * @returns a {@link QualifierMatchScore | score} indicating the extent to which\n * the condition matches the context value.\n */\n matches(\n condition: QualifierConditionValue,\n context: QualifierContextValue,\n operator: ConditionOperator\n ): QualifierMatchScore;\n\n /**\n * Sets the index for this qualifier type. Once set, index is immutable.\n */\n setIndex(index: number): Result<QualifierTypeIndex>;\n}\n\n/**\n * Parameters used to create a base {@link QualifierTypes.QualifierType | qualifier type}.\n * @public\n */\nexport interface IQualifierTypeCreateParams {\n /**\n * The name of the qualifier type. No default value.\n */\n name: string;\n\n /**\n * Global index for this qualifier type.\n */\n index?: number;\n\n /**\n * Flag indicating whether this qualifier type allows a list of values in a context.\n * Defaults to `false`.\n */\n allowContextList?: boolean;\n}\n\n/**\n * Abstract base class for qualifier types. Provides default implementations for\n * the {@link QualifierTypes.IQualifierType | IQualifierType} interface.\n * @public\n */\nexport abstract class QualifierType implements IQualifierType {\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.name}\n */\n public readonly name: QualifierTypeName;\n\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.key}\n */\n public get key(): QualifierTypeName {\n return this._collectible.key;\n }\n\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.index}\n */\n public get index(): QualifierTypeIndex | undefined {\n return this._collectible.index;\n }\n\n protected readonly _collectible: Collections.Collectible<QualifierTypeName, QualifierTypeIndex>;\n\n /**\n * Flag indicating whether this qualifier type allows a list of values in a context.\n * @public\n */\n public readonly allowContextList: boolean;\n\n /**\n * Constructor for use by derived classes.\n * @param name - The name of the qualifier type.\n * @param allowContextList - Flag indicating whether this qualifier type allows a\n * comma-separated list of runtime values in the context. Defaults to `false`.\n */\n protected constructor({ name, index, allowContextList }: IQualifierTypeCreateParams) {\n this.name = Convert.qualifierTypeName.convert(name).orThrow();\n this.allowContextList = allowContextList === true;\n this._collectible = new Collections.Collectible<QualifierTypeName, QualifierTypeIndex>({\n key: this.name,\n index: index,\n indexConverter: Convert.qualifierTypeIndex\n });\n }\n\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.isValidConditionValue}\n */\n public abstract isValidConditionValue(value: string): value is QualifierConditionValue;\n\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.isValidContextValue}\n */\n public isValidContextValue(value: string): value is QualifierContextValue {\n if (this.isValidConditionValue(value)) {\n return true;\n }\n if (this.allowContextList) {\n /* c8 ignore next 1 - functional code tested but coverage intermittently missed */\n return value.split(',').every((v) => this.isValidConditionValue(v.trim()));\n }\n /* c8 ignore next 1 - functional code tested but coverage intermittently missed */\n return false;\n }\n\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.isPotentialMatch}\n */\n public isPotentialMatch(conditionValue: string, contextValue: string): boolean {\n if (this.isValidConditionValue(conditionValue) && this.isValidContextValue(contextValue)) {\n return this._matchOne(conditionValue, contextValue, 'matches') !== NoMatch;\n }\n return false;\n }\n\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.validateCondition}\n */\n public validateCondition(value: string, operator?: ConditionOperator): Result<QualifierConditionValue> {\n operator = operator ?? 'matches';\n /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */\n if (operator !== 'matches') {\n return fail(`${operator}: invalid condition operator`);\n /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */\n } else if (!this.isValidConditionValue(value)) {\n return fail(`${value}: invalid condition value for qualifierType ${this.name}`);\n }\n return succeed(value);\n }\n\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.validateContextValue}\n */\n public validateContextValue(value: string): Result<QualifierContextValue> {\n /* c8 ignore next 2 - functional error case tested but coverage intermittently missed */\n if (!this.isValidContextValue(value)) {\n return fail(`${value}: invalid context value for qualifierType ${this.name}`);\n }\n return succeed(value);\n }\n\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.matches}\n */\n public matches(\n condition: QualifierConditionValue,\n context: QualifierContextValue,\n operator: ConditionOperator\n ): QualifierMatchScore {\n if (this.allowContextList) {\n return this._matchList(condition, QualifierType._splitContext(context), operator);\n }\n return this._matchOne(condition, context, operator);\n }\n\n /**\n * {@inheritdoc QualifierTypes.IQualifierType.setIndex}\n */\n public setIndex(index: number): Result<QualifierTypeIndex> {\n return this._collectible.setIndex(index);\n }\n\n /**\n * {@inheritdoc Validate.isValidQualifierTypeName}\n */\n public static isValidName(name: string): name is QualifierTypeName {\n return Validate.isValidQualifierTypeName(name);\n }\n\n /**\n * {@inheritdoc Validate.isValidQualifierTypeIndex}\n */\n public static isValidIndex(index: number): index is QualifierTypeIndex {\n return Validate.isValidQualifierTypeIndex(index);\n }\n\n /**\n * Compares two qualifier types by index.\n * @param t1 - The first qualifier type to compare.\n * @param t2 - The second qualifier type to compare.\n * @returns a number indicating the relative order of the two qualifier types.\n */\n public static compare(t1: QualifierType, t2: QualifierType): number {\n const i1 = t1._collectible.index ?? -1;\n const i2 = t2._collectible.index ?? -1;\n let diff = i1 - i2;\n\n if (diff === 0) {\n diff = t1.name.localeCompare(t2.name);\n }\n\n return diff;\n }\n\n /**\n * Matches a single condition value against a single context value.\n * @param condition - The {@link QualifierConditionValue | condition value} to match.\n * @param context - The {@link QualifierContextValue | context value} to match.\n * @param operator - The {@link ConditionOperator | operator} to use in the match.\n * @returns a {@link QualifierMatchScore | score} indicating the extent to which\n * the condition matches the context value.\n * @public\n */\n protected abstract _matchOne(\n condition: QualifierConditionValue,\n context: QualifierContextValue,\n operator: ConditionOperator\n ): QualifierMatchScore;\n\n /**\n * Matches a single condition value against a list of context values.\n * @param condition - The {@link QualifierConditionValue | condition value} to match.\n * @param context - The comma-separated list of {@link QualifierContextValue | context values} to match.\n * @param operator - The {@link ConditionOperator | operator} to use in the match.\n * @returns a {@link QualifierMatchScore | score} indicating the extent to which\n * the condition matches the context value.\n * @public\n */\n protected _matchList(\n condition: QualifierConditionValue,\n context: QualifierContextValue[],\n operator: ConditionOperator\n ): QualifierMatchScore {\n const scorePerPosition = 1 / context.length;\n let baseScore = PerfectMatch - scorePerPosition;\n\n for (let i = 0; i < context.length; i++) {\n const score = this._matchOne(condition, context[i], operator);\n if (score > NoMatch) {\n const adjusted = baseScore + scorePerPosition * score;\n if (Validate.isValidQualifierMatchScore(adjusted)) {\n return adjusted;\n }\n }\n baseScore -= scorePerPosition;\n }\n return NoMatch;\n }\n\n /**\n * Splits a comma-separated {@link QualifierContextValue | context value} into an\n * array of individual values.\n * @param value - The value to split.\n * @returns an array of individual context values.\n * @public\n */\n protected static _splitContext(value: QualifierContextValue): QualifierContextValue[] {\n return value.split(',').map((s) => s.trim() as QualifierContextValue);\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fgv/ts-res",
|
|
3
|
-
"version": "5.0.0-
|
|
3
|
+
"version": "5.0.0-2",
|
|
4
4
|
"description": "Multi-dimensional Resource Runtime",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "dist/ts-res.d.ts",
|
|
@@ -44,22 +44,22 @@
|
|
|
44
44
|
"@rushstack/heft-node-rig": "~2.9.0",
|
|
45
45
|
"@types/heft-jest": "1.0.6",
|
|
46
46
|
"eslint-plugin-tsdoc": "~0.4.0",
|
|
47
|
-
"@fgv/ts-
|
|
48
|
-
"@fgv/ts-utils-jest": "5.0.0-
|
|
49
|
-
"@fgv/ts-
|
|
50
|
-
"@fgv/ts-bcp47": "5.0.0-
|
|
51
|
-
"@fgv/ts-
|
|
52
|
-
"@fgv/ts-json": "5.0.0-
|
|
47
|
+
"@fgv/ts-json-base": "5.0.0-2",
|
|
48
|
+
"@fgv/ts-utils-jest": "5.0.0-2",
|
|
49
|
+
"@fgv/ts-extras": "5.0.0-2",
|
|
50
|
+
"@fgv/ts-bcp47": "5.0.0-2",
|
|
51
|
+
"@fgv/ts-utils": "5.0.0-2",
|
|
52
|
+
"@fgv/ts-json": "5.0.0-2"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
55
|
"luxon": "^3.6.1"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|
|
58
|
-
"@fgv/ts-
|
|
59
|
-
"@fgv/ts-
|
|
60
|
-
"@fgv/ts-json": "5.0.0-
|
|
61
|
-
"@fgv/ts-
|
|
62
|
-
"@fgv/ts-
|
|
58
|
+
"@fgv/ts-extras": "5.0.0-2",
|
|
59
|
+
"@fgv/ts-utils": "5.0.0-2",
|
|
60
|
+
"@fgv/ts-json-base": "5.0.0-2",
|
|
61
|
+
"@fgv/ts-bcp47": "5.0.0-2",
|
|
62
|
+
"@fgv/ts-json": "5.0.0-2"
|
|
63
63
|
},
|
|
64
64
|
"scripts": {
|
|
65
65
|
"build": "heft build --clean",
|