/
array.js
410 lines (377 loc) · 13 KB
/
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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Copyright 2010 Jive Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Invokes `fun` on each element of the array in turn.
*
* The first argument given to `fun` is a single array element and the second
* argument is the index of that element in the array.
*
* This definition is compatible with the JavaScript 1.6 definition for
* `Array#forEach` in Spidermonkey.
*
* This implementation comes from:
* https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/forEach
*
* @function
* @param {Function} fun function that will be applied to each array
* element
* @param {Object} [thisp] context in which `fun` will be invoked - `this`
* in `fun` will refer to `thisp`
*/
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];
for (var i = 0; i < len; i++) {
if (i in this) {
fun.call(thisp, this[i], i, this);
}
}
};
/**
* Invokes `fun` on each element of the array and returns a new array of the
* results of each application.
*
* The first argument to `fun` is a single array
* element and the second argument is the index of that element in the array.
*
* This definition is compatible with the JavaScript 1.6 definition for
* `Array#map` in Spidermonkey and with the definition in the Prototype library.
*
* This implementation comes from:
* https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/map
*
* @function
* @param {Function} fun function that will be applied to each array element
* @param {Object} [thisp] context in which `fun` will be invoked - `this`
* in `fun` will refer to `thisp`
* @returns {Array} a new array made up of the return values of every invocation of `fun`
*/
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];
for (var i = 0; i < len; i++) {
if (i in this) {
res[i] = fun.call(thisp, this[i], i);
}
}
return res;
};
/**
* Applies `fun` to `initial` and the first element of the array, and then to the
* result and the second element, and so on. Returns the result of applying
* `fun` to the accumulated value and the last element of the array.
*
* `fun` is given four arguments:
* <ol>
* <li>the result of the previous invocation of `fun`, or the initial value on the first invocation</li>
* <li>a single array element</li>
* <li>the index of that element in the array</li>
* <li>the original array</li>
* </ol>
*
* The 'reduce' algorithm is also known as 'fold' and 'inject'.
*
* This definition is *not* compatible with the definitions of `Array#reduce`
* or `Array#inject` in the Prototype library. However it is compatible with
* the JavaScript 1.6 definition of `Array#reduce` in Spidermonkey.
*
* This implementation comes from:
* https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Array/Reduce
*
* @function
* @param {Function} fun function that will be applied to each array element
* @param {any} [initial] initial value; defaults to the first array element
* @returns {any} the return value from the last invocation of `fun`
*/
Array.prototype.reduce = Array.prototype.reduce || function(fun /*, initial*/) {
var len = this.length >>> 0;
if (typeof fun != "function") {
throw new TypeError();
}
// no value to return if no initial value and an empty array
if (len == 0 && arguments.length == 1) {
throw new TypeError();
}
var i = 0;
if (arguments.length >= 2) {
var rv = arguments[1];
} else {
do {
if (i in this) {
rv = this[i++];
break;
}
// if array contains no values, no initial value to return
if (++i >= len) {
throw new TypeError();
}
} while (true);
}
for (; i < len; i++) {
if (i in this) {
rv = fun.call(null, rv, this[i], i, this);
}
}
return rv;
};
/**
* Applies `fun` to `initial` and the last element of the array, and then to
* the result and the second-to-last element, and so on. Returns the result of
* applying `fun` to the accumulated value and the first element of the array.
*
* `fun` is given four arguments:
* <ol>
* <li>the result of the previous invocation of `fun`, or the initial value on the first invocation</li>
* <li>a single array element</li>
* <li>the index of that element in the array</li>
* <li>the original array</li>
* </ol>
*
* This method behaves identically to Array#reduce except that it performs a
* right-reduce instead of a left-reduce.
*
* This definition is compatible with the JavaScript 1.6 definition of
* `Array#reduceRight` in Spidermonkey.
*
* This implementation comes from:
* https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Array/Reduce
*
* @function
* @param {Function} fun function that will be applied to each array element
* @param {any} [initial] initial value; defaults to the last array element
* @returns {any} the return value from the last invocation of `fun`
*/
Array.prototype.reduceRight = function(fun /*, initial*/) {
var len = this.length >>> 0;
if (typeof fun != "function") {
throw new TypeError();
}
// no value to return if no initial value, empty array
if (len == 0 && arguments.length == 1) {
throw new TypeError();
}
var i = len - 1;
if (arguments.length >= 2) {
var rv = arguments[1];
} else {
do {
if (i in this) {
var rv = this[i--];
break;
}
// if array contains no values, no initial value to return
if (--i < 0) {
throw new TypeError();
}
} while (true);
}
for (; i >= 0; i--) {
if (i in this) {
rv = fun.call(null, rv, this[i], i, this);
}
}
return rv;
};
/**
* Applies `fun` to each element of the array and returns a new array of all
* the values for which `fun` returned `true`.
*
* The first argument given to `fun` is a single array element and the second
* argument is the index of that element in the array.
*
* This definition is compatible with the JavaScript 1.6 definition for
* `Array#filter` in Spidermonkey and with the definition in the Prototype library.
*
* This implementation comes from:
* https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/filter
*
* @function
* @param {Function} fun predicate function that will be applied to each
* array element
* @param {Object} [thisp] context in which `fun` will be invoked - `this`
* in `fun` will refer to `thisp`
* @returns {Array} a new array containing only the elements for which `fun` return true
*/
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];
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)) {
res.push(val);
}
}
}
return res;
};
/**
* Invokes `fun` on each element of the array and returns true if every
* invocation of `fun` returns true or returns a truthy value. Otherwise
* returns false.
*
* The first argument given to `fun` is a single array element and the second
* argument is the index of that element in the array.
*
* This definition is compatible with the JavaScript 1.6 definition for
* `Array#every` in Spidermonkey.
*
* This implementation comes from:
* https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/every
*
* @function
* @param {Function} fun predicate function that will be applied to each
* array element
* @param {Object} [thisp] context in which `fun` will be invoked - `this`
* in `fun` will refer to `thisp`
* @returns {Boolean} true if `fun` returned true for every array element, false otherwise
*/
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];
for (var i = 0; i < len; i++) {
if (i in this &&
!fun.call(thisp, this[i], i, this)) {
return false;
}
}
return true;
};
/**
* Invokes `fun` on each element of the array and returns true if at least one
* invocation of `fun` returns true or returns a truthy value. Otherwise
* returns false.
*
* The first argument given to `fun` is a single array element and the second
* argument is the index of that element in the array.
*
* This definition is compatible with the JavaScript 1.6 definition for
* `Array#every` in Spidermonkey.
*
* This implementation comes from:
* https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/some
*
* @function
* @param {Function} fun predicate function that will be applied to each
* array element
* @param {Object} [thisp] context in which `fun` will be invoked - `this`
* in `fun` will refer to `thisp`
* @returns {Boolean} true if `fun` returned true for some array element, false otherwise
*/
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];
for (; i < len; i++) {
if (i in this &&
fun.call(thisp, this[i], i, this)) {
return true;
}
}
return false;
};
/**
* Compares elements in the array with `searchElement` using strict equality
* (===). If any element matches `searchElement` the lowest matching index is
* returned. Otherwise -1 is returned.
*
* You can optionally restrict the search by passing a `fromIndex` argument.
*
* This definition is compatible with the JavaScript 1.6 definition for
* `Array#indexOf` in Spidermonkey.
*
* This implementation comes from:
* https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf
*
* @function
* @param {any} searchElement element to search for within the array
* @param {number} [fromIndex] index at which to begin search
* @returns {number} the index of the matching element if one is found, -1 otherwise
*/
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;
};
/**
* Compares elements in the array with `searchElement` using strict equality
* (===). If any element matches `searchElement` the highest matching index is
* returned. Otherwise -1 is returned.
*
* You can optionally restrict the search by passing a `fromIndex` argument.
*
* This definition is compatible with the JavaScript 1.6 definition for
* `Array#indexOf` in Spidermonkey.
*
* This implementation comes from:
* https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf
*
* @function
* @param {any} searchElement element to search for within the array
* @param {number} [fromIndex] index at which to begin search
* @returns {number} the index of the matching element if one is found, -1 otherwise
*/
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;
};