-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfquery.js
144 lines (122 loc) · 3.69 KB
/
fquery.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var create_fquery_delegator = function(name) {
return function() {
var args = arguments;
return function(item) {
return item[name].apply(item, args);
};
};
};
var fquerybuilder = function(key_name, base, config) {
var fquery = {
flat: !key_name,
key_name: key_name || "___",
base: base,
processors: [],
result: [],
config: config || {},
lock: false
};
fquery.not = function(condition) {
return function(item) {
return !condition(item);
};
};
fquery.prepare = function(func) {
fquery.prepare_handler = func;
return fquery;
};
fquery.enter = function(condition, action) {
var processor = {
condition: condition,
action: action
};
fquery.processors.push(processor);
return fquery;
};
fquery.exit = function(func) {
fquery.exit_handler = func;
return fquery;
};
fquery.end = function(func) {
fquery.end_handler = func;
return fquery;
};
fquery.select = function(key) {
var selection;
for (var i = 0; i < fquery.result.length; i++) {
if (fquery.result[i][fquery.key_name] === key) {
selection = fquery.result[i];
}
}
if (selection === undefined) {
var item = {};
item[fquery.key_name] = key;
for (var prop in base) {
item[prop] = base[prop];
}
fquery.result.push(item);
fquery.last_selection = item;
return item;
} else {
fquery.last_selection = selection;
return selection;
}
};
fquery.count = function(counter_name, condition, key, lock) {
fquery.enter(condition, function(item, fq) {
var selector = fq.select(key);
if (!selector.hasOwnProperty(counter_name)) {
selector[counter_name] = 0;
}
selector[counter_name]++;
if (lock) {
fquery.lock = true;
}
});
return fquery;
};
fquery.run = function(data, override_config) {
var config = override_config || fquery.config;
fquery.index = -1;
fquery.source = data;
fquery.result = [];
fquery.last_selection = undefined;
if (fquery.prepare_handler) {
fquery.prepare_handler(fquery);
}
data.forEach(function(item) {
fquery.index++;
fquery.processors.forEach(function(processor) {
if (!fquery.lock && (processor.condition === true || processor.condition(item))) {
processor.action(item, fquery);
}
});
fquery.lock = false;
});
if (fquery.exit_handler) {
fquery.result.forEach(function(item) {
fquery.exit_handler(item, fquery);
});
}
if (config.sort_by) {
var sort_prop = config.sort_by;
fquery.result.sort(function(a, b) {
return config.asc ? a[sort_prop] - b[sort_prop] : b[sort_prop] - a[sort_prop];
});
}
if (fquery.end_handler) {
fquery.end_handler(fquery.result, fquery);
}
return fquery.flat && fquery.result.length ? fquery.result[0] : fquery.result;
};
return fquery;
};
function create(helpers) {
var fquery = {ops: {}};
for (var name in helpers.operators) {
fquery.ops[name] = create_fquery_delegator(name);
}
fquery.factory = fquerybuilder;
return fquery;
}
module.exports = create;