This repository has been archived by the owner on Jun 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.fp.js
78 lines (78 loc) · 2.65 KB
/
jquery.fp.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
(function(jQuery) {
jQuery.fn.extend({
getOrElse: function(defaultValue) {
var value = this.get(0);
return value==document ? defaultValue : value;
},
foldLeft: function(value, func) {
for(var index in this.get()) {
value = func.call(this, value, this.get(index));
}
return value;
},
foldRight: function(value, func){
for(var item in this.get()) {
var index = this.size()-1-item;
value = func.call(this, value, this.get(index));
}
return value;
},
reverse: [].reverse,
head: function() { return this.get(0); },
tail: function() { return this.slice(1); },
exists: function(func) { return this.filter(func).size() > 0; },
forall: function(func) { return this.filter(func).size() == this.size(); },
take: function(count) { return this.slice(0, count).get(); },
drop: function(first) { return this.slice(first).get(); },
takeWhile: function(func) {
var ret = [];
for(var index in this.get()) {
var item = this.get(index);
if (!func.call(item))return ret;
ret.push(item);
}
return ret;
},
dropWhile: function(func) {
var ret = [];
var matched = false;
for(var index in this.get()) {
var item = this.get(index);
if (!matched && func.call(item)) {
matched = true;
}
if(matched) {
ret.push(item);
}
}
return ret;
},
groupBy: function(func) {
var ret = {};
for(var index in this.get()) {
var item = this.get(index);
var group = func.call(item);
var elems = ret[group];
if (elems == null) {
ret[group] = [item];
} else {
ret[group].push(item);
}
}
return ret;
},
partition: function(pred) {
var satisfied = [];
var nonSatisfied = [];
for(var index in this.get()) {
var item = this.get(index);
if (pred.call(item)) {
satisfied.push(item);
} else {
nonSatisfied.push(item);
}
}
return { "true": satisfied, "false": nonSatisfied };
}
});
})(jQuery);