-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcollections.js
More file actions
45 lines (41 loc) · 849 Bytes
/
collections.js
File metadata and controls
45 lines (41 loc) · 849 Bytes
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
var sort = function(list, keyFunction) {
list.sort(function(a, b) {
a = keyFunction(a);
b = keyFunction(b);
if (a == b)
return 0;
if (a < b)
return -1;
return 1;
});
return list;
};
var each = function(list, eachFunction) {
for (var index in list) {
eachFunction(index, list[index]);
}
}
var select = function(list, selectFunction) {
var ret = [];
each(list, function(index, value) {
ret.push(selectFunction(index, value));
});
return ret;
}
var filter = function(list, selectFunction) {
var ret = [];
each(list, function(index, value) {
var selected = selectFunction(index, value);
if (selected != null)
ret.push(selected);
});
return ret;
}
try {
exports.sort = sort;
exports.select = select;
exports.filter = filter;
exports.each = each;
}
catch (e) {
}