forked from minj/foxtrick
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy patharray.js
More file actions
296 lines (265 loc) · 7.46 KB
/
array.js
File metadata and controls
296 lines (265 loc) · 7.46 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* array.js
* Array handler functions
* Most of them can be used with array-like structures like node lists.
* @author ryanli, convincedd, LA-MJ
*/
'use strict';
/* eslint-disable */
if (!this.Foxtrick)
var Foxtrick = {};
/* eslint-enable */
/**
* Convert an array-like object to an array
* Suitable for node lists, arrays, strings,
* match() object and the arguments object
* Anything that has a length property and properties 0, 1, 2...
* @template T
* @param {ArrayLike<T>|Iterable<T>} arrayLike
* @return {T[]}
*/
Foxtrick.toArray = function(arrayLike) {
if (typeof arrayLike === 'undefined') {
Foxtrick.log(new Error('No array specified'));
return [];
}
else if (arrayLike === null) {
return [];
}
return Array.from(arrayLike);
};
/**
* Runs func(el, i, array) for each element in an array-like.
* @template T
* @param {function(T, number, T[]):void} func
* @param {ArrayLike<T>|Iterable<T>} array array-like
*/
Foxtrick.forEach = function(func, array) {
Foxtrick.toArray(array).forEach(func);
};
/**
* Returns an array with the values of func(el, i, array)
* for each element el in array-like.
* @template T, V
* @param {function(T, number, T[]):V} func
* @param {ArrayLike<T>|Iterable<T>} array array-like
* @return {V[]}
*/
Foxtrick.map = function(func, array) {
return Foxtrick.toArray(array).map(func);
};
/**
* Returns an array that contains elements in a given array-like
* that satisfy func(el, i, array) == true.
* @template T
* @param {function(T, number, T[]):boolean} func
* @param {ArrayLike<T>|Iterable<T>} array array-like
* @return {T[]}
*/
Foxtrick.filter = function(func, array) {
return Foxtrick.toArray(array).filter(func);
};
/**
* Tests whether at least one element in a given array-like
* satisfies func(el, i, array) == true.
* @template T
* @param {function(T, number, T[]):boolean} func
* @param {ArrayLike<T>|Iterable<T>} array array-like
* @return {boolean}
*/
Foxtrick.any = function(func, array) {
return Foxtrick.toArray(array).some(func);
};
/**
* Tests whether all elements in a given array-like
* satisfy func(el, i, array) == true.
* @template T
* @param {function(T, number, T[]):boolean} func
* @param {ArrayLike<T>|Iterable<T>} array array-like
* @return {boolean}
*/
Foxtrick.all = function(func, array) {
return Foxtrick.toArray(array).every(func);
};
/**
* Tests how many elements in a given array-like
* satisfy func(el, i, array) == true.
* @template T
* @param {function(T, number, T[]):boolean} func
* @param {ArrayLike<T>|Iterable<T>} array array-like
* @return {number}
*/
Foxtrick.count = function(func, array) {
return Foxtrick.toArray(array).reduce(function(ct, e, i, a) {
var newCt = ct;
if (func(e, i, a))
newCt++;
return newCt;
}, 0);
};
/**
* Returns the n-th element in a given array-like
* that satisfies func(el, i, array) == true, or null if not found.
* n is optional and defaults to 0 (first element).
* @template T
* @param {function(T, number, T[]):boolean} func
* @param {ArrayLike<T>|Iterable<T>} array array-like
* @param {number} [n]
* @return {?T} found element or null
*/
Foxtrick.nth = function(func, array, n) {
var count = n || 0;
var ct = 0;
var ret = null;
Foxtrick.any(function(e, i, a) {
// loop until found
if (func(e, i, a)) {
// we have a match
if (ct++ === count) {
// found n-th match: stop
ret = e;
return true;
}
}
// continue
return false;
}, array);
return ret;
};
/**
* Returns the concat of two array-likes a and b.
* Does not modify originals.
* @template T
* @param {ArrayLike<T>|Iterable<T>} a array-like
* @param {ArrayLike<T>|Iterable<T>} b array-like
* @return {T[]} concat
*/
Foxtrick.concat = function(a, b) {
return Foxtrick.toArray(a).concat(Foxtrick.toArray(b));
};
/**
* Returns the unique intersection (===) of two array-likes a and b.
* Does not modify originals.
* @template T
* @param {ArrayLike<T>|Iterable<T>} a array-like
* @param {ArrayLike<T>|Iterable<T>} b array-like
* @return {T[]} intersection
*/
Foxtrick.intersect = function(a, b) {
var r = Foxtrick.filter(function(e) {
return Foxtrick.has(b, e);
}, a);
return Foxtrick.unique(r);
};
/**
* Pushes all elements of array b onto array a.
* Better use native arrays only.
* @template T
* @param {T[]} a array
* @param {ArrayLike<T>|Iterable<T>} b array-like
* @return {number} new length
*/
Foxtrick.push = function(a, b) {
return [].push.apply(a, Foxtrick.toArray(b));
};
/**
* Pushes all elements of array b onto array a if they don't already exist (===) there.
* Better use native arrays only.
* @template T
* @param {T[]} a array
* @param {ArrayLike<T>|Iterable<T>} b array-like
* @return {number} new length
*/
Foxtrick.pushNew = function(a, b) {
// find elements in b but not in a
var newElements = Foxtrick.filter(function(e) {
return !Foxtrick.has(a, e);
}, b);
return Foxtrick.push(a, newElements);
};
/**
* Returns the first index of an element e in an array-like (===), or -1 if not found.
* @template T
* @param {ArrayLike<T>|Iterable<T>} array
* @param {T} e
* @return {number}
*/
Foxtrick.indexOf = function(array, e) {
return Foxtrick.toArray(array).indexOf(e);
};
/**
* Tests whether an element e is in an array-like (===).
* @template T
* @param {ArrayLike<T>|Iterable<T>} array
* @param {T} e
* @return {boolean}
*/
Foxtrick.has = function(array, e) {
return Foxtrick.indexOf(array, e) !== -1;
};
/**
* Returns an array that contains elements in a given array-like
* that are not (!==) element e.
* Does not modify the original array.
* @template T
* @param {ArrayLike<T>|Iterable<T>} array
* @param {T} e element to exclude
* @return {T[]}
*/
Foxtrick.exclude = function(array, e) {
return Foxtrick.filter(function(current) {
return current !== e;
}, array);
};
/**
* Returns an array that contains unique elements in a given array-like.
* Does not modify the original array.
* @template T
* @param {ArrayLike<T>|Iterable<T>} arrayLike array-like
* @return {T[]}
*/
Foxtrick.unique = function(arrayLike) {
return Array.from(new Set(Foxtrick.toArray(arrayLike)));
};
/**
* Zips an arbitrary number of arrays
* @template {any} T
* @template {ArrayLike<T>[]} A
* @param {A} arrayLikes array-likes
* @return {IterableIterator<T[]>}
*/
Foxtrick.zip = (...arrayLikes) => (function*(arrayLikes) {
let len = Math.min(...arrayLikes.map(a => a.length));
for (let i = 0; i < len; i++)
yield arrayLikes.map(a => a[i]);
})(arrayLikes);
/**
* Python's range implementation.
*
* @param {number} start
* @param {number} [limit] optional; becomes (0, start) effectively
* @param {number} [step] optional; defaults to 1
* @return {IterableIterator<number>} iterable of numbers, NOT an array
*/
Foxtrick.range = (start, limit, step = 1) => (function*(start, limit, step) {
let i = Number(start), lim = Number(limit), stp = Number(step);
if (typeof limit == 'undefined') {
lim = i;
i = 0;
}
if (!stp) {
throw new TypeError('Step must be non-zero');
}
else if (stp > 0) {
while (i < lim) {
yield i;
i += stp;
}
}
else {
while (i > lim) {
yield i;
i += stp;
}
}
})(start, limit, step);