Repository navigation
Expand file tree
/
Copy pathArrayExtensions.js
More file actions
138 lines (117 loc) · 2.78 KB
/
Copy pathArrayExtensions.js
File metadata and controls
138 lines (117 loc) · 2.78 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'use strict';
Array.prototype.evaluateSpec = function(element, spec){
if(spec instanceof Function){
return spec.call(this, element);
}else{
return element == spec;
}
};
Array.prototype.isEmpty = function(){
return !this.length;
};
Array.prototype.each = function(callback){
for(var i =0,length = this.length; i< length; i++){
callback.call(this, this[i], i);
}
};
Array.prototype.where = function(spec){
if(spec === undefined){
return this;
}
var filteredArray = [];
this.each(function(element, index){
if(spec.call(this, element)){
filteredArray.push(element);
}
});
return filteredArray;
};
Array.prototype.any = function(spec){
for(var i =0,length = this.length; i < length; i++){
if(this.evaluateSpec(this[i], spec)){
return true;
}
};
return false;
};
Array.prototype.select = function(spec){
var selectArray = [];
this.each(function(element, index){
selectArray.push(spec.call(this, element));
});
return selectArray;
};
Array.prototype.take = function(howMany, spec){
if(spec){
return this.where(spec).slice(0,howMany);
}else{
return this.slice(0, howMany);
}
};
Array.prototype.skip = function(howMany){
return this.slice(howMany);
};
Array.prototype.first = function(spec){
var result = this.where(spec);
return result.isEmpty() ? null : result[0];
};
Array.prototype.last = function(spec){
var results = this.where(spec);
return results.isEmpty() ? null : results[results.length - 1];
};
Array.prototype.count = function(spec){
return this.where(spec).length;
};
Array.prototype.index = function(spec){
for(var i=0, length = this.length; i < length; i++){
if(this.evaluateSpec(this[i], spec)){
return i;
}
}
return -1;
};
Array.prototype.pluck = function(property){
var properties = [];
this.each(function(element, index){
properties.push(element[property]);
});
return properties;
};
Array.prototype.sum = function(spec){
if(this.isEmpty()){
return null;
}
var sum = spec ? spec.call(this, this[0]) : this[0];
for(var i = 1, length = this.length; i < length; i++){
sum += spec ? spec.call(this, this[i]) : this[i];
}
return sum;
};
Array.prototype.max = function(comparer){
if(this.isEmpty()){
return null;
}
comparer = comparer || function(a, b){return a - b};
return this.sort(comparer)[this.length -1];
};
Array.prototype.min = function(comparer){
if(this.isEmpty()){
return null;
}
comparer = comparer || function(a, b){return a - b};
return this.sort(comparer)[0];
};
Array.prototype.flatten = function(){
function addArrayElement(newArray, element){
if(Array.isArray(element)){
element.each(function(el, idx){
addArrayElement(newArray, el);
})
}else{
newArray.push(element);
}
};
var newArray = [];
addArrayElement(newArray, this);
return newArray.isEmpty() ? null : newArray;
};