11import type { Expression } from './types' ;
2- import { expressionToRPN } from './evaluation' ;
2+ import { evaluate } from './evaluation' ;
33
44const baseExpression : Expression = {
55 joiner : 'and' ,
@@ -73,15 +73,15 @@ const singleRuleExpression: Expression = {
7373 ] ,
7474} ;
7575
76- const variablesValueMap = new Map (
76+ const variablesValueMap = new Map < string , string > (
7777 [
7878 {
7979 id : 'variable-id-a' ,
80- value : 'a ' ,
80+ value : 'A ' ,
8181 } ,
8282 {
8383 id : 'variable-id-b' ,
84- value : 'b ' ,
84+ value : 'B ' ,
8585 } ,
8686 {
8787 id : 'variable-id-c' ,
@@ -98,20 +98,26 @@ const variablesValueMap = new Map(
9898 ] . map ( ( { id, value} ) => [ id , value ] )
9999) ;
100100
101- describe ( 'expressionToRPN' , ( ) => {
102- it ( 'should return `[true]`' , ( ) => {
103- expect ( expressionToRPN ( baseExpression , variablesValueMap ) ) . toEqual ( [ true ] ) ;
101+ describe ( 'evaluate' , ( ) => {
102+ it ( 'should return `true`' , ( ) => {
103+ expect (
104+ evaluate ( { expression : baseExpression , variableIdToVariablesMap : variablesValueMap } )
105+ ) . toEqual ( true ) ;
104106 } ) ;
105107
106- it ( 'should return `[true]` for expression w/ non-binary operator' , ( ) => {
107- expect ( expressionToRPN ( notBinaryExpression , variablesValueMap ) ) . toEqual ( [ true ] ) ;
108+ it ( 'should return `true` for expression w/ non-binary operator' , ( ) => {
109+ expect (
110+ evaluate ( { expression : notBinaryExpression , variableIdToVariablesMap : variablesValueMap } )
111+ ) . toEqual ( true ) ;
108112 } ) ;
109113
110- it ( 'should return `[true]` for single rule when passed correct variable value' , ( ) => {
111- expect ( expressionToRPN ( singleRuleExpression , variablesValueMap ) ) . toEqual ( [ true ] ) ;
114+ it ( 'should return `true` for single rule when passed correct variable value' , ( ) => {
115+ expect (
116+ evaluate ( { expression : singleRuleExpression , variableIdToVariablesMap : variablesValueMap } )
117+ ) . toEqual ( true ) ;
112118 } ) ;
113119
114- it ( 'should return `[ false] ` for single rule when passed wrong variable value' , ( ) => {
120+ it ( 'should return `false` for single rule when passed wrong variable value' , ( ) => {
115121 const variablesValueMap = new Map ( [ [ 'variable-id-a' , 'wrong-value' ] ] ) ;
116122
117123 const expression : Expression = {
@@ -124,10 +130,10 @@ describe('expressionToRPN', () => {
124130 ] ,
125131 } ;
126132
127- expect ( expressionToRPN ( expression , variablesValueMap ) ) . toEqual ( [ false ] ) ;
133+ expect ( evaluate ( { expression, variableIdToVariablesMap : variablesValueMap } ) ) . toEqual ( false ) ;
128134 } ) ;
129135
130- it ( 'should return `[ false] ` when variables value not passed' , ( ) => {
136+ it ( 'should return `false` when variables value not passed' , ( ) => {
131137 const variablesValueMap = new Map ( ) ;
132138
133139 const expression : Expression = {
@@ -140,12 +146,12 @@ describe('expressionToRPN', () => {
140146 ] ,
141147 } ;
142148
143- expect ( expressionToRPN ( expression , variablesValueMap ) ) . toEqual ( [ false ] ) ;
149+ expect ( evaluate ( { expression, variableIdToVariablesMap : variablesValueMap } ) ) . toEqual ( false ) ;
144150 } ) ;
145151
146152 const variableValueSomething = new Map ( [ [ 'variable-id-a' , 'something' ] ] ) ;
147153
148- it ( 'should return `[ true] ` for expression and `contains` rule' , ( ) => {
154+ it ( 'should return `true` for expression and `contains` rule' , ( ) => {
149155 const expression : Expression = {
150156 rules : [
151157 {
@@ -156,10 +162,10 @@ describe('expressionToRPN', () => {
156162 ] ,
157163 } ;
158164
159- expect ( expressionToRPN ( expression , variableValueSomething ) ) . toEqual ( [ true ] ) ;
165+ expect ( evaluate ( { expression, variableIdToVariablesMap : variableValueSomething } ) ) . toEqual ( true ) ;
160166 } ) ;
161167
162- it ( 'should return `[ false] ` for expression and `contains` rule' , ( ) => {
168+ it ( 'should return `false` for expression and `contains` rule' , ( ) => {
163169 const expression : Expression = {
164170 rules : [
165171 {
@@ -170,10 +176,10 @@ describe('expressionToRPN', () => {
170176 ] ,
171177 } ;
172178
173- expect ( expressionToRPN ( expression , variableValueSomething ) ) . toEqual ( [ false ] ) ;
179+ expect ( evaluate ( { expression, variableIdToVariablesMap : variableValueSomething } ) ) . toEqual ( false ) ;
174180 } ) ;
175181
176- it ( 'should return `[ false] ` for expression and `not_contains` rule' , ( ) => {
182+ it ( 'should return `false` for expression and `not_contains` rule' , ( ) => {
177183 const expression : Expression = {
178184 rules : [
179185 {
@@ -184,10 +190,10 @@ describe('expressionToRPN', () => {
184190 ] ,
185191 } ;
186192
187- expect ( expressionToRPN ( expression , variableValueSomething ) ) . toEqual ( [ false ] ) ;
193+ expect ( evaluate ( { expression, variableIdToVariablesMap : variableValueSomething } ) ) . toEqual ( false ) ;
188194 } ) ;
189195
190- it ( 'should return `[ true] ` for expression and `not_contains` rule' , ( ) => {
196+ it ( 'should return `true` for expression and `not_contains` rule' , ( ) => {
191197 const expression : Expression = {
192198 rules : [
193199 {
@@ -198,10 +204,10 @@ describe('expressionToRPN', () => {
198204 ] ,
199205 } ;
200206
201- expect ( expressionToRPN ( expression , variableValueSomething ) ) . toEqual ( [ true ] ) ;
207+ expect ( evaluate ( { expression, variableIdToVariablesMap : variableValueSomething } ) ) . toEqual ( true ) ;
202208 } ) ;
203209
204- it ( 'should return `[ true] ` for complex expression' , ( ) => {
210+ it ( 'should return `true` for complex expression' , ( ) => {
205211 const complexExpression : Expression = {
206212 joiner : 'and' ,
207213 rules : [
@@ -225,10 +231,12 @@ describe('expressionToRPN', () => {
225231 ] ,
226232 } ;
227233
228- expect ( expressionToRPN ( complexExpression , variablesValueMap ) ) . toEqual ( [ true ] ) ;
234+ expect (
235+ evaluate ( { expression : complexExpression , variableIdToVariablesMap : variablesValueMap } )
236+ ) . toEqual ( true ) ;
229237 } ) ;
230238
231- it ( 'should return `[ false] ` for complex expression' , ( ) => {
239+ it ( 'should return `false` for complex expression' , ( ) => {
232240 const complexExpression : Expression = {
233241 joiner : 'and' ,
234242 rules : [
@@ -252,6 +260,41 @@ describe('expressionToRPN', () => {
252260 ] ,
253261 } ;
254262
255- expect ( expressionToRPN ( complexExpression , variablesValueMap ) ) . toEqual ( [ false ] ) ;
263+ expect (
264+ evaluate ( { expression : complexExpression , variableIdToVariablesMap : variablesValueMap } )
265+ ) . toEqual ( false ) ;
266+ } ) ;
267+
268+ it ( 'should return `false` for case sensitive option' , ( ) => {
269+ expect (
270+ evaluate ( {
271+ expression : baseExpression ,
272+ variableIdToVariablesMap : variablesValueMap ,
273+ options : { caseSensitive : true } ,
274+ } )
275+ ) . toEqual ( false ) ;
276+ } ) ;
277+
278+ it ( 'should return `true` for case sensitive option' , ( ) => {
279+ const variablesLowerCaseValueMap = new Map < string , string > (
280+ [
281+ {
282+ id : 'variable-id-a' ,
283+ value : 'a' ,
284+ } ,
285+ {
286+ id : 'variable-id-b' ,
287+ value : 'b' ,
288+ } ,
289+ ] . map ( ( { id, value} ) => [ id , value ] )
290+ ) ;
291+
292+ expect (
293+ evaluate ( {
294+ expression : baseExpression ,
295+ variableIdToVariablesMap : variablesLowerCaseValueMap ,
296+ options : { caseSensitive : true } ,
297+ } )
298+ ) . toEqual ( true ) ;
256299 } ) ;
257300} ) ;
0 commit comments