Skip to content

Commit

Permalink
Support regular expression matches
Browse files Browse the repository at this point in the history
Introduce an operator (~=) to perform matches using regular expressions:

    {country: "US"}
    country ~= "^(US|CA)"
  • Loading branch information
msantos committed Mar 5, 2015
1 parent fa299f8 commit e446ae9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ x < y | Less than
x <= y | Less than or equal to
x > y | Greater than
x >= y | Greater than or equal to
x ~= y | Regular expression match
x in (a, b, c) | Equivalent to (x == a or x == b or x == c)
x not in (a, b, c) | Equivalent to (x != a and x != b and x != c)

Expand Down
4 changes: 3 additions & 1 deletion filtrex.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function filtrexParser() {
['\\,', 'return ",";'],
['==', 'return "==";'],
['\\!=', 'return "!=";'],
['\\~=', 'return "~=";'],
['>=', 'return ">=";'],
['<=', 'return "<=";'],
['<', 'return "<";'],
Expand Down Expand Up @@ -120,7 +121,7 @@ function filtrexParser() {
['left', 'or'],
['left', 'and'],
['left', 'in'],
['left', '==', '!='],
['left', '==', '!=', '~='],
['left', '<', '<=', '>', '>='],
['left', '+', '-'],
['left', '*', '/', '%'],
Expand All @@ -146,6 +147,7 @@ function filtrexParser() {
['not e' , code(['Number(!', 2, ')'])],
['e == e' , code(['Number(', 1, '==', 3, ')'])],
['e != e' , code(['Number(', 1, '!=', 3, ')'])],
['e ~= e' , code(['RegExp(', 3, ').test(', 1, ')'])],
['e < e' , code(['Number(', 1, '<' , 3, ')'])],
['e <= e' , code(['Number(', 1, '<=', 3, ')'])],
['e > e' , code(['Number(', 1, '> ', 3, ')'])],
Expand Down
4 changes: 3 additions & 1 deletion src/filtrex.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function filtrexParser() {
['\\,', 'return ",";'],
['==', 'return "==";'],
['\\!=', 'return "!=";'],
['\\~=', 'return "~=";'],
['>=', 'return ">=";'],
['<=', 'return "<=";'],
['<', 'return "<";'],
Expand Down Expand Up @@ -120,7 +121,7 @@ function filtrexParser() {
['left', 'or'],
['left', 'and'],
['left', 'in'],
['left', '==', '!='],
['left', '==', '!=', '~='],
['left', '<', '<=', '>', '>='],
['left', '+', '-'],
['left', '*', '/', '%'],
Expand All @@ -146,6 +147,7 @@ function filtrexParser() {
['not e' , code(['Number(!', 2, ')'])],
['e == e' , code(['Number(', 1, '==', 3, ')'])],
['e != e' , code(['Number(', 1, '!=', 3, ')'])],
['e ~= e' , code(['RegExp(', 3, ').test(', 1, ')'])],
['e < e' , code(['Number(', 1, '<' , 3, ')'])],
['e <= e' , code(['Number(', 1, '<=', 3, ')'])],
['e > e' , code(['Number(', 1, '> ', 3, ')'])],
Expand Down
5 changes: 5 additions & 0 deletions test/filtrex-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
eq(1, compileExpression('foo not in ("aa", "bb")')({foo:'cc'}));
},

'regexp test': function() {
eq(1, compileExpression('foo ~= "^[hH]ello"')({foo:'hello'}));
eq(0, compileExpression('foo ~= "^[hH]ello"')({foo:'bye'}));
},

'a ? b : c': function() {
eq(4, compileExpression('1 > 2 ? 3 : 4')());
eq(3, compileExpression('1 < 2 ? 3 : 4')());
Expand Down

0 comments on commit e446ae9

Please sign in to comment.