-
Notifications
You must be signed in to change notification settings - Fork 0
/
where.js
executable file
·59 lines (44 loc) · 1.73 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
(function() {
var sanitizeCondition = function(condition, params) {
if (params) {
params = JSON.parse(JSON.stringify(params));
for (var key in params)
condition = condition.replace("@" + key, params[key]);
}
return condition;
};
Array.prototype.where = function(condition, params) {
condition = sanitizeCondition(condition, params);
var func = eval("Array.prototype.where.cache = function() { return " + condition + "; };"),
result = [];
for (var i = 0, l = this.length; i != l; i++) {
if (func.apply(this[i]))
result.push(this[i]);
}
return result;
};
Array.prototype.link = function(otherArray, condition, params) {
condition = sanitizeCondition(condition, params);
var func = eval("Array.prototype.where.cache = function(other) { return " + condition + "; };"),
result = [];
for (var i = 0, l = this.length; i != l; i++) {
for (var i2 = 0, l2 = otherArray.length; i2 != l2; i2++) {
if (func.call(this[i], otherArray[i2]))
result.push({ left: this[i], right: otherArray[i2] });
}
}
return result;
};
Array.prototype.orderBy = function(fields) {
fields = fields.split(/[\s;,]+/);
this.sort(function(a, b) {
for (var i = 0, l = fields.length; i != l; i++) {
var field = fields[i];
if (a[field] > b[field]) return 1;
if (a[field] < b[field]) return -1;
}
return 0;
});
return this;
};
})();