-
Notifications
You must be signed in to change notification settings - Fork 1
/
where.js
94 lines (75 loc) · 2.44 KB
/
where.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
function poepleHaveNoThisField(people, predicate) {
for (var i in predicate) {
var scalar = predicate[i];
if (scalar[0] === 'COLUMN' && typeof people[scalar[1]] === 'undefined')
return true;
}
}
function comparisonCond(cond) {
return function(people) {
if (poepleHaveNoThisField(people, cond.predicate))
return false;
var extractedScalar = cond.predicate.map(function(scalar) {
return scalar[0] === 'COLUMN' ? people[scalar[1]] : scalar ;
});
if (extractedScalar[0] === '>=')
return extractedScalar[1] >= extractedScalar[2];
if (extractedScalar[0] === '<=')
return extractedScalar[1] <= extractedScalar[2];
if (extractedScalar[0] === '>')
return extractedScalar[1] > extractedScalar[2];
if (extractedScalar[0] === '<')
return extractedScalar[1] < extractedScalar[2];
if (extractedScalar[0] === '==')
return extractedScalar[1] === extractedScalar[2];
};
}
function likeCond(cond) {
return function(people) {
if (poepleHaveNoThisField(people, cond.predicate))
return false;
var extractedScalar = cond.predicate.map(function(scalar) {
return scalar[0] === 'COLUMN' ? people[scalar[1]] : scalar ;
});
if (extractedScalar[0] === null) return false;
return extractedScalar[0].match(new RegExp(extractedScalar[1], 'i'));
};
}
function testNullCond(cond) {
return function(people) {
if (cond.predicate[0] === 'NOT') {
var field = people[cond.predicate[1]];
return typeof field === 'undefined' || field === null ? false : true;
} else {
var field = people[cond.predicate[0]];
return typeof field === 'undefined' || field === null ? true : false;
}
};
}
function condition2Filter(cond) {
if (cond.type === 'COMPARISON') {
return comparisonCond(cond);
}
if (cond.type === 'LIKE') {
return likeCond(cond);
}
if (cond.type === 'TEST_NULL') {
return testNullCond(cond);
}
if (cond.type === 'OR') {
return function(people) {
return condition2Filter(cond.condition)(people) || condition2Filter(cond.condition_another)(people);
};
}
if (cond.type === 'AND') {
return function(people) {
return condition2Filter(cond.condition)(people) && condition2Filter(cond.condition_another)(people);
};
}
if (cond.type === 'NOT') {
return function(people) {
return !condition2Filter(cond.condition)(people);
};
}
}
exports.condition2Filter = condition2Filter;