/
070-ArrayExtensions.js
319 lines (260 loc) · 8.21 KB
/
070-ArrayExtensions.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//console.debug("begin arrayextensions.js");
/*
Provides array extensions, including JS 1.6 additions.
see: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Working_with_Arrays#Introduced_in_JavaScript_1.6
Static Methods:
* Array.range() - takes a min and a max input, and returns an array of [min (, ..., max)]
* Array.fromAny() - forces an object to an array, uses array-like matching, returns an empty array for null/undefined etc.
Index Methods:
* indexOf() - returns the index of the given item's first occurrence.
* lastIndexOf() - returns the index of the given item's last occurrence.
Iterative methods:
* every() - runs a function on items in the array while that function is returning true. It returns true if the function returns true for every item it could visit.
* filter() - runs a function on every item in the array and returns an array of all items for which the function returns true.
* forEach() - runs a function on every item in the array.
* map() - runs a function on every item in the array and returns the results in an array.
* some() - runs a function on items in the array while that function returns false. It returns true if the function returns true for any item it could visit.
Other methods:
* joinFn(separator, fn) - equivalent to array.map(fn).join(separator) -- used within ART project
*/
/* Static additions to Array */
//returns an array with a range of integers between and including min and max.
Array.range = function(min, max) {
if (isNaN(min)) throw "Argument 'min' is not a number.";
if (isNaN(max)) throw "Argument 'max' is not a number.";
var a = (parseInt(min) <= parseInt(max)) ? min : max;
var b = (a == min) ? max : min;
var ret = [];
for (var i=parseInt(a); i<=parseInt(b) ; i++)
ret.push(i);
return ret;
}
//returns an array based on an array-like object (arguments, getElements* results etc)
//used by 1.6 extensions below
Array.fromAny = function(obj) {
var ret = [];
//sanity checks
if (typeof obj === 'undefined' || x === null || x === '') return []; //nothing to empty array
if (typeof obj === 'array') return obj; //already an array
//test for length property
var len = parseInt(obj && obj.length, 10);
//length is 0
if (len === 0) return [];
//has a .length property, force it to an array
if (len) {
//not a DOM tree
if (!(obj[0] && obj[0].tagName))
return Array.prototype.slice.call(obj);
//DOM tree, walk it
for (var i=0; i<obj.length; i++) {
if (obj[i]) ret[i] = obj[i];
}
return ret;
}
//inspect the object's properties for integer keys
var tmp = 0;
for (var x in obj) {
//don't use null/empty string as 0
if (typeof x === 'undefined' || x === null || x === '') continue;
//property is numeric, and an integer (ceil matches floor)
tmp = Math.floor(x,10);
if ((tmp || tmp === 0) && tmp == Math.ceil(x)) {
ret[tmp] = obj[x];
}
}
return ret;
}
/* Prototype extensions to Array */
//join by delimiter such that f of item requires map (defined below)
Array.prototype.joinFn = function(d, fn) {
return this.map(fn).join(d);
}
/* Add JavaScript 1.6 extensions to Array if needed. */
//indexOf(searchElement[, fromIndex) searches the array for searchElement and returns the index of the first match.
if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this && this[from] === elt)
return from;
}
return -1;
};
}
//lastIndexOf(searchElement[, fromIndex) like indexOf, but starts at the end and searches backwards.
if (!Array.prototype.lastIndexOf)
{
Array.prototype.lastIndexOf = function(elt /*, from*/)
{
var len = this.length;
var from = Number(arguments[1]);
if (isNaN(from))
{
from = len - 1;
}
else
{
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
else if (from >= len)
from = len - 1;
}
for (; from > -1; from--)
{
if (from in this && this[from] === elt)
return from;
}
return -1;
};
}
//every(callback[, thisObject]) returns true if callback returns true for every item in the array.
if (!Array.prototype.every)
{
Array.prototype.every = function(fun /*, thisp*/)
{
var len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1] || this;
for (var i = 0; i < len; i++)
{
if (i in this &&
!fun.call(thisp, this[i], i, this))
return false;
}
return true;
};
}
//filter(callback[, thisObject]) returns a new array containing the items for which callback returned true.
if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun /*, thisp*/)
{
var len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var res = new Array();
var thisp = arguments[1] || this;
for (var i = 0; i < len; i++)
{
if (i in this)
{
var val = this[i]; // in case fun mutates this
if (fun.call(thisp, val, i, this))
res.push(val);
}
}
return res;
};
}
//forEach(callback[, thisObject]) execute callback on every array item.
if (!Array.prototype.forEach)
{
Array.prototype.forEach = function(fun /*, thisp*/)
{
var len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1] || this;
for (var i = 0; i < len; i++)
{
if (i in this)
fun.call(thisp, this[i], i, this);
}
};
}
//map(callback[, thisObject]) returns a new array of the return value from executing callback on every array item.
if (!Array.prototype.map)
{
Array.prototype.map = function(fun /*, thisp*/)
{
var len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1] || this;
for (var i = 0; i < len; i++)
{
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
};
}
//some(callback[, thisObject]) returns true if callback returns true for at least one item in the array.
if (!Array.prototype.some)
{
Array.prototype.some = function(fun /*, thisp*/)
{
var i = 0,
len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1] || this;
for (; i < len; i++)
{
if (
i in this
&& fun.call(thisp, this[i], i, this)
)
return true;
}
return false;
};
}
//static methods for array-like objects
if (!Array.indexOf) {
Array.every = function(obj, elt, from) {
var ary = Array.fromAny(obj) || []
return ary.indexOf(elt, from);
}
}
if (!Array.lastIndexOf) {
Array.lastIndexOf = function(obj, elt, from) {
var ary = Array.fromAny(obj) || [];
return ary.lastIndexOf(elt, from);
}
}
if (!Array.every) {
Array.every = function(obj, fun, thisp) {
var ary = Array.fromAny(obj) || [];
return ary.every(fun, thisp || ary);
}
}
if (!Array.filter) {
Array.filter = function(obj, fun, thisp) {
var ary = Array.fromAny(obj) || [];
return ary.filter(fun, thisp || ary);
}
}
if (!Array.forEach) {
Array.forEach = function(obj, fun, thisp) {
var ary = Array.fromAny(obj) || [];
return ary.forEach(fun, thisp || ary);
}
}
if (!Array.map) {
Array.map = function(obj, fun, thisp) {
var ary = Array.fromAny(obj) || [];
return ary.map(fun, thisp || ary);
}
}
if (!Array.some) {
Array.some = function(obj, fun, thisp) {
var ary = Array.fromAny(obj) || [];
return ary.some(fun, thisp || ary);
}
}
//console.debug("end arrayextensions.js");