-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (53 loc) · 1.5 KB
/
index.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
// each node has a attribute pathName, which in format of { criteria: {property1: 'aaa', property2: 'bbb'}, action: 'include'/'exclude'}
function Query(rules) {
this._compileSelections(rules);
}
function explainSelectionTreeNodesAsIfStatments(treeNodes) {
var lines = [];
(treeNodes || []).forEach(function (node) {
lines.push('if(' + JSON.stringify(node.rule.criteria) + ') {');
lines.push(' return ' + (node.rule.action !== 'exclude'));
lines.push('}');
});
return lines;
}
function extend(obj1, obj2) {
for (var key in obj2) {
var temp = {};
temp[key] = obj2[key];
obj1.push(temp);
}
return obj1;
}
function compileSelectionTreeToQuery(node) {
var query = {};
var nodeQueryExp = extend([], node.rule.criteria);
if (node.rule.action === 'exclude') {
query.$nor = nodeQueryExp;
} else {
query.$and = nodeQueryExp;
}
return query;
}
function compileSelectionTreeNodesToQuery(treeNodes) {
var query = {};
var ors = query.$or = [];
treeNodes.forEach(function (node) {
ors.push(compileSelectionTreeToQuery(node));
});
if (query.$or.length === 0) {
// negate the query
query = { _id: null };
}
return query;
}
Query.prototype._compileSelections = function (rules) {
var selectionNodes = rules.map(function (rule) {
return {
rule: rule
};
});
this.explanation = explainSelectionTreeNodesAsIfStatments(selectionNodes).join('\n');
this.query = compileSelectionTreeNodesToQuery(selectionNodes);
};
module.exports = Query;