forked from bi-tm/express-nedb-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.js
97 lines (88 loc) · 4.09 KB
/
filter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var grammar = {
"lex": {
"rules": [
["\\s+", "/* skip whitespace */"],
["\\$(and|AND)\\b", "return 'AND';"],
["\\$(or|OR)\\b", "return 'OR';"],
["\\$(not|NOT)\\b", "return 'NOT';"],
["\\$(eq|EQ)\\b", "return 'EQ';"],
["\\$(ne|NE)\\b", "return 'NE';"],
["\\$(lt|LT)\\b", "return 'LT';"],
["\\$(lte|LTE)\\b", "return 'LTE';"],
["\\$(gt|GT)\\b", "return 'GT';"],
["\\$(gte|GTE)\\b", "return 'GTE';"],
["\\$(exists|EXISTS)\\b", "return 'EXISTS';"],
["\\$(regex|REGEX)\\b", "return 'REGEX';"],
["\\$(nin|NIN)\\b", "return 'NIN';"],
["\\$(in|IN)\\b", "return 'IN';"],
["\\(", "return '(';"],
["\\)", "return ')';"],
["true|TRUE|false|FALSE\\b", "return 'BOOLEAN';"],
["\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(Z|(\\+|-)\\d{2}:\\d{2})\\b", "return 'DATETIME';"],
["\\d{4}-\\d{2}-\\d{2}\\b", "return 'DATE';"],
["(\\d+,)+\\d+", "return 'NUM_ARRAY';"],
["(\\w+,)+\\w+", "return 'ARRAY';"],
["\\d+(\\.\\d+)?\\b", "return 'NUMBER';"],
["\\'[^\\']*\\'", "return 'LITERAL';"],
["\"[^\"]*\"", "return 'LITERAL';"],
["\\.", "return 'DOT';"],
["\\w\\S+(\\b|%)", "return 'WORD';"],
["$", "return 'EOF';"]
]
},
"operators": [
["left", "EXISTS"],
["left", "IN", "NIN", "EQ", "NE", "LT", "LTE", "GT", "GTE", "REGEX"],
["left", "AND"],
["left", "OR"],
["left", "NOT"]
],
"bnf": {
"filter": [
["e EOF", "return $1;"]
],
"e": [
["c AND c", "$$ = new Object(); $$['$and'] = [$1,$3];"],
["c OR c", "$$ = new Object(); $$['$or'] = [$1,$3];"],
["NOT c", "$$ = new Object(); $$['$not'] = $2;"],
["c", "$$ = $1;"]
],
"c": [
["l EQ r", "$$ = new Object; $$[$1] = $3;"],
["l NE r", "$$ = new Object(); $$[$1] = new Object(); $$[$1]['$ne'] = $3;"],
["l LT r", "$$ = new Object(); $$[$1] = new Object(); $$[$1]['$lt'] = $3;"],
["l LTE r", "$$ = new Object(); $$[$1] = new Object(); $$[$1]['$lte'] = $3;"],
["l GT r", "$$ = new Object(); $$[$1] = new Object(); $$[$1]['$gt'] = $3;"],
["l GTE r", "$$ = new Object(); $$[$1] = new Object(); $$[$1]['$gte'] = $3;"],
["l IN r", "$$ = new Object(); $$[$1] = new Object(); $$[$1]['$in'] = $3;"],
["l NIN r", "$$ = new Object(); $$[$1] = new Object(); $$[$1]['$nin'] = $3;"],
["EXISTS l", "$$ = new Object(); $$[$2] = new Object(); $$[$2]['$exists'] = true;"],
["l REGEX r", "$$ = new Object(); $$[$1] = new Object(); $$[$1]['$regex'] = new RegExp($3);"],
["( e )", "$$ = $2;"]
],
"l": [
["WORD", "$$=yytext;"],
["WORD DOT l", "$$ = $1+$2+$3;"]
],
"r": [
["ARRAY", "$$ = yytext.split(',')"],
["NUM_ARRAY", "$$ = yytext.split(',').map(a => Number(a));"],
["DATE", "$$ = new Date(yytext);"],
["DATETIME", "$$ = new Date(yytext);"],
["NUMBER", "$$ = Number(yytext);"],
["LITERAL", "$$ = yytext.slice(1,yytext.length-1);"],
["BOOLEAN", "$$ = yytext.toLowerCase() === 'true';"],
["WORD", "$$ = yytext;"]
]
}
};
var Parser = require("jison").Parser;
var parser = new Parser(grammar);
module.exports = function(input) {
if (typeof(input) == 'string' && input != "") {
return parser.parse(decodeURI(input));
}
else {
return {};
}
};