-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFilter.js
More file actions
62 lines (59 loc) · 1.36 KB
/
Filter.js
File metadata and controls
62 lines (59 loc) · 1.36 KB
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
/**
* @class Filter
*
* @author Matteo Burgassi
*/
Refuel.define('Filter',
function Filter() {
var self = this;
var items=[];
this.where = function(clause) {
var item;
var newArray = new Array();
for (var index = 0; index < items.length; index++) {
if (clause(items[index], index)) {
newArray[newArray.length] = items[index];
}
}
items = newArray;
return this;
}
this.select = function(clause) {
var item;
var newArray = new Array();
for (var i = 0; i < items.length; i++) {
if (clause(items[i])) {
newArray[newArray.length] = clause(items[i]);
}
}
items = newArray;
return this;
}
this.orderBy = function(clause) {
var tempArray = new Array();
for (var i = 0; i < items.length; i++) {
tempArray[tempArray.length] = items[i];
}
items = tempArray.sort(function(a, b) {
var x = clause(a);
var y = clause(b);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
return this;
}
this.orderByDescending = function(clause) {
var tempArray = new Array();
for (var i = 0; i < items.length; i++) {
tempArray[tempArray.length] = items[i];
}
items = tempArray.sort(function(a, b) {
var x = clause(b);
var y = clause(a);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
return this;
}
this.returnResult = function() {
return items;
}
})