-
Notifications
You must be signed in to change notification settings - Fork 4
/
Collection.js
85 lines (62 loc) · 1.65 KB
/
Collection.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
(function(define) {
define([], function() {
function array(n) {
return new Array(n);
}
return {
map: function(mapFunc) {
var results, i;
i = 0;
results = array(this.length);
this.forEach(function(item) {
results[i++] = mapFunc(item);
});
return results;
},
filter: function(filterFunc) {
var results, i;
i = 0;
results = array(this.length);
this.forEach(function(item) {
if(filterFunc(item)) {
results[i++] = item;
}
});
return results;
},
reduce: function(reduceFunc, initialValue) {
var result, self, i;
i = 0;
self = this;
result = initialValue;
this.forEach(function(item) {
result = reduceFunc(result, item, i++, self);
});
return result;
},
every: function(matchFunc) {
// unimplemented
},
some: function(matchFunc) {
// unimplemented
},
join: function(separator) {
var str, useSeparator;
str = '';
this.forEach(function(item) {
if(useSeparator) {
str += separator;
} else {
useSeparator = true;
}
str += item;
});
return str;
},
toString: function() {
return '[' + this.join(', ') + ']';
}
}
});
})(typeof define != 'undefined' ? define : function(deps, factory) { module.exports = factory(); }
);