This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbetter-array.js
233 lines (225 loc) · 7.2 KB
/
better-array.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
'use strict';
(function(global, code){
if(typeof exports === 'object'){
module.exports = code();
} else{
global.BetterArray = code();
}}(this, function(){
var BetterArray = function BetterArray(array){
var newBetterArray = Object.create(BetterArrayPrototype);
newBetterArray.native = array;
return newBetterArray;
}
var BetterArrayPrototype = {
and: function and(other){
return this.native.filter(function(e){
return other.indexOf(e) >= 0;
});
},
at: function at(index){
if(arguments.length === 1){
return this.native[index];
} else {
return Array.prototype.map.call(arguments, function(i){
return this.native[i];
}.bind(this));
}
},
$clear: function $clear(){
this.native.length = 0;
return this.native;
},
clone: function clone(){
return this.native.slice();
},
compact: function compact(){
return this.native.filter(function(e){
return e != null;
});
},
contains: function contains(element){
return this.native.indexOf(element) >= 0;
},
count: function count(object){
if(arguments.length === 0){
return this.native.length;
} else {
return this.native.filter(function(e){ return e === object }).length;
}
},
$delete: function $delete(indexes){
var indexesToDelete = Array.prototype.slice.call(arguments).sort().reverse();
for(var i = 0; i < indexesToDelete.length; i++){
this.native.splice(indexesToDelete[i], 1);
}
return this.native;
},
doesEvery: function doesEvery(){
return this.native.every.apply(this.native, arguments);
},
doesSome: function doesSome(){
return this.native.some.apply(this.native, arguments);
},
doesNone: function doesNone(fn){
return this.native.every.call(this.native, function(e){ return !fn(e); });
},
each: function each(){
return this.native.forEach.apply(this.native, arguments);
},
filter: function filter(){
return this.native.filter.apply(this.native, arguments);
},
first: function first(number){
if(arguments.length === 0){
return this.native[0];
} else {
return this.native.slice(0, number);
}
},
grep: function grep(matcher){
return this.native.filter(function(e){
return matcher.test(e)
});
},
indexOf: function indexOf(){
return this.native.indexOf.apply(this.native, arguments);
},
$insert: function $insert(index, element){
this.native.splice(index, 0, element);
return this.native;
},
isEmpty: function isEmpty(){
return this.native.length === 0;
},
last: function last(number){
if(arguments.length === 0){
return this.native[this.native.length - 1];
} else {
return this.native.slice(-number, this.native.length);
}
},
lastIndexOf: function lastIndexOf(){
return this.native.lastIndexOf.apply(this.native, arguments);
},
map: function map(){
return this.native.map.apply(this.native, arguments);
},
minus: function minus(other){
return BetterArray(
this.native.filter(function(e){
return other.indexOf(e) < 0;
})
).unique();
},
or: function or(other){
return BetterArray(this.native.concat(other)).unique();
},
plus: function plus(other){
return this.native.concat(other);
},
$pop: function $pop(){
return this.native.pop.apply(this.native, arguments);
},
$push: function $push(){
return this.native.push.apply(this.native, arguments);
},
reduce: function reduce(){
return this.native.reduce.apply(this.native, arguments);
},
reduceRight: function reduceRight(){
return this.native.reduceRight.apply(this.native, arguments);
},
reverse: function reverse(){
var clone = this.native.slice();
return clone.reverse.apply(clone, arguments);
},
$reverse: function $reverse(){
return this.native.reverse.apply(this.native, arguments);
},
rotate: function rotate(count){
if(arguments.length === 0){ count = 1 }
return this.native.slice(count, this.native.length).concat(this.native.slice(0, count));
},
$set: function $set(index, element){
this.native[index] = element;
return this.native;
},
size: function size(){
return this.native.length;
},
$shift: function $shift(){
return this.native.shift.apply(this.native, arguments);
},
slice: function slice(){
return this.native.slice.apply(this.native, arguments);
},
sliceLength: function sliceLength(from, length){
return this.native.slice(from, from + length);
},
sort: function sort(){
var clone = this.native.slice();
return clone.sort.apply(clone, arguments);
},
$sort: function $sort(){
return this.native.sort.apply( this.native, arguments);
},
$splice: function $splice(){
return this.native.splice.apply(this.native, arguments);
},
times: function times(integer){
var res = []
for(var i = 0; i < integer; i++){
res = res.concat(this.native);
}
return res;
},
toArray: function toArray(){
return this.native;
},
unique: function unique(){
return this.native.filter(function(e){
return e in this ? false : this[e] = true;
}, {});
},
$unshift: function $unshift(){
return this.native.unshift.apply(this.native, arguments);
},
withoutFirst: function withoutFirst(number){
if(arguments.length === 0){ number = 1; }
return this.native.slice(number, this.native.length);
},
withoutLast: function withoutLast(number){
if(arguments.length === 0){ number = 1; }
return this.native.slice(0, this.native.length - number);
},
zip: function zip(){
var others = Array.prototype.slice.call(arguments);
return this.native.map(function(e, i) {
var row = others.map(function(f){ return f[i]; });
row.unshift(e);
return row;
});
}
}
// Allow some chaining
var BetterArrayChainPrototype = {};
for(var functionName in BetterArrayPrototype){
(function(){
var fn = BetterArrayPrototype[functionName];
if(functionName === "toArray"){
BetterArrayChainPrototype[functionName] = fn;
} else {
BetterArrayChainPrototype[functionName] = function chain(){
return BetterArray.chain(fn.apply(this, arguments));
}
}
})();
}
BetterArray.chain = function BetterArrayChain(array){
var newBetterArrayChain = Object.create(BetterArrayChainPrototype);
newBetterArrayChain.native = array;
return newBetterArrayChain;
}
return BetterArray;
})
);