@@ -81,50 +81,45 @@ export abstract class ContextKeyExpr {
81
81
public static false ( ) : ContextKeyExpression {
82
82
return ContextKeyFalseExpr . INSTANCE ;
83
83
}
84
-
85
84
public static true ( ) : ContextKeyExpression {
86
85
return ContextKeyTrueExpr . INSTANCE ;
87
86
}
88
-
89
87
public static has ( key : string ) : ContextKeyExpression {
90
88
return ContextKeyDefinedExpr . create ( key ) ;
91
89
}
92
-
93
90
public static equals ( key : string , value : any ) : ContextKeyExpression {
94
91
return ContextKeyEqualsExpr . create ( key , value ) ;
95
92
}
96
-
97
93
public static notEquals ( key : string , value : any ) : ContextKeyExpression {
98
94
return ContextKeyNotEqualsExpr . create ( key , value ) ;
99
95
}
100
-
101
96
public static regex ( key : string , value : RegExp ) : ContextKeyExpression {
102
97
return ContextKeyRegexExpr . create ( key , value ) ;
103
98
}
104
-
105
99
public static in ( key : string , value : string ) : ContextKeyExpression {
106
100
return ContextKeyInExpr . create ( key , value ) ;
107
101
}
108
-
109
102
public static not ( key : string ) : ContextKeyExpression {
110
103
return ContextKeyNotExpr . create ( key ) ;
111
104
}
112
-
113
105
public static and ( ...expr : Array < ContextKeyExpression | undefined | null > ) : ContextKeyExpression | undefined {
114
106
return ContextKeyAndExpr . create ( expr , null ) ;
115
107
}
116
-
117
108
public static or ( ...expr : Array < ContextKeyExpression | undefined | null > ) : ContextKeyExpression | undefined {
118
109
return ContextKeyOrExpr . create ( expr , null , true ) ;
119
110
}
120
-
121
- public static greater ( key : string , value : any ) : ContextKeyExpression {
111
+ public static greater ( key : string , value : number ) : ContextKeyExpression {
122
112
return ContextKeyGreaterExpr . create ( key , value ) ;
123
113
}
124
-
125
- public static less ( key : string , value : any ) : ContextKeyExpression {
114
+ public static greaterEquals ( key : string , value : number ) : ContextKeyExpression {
115
+ return ContextKeyGreaterEqualsExpr . create ( key , value ) ;
116
+ }
117
+ public static smaller ( key : string , value : number ) : ContextKeyExpression {
126
118
return ContextKeySmallerExpr . create ( key , value ) ;
127
119
}
120
+ public static smallerEquals ( key : string , value : number ) : ContextKeyExpression {
121
+ return ContextKeySmallerEqualsExpr . create ( key , value ) ;
122
+ }
128
123
129
124
public static deserialize ( serialized : string | null | undefined , strict : boolean = false ) : ContextKeyExpression | undefined {
130
125
if ( ! serialized ) {
@@ -741,17 +736,30 @@ export class ContextKeyNotExpr implements IContextKeyExpression {
741
736
}
742
737
}
743
738
739
+ function withFloatOrStr < T extends ContextKeyExpression > ( value : any , callback : ( value : number | string ) => T ) : T | ContextKeyFalseExpr {
740
+ if ( typeof value === 'string' ) {
741
+ const n = parseFloat ( value ) ;
742
+ if ( ! isNaN ( n ) ) {
743
+ value = n ;
744
+ }
745
+ }
746
+ if ( typeof value === 'string' || typeof value === 'number' ) {
747
+ return callback ( value ) ;
748
+ }
749
+ return ContextKeyFalseExpr . INSTANCE ;
750
+ }
751
+
744
752
export class ContextKeyGreaterExpr implements IContextKeyExpression {
745
753
746
- public static create ( key : string , value : any , negated : ContextKeyExpression | null = null ) : ContextKeyExpression {
747
- return new ContextKeyGreaterExpr ( key , value , negated ) ;
754
+ public static create ( key : string , _value : any , negated : ContextKeyExpression | null = null ) : ContextKeyExpression {
755
+ return withFloatOrStr ( _value , ( value ) => new ContextKeyGreaterExpr ( key , value , negated ) ) ;
748
756
}
749
757
750
758
public readonly type = ContextKeyExprType . Greater ;
751
759
752
760
private constructor (
753
761
private readonly key : string ,
754
- private readonly value : any ,
762
+ private readonly value : number | string ,
755
763
private negated : ContextKeyExpression | null
756
764
) { }
757
765
@@ -774,7 +782,10 @@ export class ContextKeyGreaterExpr implements IContextKeyExpression {
774
782
}
775
783
776
784
public evaluate ( context : IContext ) : boolean {
777
- return ( parseFloat ( < any > context . getValue ( this . key ) ) > parseFloat ( this . value ) ) ;
785
+ if ( typeof this . value === 'string' ) {
786
+ return false ;
787
+ }
788
+ return ( parseFloat ( < any > context . getValue ( this . key ) ) > this . value ) ;
778
789
}
779
790
780
791
public serialize ( ) : string {
@@ -799,15 +810,15 @@ export class ContextKeyGreaterExpr implements IContextKeyExpression {
799
810
800
811
export class ContextKeyGreaterEqualsExpr implements IContextKeyExpression {
801
812
802
- public static create ( key : string , value : any , negated : ContextKeyExpression | null = null ) : ContextKeyExpression {
803
- return new ContextKeyGreaterEqualsExpr ( key , value , negated ) ;
813
+ public static create ( key : string , _value : any , negated : ContextKeyExpression | null = null ) : ContextKeyExpression {
814
+ return withFloatOrStr ( _value , ( value ) => new ContextKeyGreaterEqualsExpr ( key , value , negated ) ) ;
804
815
}
805
816
806
817
public readonly type = ContextKeyExprType . GreaterEquals ;
807
818
808
819
private constructor (
809
820
private readonly key : string ,
810
- private readonly value : any ,
821
+ private readonly value : number | string ,
811
822
private negated : ContextKeyExpression | null
812
823
) { }
813
824
@@ -830,7 +841,10 @@ export class ContextKeyGreaterEqualsExpr implements IContextKeyExpression {
830
841
}
831
842
832
843
public evaluate ( context : IContext ) : boolean {
833
- return ( parseFloat ( < any > context . getValue ( this . key ) ) >= parseFloat ( this . value ) ) ;
844
+ if ( typeof this . value === 'string' ) {
845
+ return false ;
846
+ }
847
+ return ( parseFloat ( < any > context . getValue ( this . key ) ) >= this . value ) ;
834
848
}
835
849
836
850
public serialize ( ) : string {
@@ -855,15 +869,15 @@ export class ContextKeyGreaterEqualsExpr implements IContextKeyExpression {
855
869
856
870
export class ContextKeySmallerExpr implements IContextKeyExpression {
857
871
858
- public static create ( key : string , value : any , negated : ContextKeyExpression | null = null ) : ContextKeyExpression {
859
- return new ContextKeySmallerExpr ( key , value , negated ) ;
872
+ public static create ( key : string , _value : any , negated : ContextKeyExpression | null = null ) : ContextKeyExpression {
873
+ return withFloatOrStr ( _value , ( value ) => new ContextKeySmallerExpr ( key , value , negated ) ) ;
860
874
}
861
875
862
876
public readonly type = ContextKeyExprType . Smaller ;
863
877
864
878
private constructor (
865
879
private readonly key : string ,
866
- private readonly value : any ,
880
+ private readonly value : number | string ,
867
881
private negated : ContextKeyExpression | null
868
882
) {
869
883
}
@@ -887,7 +901,10 @@ export class ContextKeySmallerExpr implements IContextKeyExpression {
887
901
}
888
902
889
903
public evaluate ( context : IContext ) : boolean {
890
- return ( parseFloat ( < any > context . getValue ( this . key ) ) < parseFloat ( this . value ) ) ;
904
+ if ( typeof this . value === 'string' ) {
905
+ return false ;
906
+ }
907
+ return ( parseFloat ( < any > context . getValue ( this . key ) ) < this . value ) ;
891
908
}
892
909
893
910
public serialize ( ) : string {
@@ -912,15 +929,15 @@ export class ContextKeySmallerExpr implements IContextKeyExpression {
912
929
913
930
export class ContextKeySmallerEqualsExpr implements IContextKeyExpression {
914
931
915
- public static create ( key : string , value : any , negated : ContextKeyExpression | null = null ) : ContextKeyExpression {
916
- return new ContextKeySmallerEqualsExpr ( key , value , negated ) ;
932
+ public static create ( key : string , _value : any , negated : ContextKeyExpression | null = null ) : ContextKeyExpression {
933
+ return withFloatOrStr ( _value , ( value ) => new ContextKeySmallerEqualsExpr ( key , value , negated ) ) ;
917
934
}
918
935
919
936
public readonly type = ContextKeyExprType . SmallerEquals ;
920
937
921
938
private constructor (
922
939
private readonly key : string ,
923
- private readonly value : any ,
940
+ private readonly value : number | string ,
924
941
private negated : ContextKeyExpression | null
925
942
) {
926
943
}
@@ -944,7 +961,10 @@ export class ContextKeySmallerEqualsExpr implements IContextKeyExpression {
944
961
}
945
962
946
963
public evaluate ( context : IContext ) : boolean {
947
- return ( parseFloat ( < any > context . getValue ( this . key ) ) <= parseFloat ( this . value ) ) ;
964
+ if ( typeof this . value === 'string' ) {
965
+ return false ;
966
+ }
967
+ return ( parseFloat ( < any > context . getValue ( this . key ) ) <= this . value ) ;
948
968
}
949
969
950
970
public serialize ( ) : string {
0 commit comments