2007-04-29 05:37:07 +00:00
|
|
|
function $A(iterable) {
|
2007-01-18 22:24:27 +00:00
|
|
|
if (!iterable) return [];
|
2007-07-09 18:55:58 +00:00
|
|
|
if (iterable.toArray) return iterable.toArray();
|
2007-10-14 11:12:08 +00:00
|
|
|
var length = iterable.length, results = new Array(length);
|
|
|
|
while (length--) results[length] = iterable[length];
|
|
|
|
return results;
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
|
2007-03-11 13:42:40 +00:00
|
|
|
if (Prototype.Browser.WebKit) {
|
2007-04-29 05:37:07 +00:00
|
|
|
function $A(iterable) {
|
2007-03-11 13:42:40 +00:00
|
|
|
if (!iterable) return [];
|
2007-10-14 11:12:08 +00:00
|
|
|
if (!(Object.isFunction(iterable) && iterable == '[object NodeList]') &&
|
|
|
|
iterable.toArray) return iterable.toArray();
|
|
|
|
var length = iterable.length, results = new Array(length);
|
|
|
|
while (length--) results[length] = iterable[length];
|
|
|
|
return results;
|
2007-03-11 13:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-29 05:37:07 +00:00
|
|
|
Array.from = $A;
|
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
Object.extend(Array.prototype, Enumerable);
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
if (!Array.prototype._reverse) Array.prototype._reverse = Array.prototype.reverse;
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
Object.extend(Array.prototype, {
|
|
|
|
_each: function(iterator) {
|
|
|
|
for (var i = 0, length = this.length; i < length; i++)
|
|
|
|
iterator(this[i]);
|
|
|
|
},
|
|
|
|
|
|
|
|
clear: function() {
|
|
|
|
this.length = 0;
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
first: function() {
|
|
|
|
return this[0];
|
|
|
|
},
|
|
|
|
|
|
|
|
last: function() {
|
|
|
|
return this[this.length - 1];
|
|
|
|
},
|
|
|
|
|
|
|
|
compact: function() {
|
|
|
|
return this.select(function(value) {
|
|
|
|
return value != null;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
flatten: function() {
|
|
|
|
return this.inject([], function(array, value) {
|
2007-07-24 18:52:17 +00:00
|
|
|
return array.concat(Object.isArray(value) ?
|
2007-01-18 22:24:27 +00:00
|
|
|
value.flatten() : [value]);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
without: function() {
|
|
|
|
var values = $A(arguments);
|
|
|
|
return this.select(function(value) {
|
|
|
|
return !values.include(value);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
reverse: function(inline) {
|
|
|
|
return (inline !== false ? this : this.toArray())._reverse();
|
|
|
|
},
|
|
|
|
|
|
|
|
reduce: function() {
|
|
|
|
return this.length > 1 ? this : this[0];
|
|
|
|
},
|
|
|
|
|
2007-02-05 05:09:41 +00:00
|
|
|
uniq: function(sorted) {
|
|
|
|
return this.inject([], function(array, value, index) {
|
|
|
|
if (0 == index || (sorted ? array.last() != value : !array.include(value)))
|
|
|
|
array.push(value);
|
|
|
|
return array;
|
2007-01-18 22:24:27 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2007-07-18 21:07:31 +00:00
|
|
|
intersect: function(array) {
|
|
|
|
return this.uniq().findAll(function(item) {
|
2007-08-17 11:06:07 +00:00
|
|
|
return array.detect(function(value) { return item === value });
|
2007-07-18 21:07:31 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
clone: function() {
|
|
|
|
return [].concat(this);
|
|
|
|
},
|
|
|
|
|
|
|
|
size: function() {
|
|
|
|
return this.length;
|
|
|
|
},
|
|
|
|
|
|
|
|
inspect: function() {
|
|
|
|
return '[' + this.map(Object.inspect).join(', ') + ']';
|
2007-03-09 03:23:24 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
toJSON: function() {
|
|
|
|
var results = [];
|
|
|
|
this.each(function(object) {
|
|
|
|
var value = Object.toJSON(object);
|
2007-11-15 21:53:15 +00:00
|
|
|
if (!Object.isUndefined(value)) results.push(value);
|
2007-03-09 03:23:24 +00:00
|
|
|
});
|
2007-04-24 02:50:52 +00:00
|
|
|
return '[' + results.join(', ') + ']';
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
// use native browser JS 1.6 implementation if available
|
2007-08-08 23:19:45 +00:00
|
|
|
if (Object.isFunction(Array.prototype.forEach))
|
2007-07-09 18:55:58 +00:00
|
|
|
Array.prototype._each = Array.prototype.forEach;
|
|
|
|
|
|
|
|
if (!Array.prototype.indexOf) Array.prototype.indexOf = function(item, i) {
|
|
|
|
i || (i = 0);
|
|
|
|
var length = this.length;
|
|
|
|
if (i < 0) i = length + i;
|
|
|
|
for (; i < length; i++)
|
|
|
|
if (this[i] === item) return i;
|
|
|
|
return -1;
|
2007-08-07 19:41:13 +00:00
|
|
|
};
|
2007-07-09 18:55:58 +00:00
|
|
|
|
|
|
|
if (!Array.prototype.lastIndexOf) Array.prototype.lastIndexOf = function(item, i) {
|
|
|
|
i = isNaN(i) ? this.length : (i < 0 ? this.length + i : i) + 1;
|
|
|
|
var n = this.slice(0, i).reverse().indexOf(item);
|
|
|
|
return (n < 0) ? n : i - n - 1;
|
2007-08-07 19:41:13 +00:00
|
|
|
};
|
2007-07-09 18:55:58 +00:00
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
Array.prototype.toArray = Array.prototype.clone;
|
|
|
|
|
2007-03-09 04:23:28 +00:00
|
|
|
function $w(string) {
|
2007-10-15 17:19:16 +00:00
|
|
|
if (!Object.isString(string)) return [];
|
2007-01-18 22:24:27 +00:00
|
|
|
string = string.strip();
|
|
|
|
return string ? string.split(/\s+/) : [];
|
|
|
|
}
|
|
|
|
|
2007-03-09 04:23:28 +00:00
|
|
|
if (Prototype.Browser.Opera){
|
|
|
|
Array.prototype.concat = function() {
|
2007-01-18 22:24:27 +00:00
|
|
|
var array = [];
|
2007-03-09 04:23:28 +00:00
|
|
|
for (var i = 0, length = this.length; i < length; i++) array.push(this[i]);
|
|
|
|
for (var i = 0, length = arguments.length; i < length; i++) {
|
2007-07-24 18:52:17 +00:00
|
|
|
if (Object.isArray(arguments[i])) {
|
2007-03-09 04:23:28 +00:00
|
|
|
for (var j = 0, arrayLength = arguments[i].length; j < arrayLength; j++)
|
2007-01-18 22:24:27 +00:00
|
|
|
array.push(arguments[i][j]);
|
|
|
|
} else {
|
|
|
|
array.push(arguments[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array;
|
2007-08-07 19:41:13 +00:00
|
|
|
};
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|