2007-01-18 22:24:27 +00:00
|
|
|
var $A = Array.from = function(iterable) {
|
|
|
|
if (!iterable) return [];
|
|
|
|
if (iterable.toArray) {
|
|
|
|
return iterable.toArray();
|
|
|
|
} else {
|
|
|
|
var results = [];
|
|
|
|
for (var i = 0, length = iterable.length; i < length; i++)
|
|
|
|
results.push(iterable[i]);
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.extend(Array.prototype, Enumerable);
|
|
|
|
|
|
|
|
if (!Array.prototype._reverse)
|
|
|
|
Array.prototype._reverse = Array.prototype.reverse;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
return array.concat(value && value.constructor == Array ?
|
|
|
|
value.flatten() : [value]);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
without: function() {
|
|
|
|
var values = $A(arguments);
|
|
|
|
return this.select(function(value) {
|
|
|
|
return !values.include(value);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
indexOf: function(object) {
|
|
|
|
for (var i = 0, length = this.length; i < length; i++)
|
|
|
|
if (this[i] == object) return i;
|
|
|
|
return -1;
|
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
clone: function() {
|
|
|
|
return [].concat(this);
|
|
|
|
},
|
|
|
|
|
|
|
|
size: function() {
|
|
|
|
return this.length;
|
|
|
|
},
|
|
|
|
|
|
|
|
inspect: function() {
|
|
|
|
return '[' + this.map(Object.inspect).join(', ') + ']';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Array.prototype.toArray = Array.prototype.clone;
|
|
|
|
|
|
|
|
function $w(string){
|
|
|
|
string = string.strip();
|
|
|
|
return string ? string.split(/\s+/) : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(window.opera){
|
|
|
|
Array.prototype.concat = function(){
|
|
|
|
var array = [];
|
|
|
|
for(var i = 0, length = this.length; i < length; i++) array.push(this[i]);
|
|
|
|
for(var i = 0, length = arguments.length; i < length; i++) {
|
|
|
|
if(arguments[i].constructor == Array) {
|
|
|
|
for(var j = 0, arrayLength = arguments[i].length; j < arrayLength; j++)
|
|
|
|
array.push(arguments[i][j]);
|
|
|
|
} else {
|
|
|
|
array.push(arguments[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
}
|