@@ -41,18 +41,36 @@ export const matchingFunctions = {
4141 ! ! otherValue && otherValue . includes ( thisValue )
4242} ;
4343
44+ // Semver library throws an error if the version is invalid, in this case, we want to catch and return false
45+ const safeSemverCompare = (
46+ semverMatchingFunction : ( conditionValue : any , traitValue : any ) => boolean
47+ ) => {
48+ return ( conditionValue : any , traitValue : any ) => {
49+ try {
50+ return semverMatchingFunction ( conditionValue , traitValue ) ;
51+ } catch {
52+ return false ;
53+ }
54+ } ;
55+ } ;
56+
4457export const semverMatchingFunction = {
4558 ...matchingFunctions ,
46- [ CONDITION_OPERATORS . EQUAL ] : ( thisValue : any , otherValue : any ) =>
47- semver . eq ( thisValue , otherValue ) ,
48- [ CONDITION_OPERATORS . GREATER_THAN ] : ( thisValue : any , otherValue : any ) =>
49- semver . gt ( otherValue , thisValue ) ,
50- [ CONDITION_OPERATORS . GREATER_THAN_INCLUSIVE ] : ( thisValue : any , otherValue : any ) =>
51- semver . gte ( otherValue , thisValue ) ,
52- [ CONDITION_OPERATORS . LESS_THAN ] : ( thisValue : any , otherValue : any ) =>
53- semver . gt ( thisValue , otherValue ) ,
54- [ CONDITION_OPERATORS . LESS_THAN_INCLUSIVE ] : ( thisValue : any , otherValue : any ) =>
55- semver . gte ( thisValue , otherValue )
59+ [ CONDITION_OPERATORS . EQUAL ] : safeSemverCompare ( ( conditionValue , traitValue ) =>
60+ semver . eq ( traitValue , conditionValue )
61+ ) ,
62+ [ CONDITION_OPERATORS . GREATER_THAN ] : safeSemverCompare ( ( conditionValue , traitValue ) =>
63+ semver . gt ( traitValue , conditionValue )
64+ ) ,
65+ [ CONDITION_OPERATORS . GREATER_THAN_INCLUSIVE ] : safeSemverCompare ( ( conditionValue , traitValue ) =>
66+ semver . gte ( traitValue , conditionValue )
67+ ) ,
68+ [ CONDITION_OPERATORS . LESS_THAN ] : safeSemverCompare ( ( conditionValue , traitValue ) =>
69+ semver . lt ( traitValue , conditionValue )
70+ ) ,
71+ [ CONDITION_OPERATORS . LESS_THAN_INCLUSIVE ] : safeSemverCompare ( ( conditionValue , traitValue ) =>
72+ semver . lte ( traitValue , conditionValue )
73+ )
5674} ;
5775
5876export const getMatchingFunctions = ( semver : boolean ) =>
0 commit comments