|
|
|
@ -1,65 +1,76 @@
|
|
|
|
|
/* Array utilities. */
|
|
|
|
|
var arrays = {
|
|
|
|
|
/* Like Python's |range|, but without |step|. */
|
|
|
|
|
range: function(start, stop) {
|
|
|
|
|
if (stop === undefined) {
|
|
|
|
|
stop = start;
|
|
|
|
|
start = 0;
|
|
|
|
|
}
|
|
|
|
|
var length = stop - start,
|
|
|
|
|
result = new Array(length),
|
|
|
|
|
i;
|
|
|
|
|
|
|
|
|
|
var result = new Array(Math.max(0, stop - start));
|
|
|
|
|
for (var i = 0, j = start; j < stop; i++, j++) {
|
|
|
|
|
for (i = 0, j = start; i < length; i++, j++) {
|
|
|
|
|
result[i] = j;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
find: function(array, callback) {
|
|
|
|
|
var length = array.length;
|
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
|
if (callback(array[i])) {
|
|
|
|
|
return array[i];
|
|
|
|
|
find: function(array, valueOrPredicate) {
|
|
|
|
|
var length = array.length, i;
|
|
|
|
|
|
|
|
|
|
if (typeof valueOrPredicate === "function") {
|
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
|
if (valueOrPredicate(array[i])) {
|
|
|
|
|
return array[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
|
if (array[i] === valueOrPredicate) {
|
|
|
|
|
return array[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
indexOf: function(array, callback) {
|
|
|
|
|
var length = array.length;
|
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
|
if (callback(array[i])) {
|
|
|
|
|
return i;
|
|
|
|
|
indexOf: function(array, valueOrPredicate) {
|
|
|
|
|
var length = array.length, i;
|
|
|
|
|
|
|
|
|
|
if (typeof valueOrPredicate === "function") {
|
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
|
if (valueOrPredicate(array[i])) {
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
|
if (array[i] === valueOrPredicate) {
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
contains: function(array, value) {
|
|
|
|
|
/*
|
|
|
|
|
* Stupid IE does not have Array.prototype.indexOf, otherwise this function
|
|
|
|
|
* would be a one-liner.
|
|
|
|
|
*/
|
|
|
|
|
var length = array.length;
|
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
|
if (array[i] === value) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
contains: function(array, valueOrPredicate) {
|
|
|
|
|
return arrays.indexOf(array, valueOrPredicate) !== -1;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
each: function(array, callback) {
|
|
|
|
|
var length = array.length;
|
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
|
callback(array[i], i);
|
|
|
|
|
each: function(array, iterator) {
|
|
|
|
|
var length = array.length, i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
|
iterator(array[i], i);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
map: function(array, callback) {
|
|
|
|
|
var result = [];
|
|
|
|
|
var length = array.length;
|
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
|
result[i] = callback(array[i], i);
|
|
|
|
|
map: function(array, iterator) {
|
|
|
|
|
var length = array.length,
|
|
|
|
|
result = new Array(length),
|
|
|
|
|
i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
|
result[i] = iterator(array[i], i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|