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-12-20 15:47:32 +00:00
|
|
|
var length = iterable.length || 0, results = new Array(length);
|
2007-10-14 11:12:08 +00:00
|
|
|
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) {
|
2008-01-22 23:36:47 +00:00
|
|
|
$A = function(iterable) {
|
2008-07-17 20:31:28 +00:00
|
|
|
if (!iterable) return [];
|
|
|
|
// In Safari, only use the `toArray` method if it's not a NodeList.
|
|
|
|
// A NodeList is a function, has an function `item` property, and a numeric
|
|
|
|
// `length` property. Adapted from Google Doctype.
|
|
|
|
if (!(typeof iterable === 'function' && typeof iterable.length ===
|
|
|
|
'number' && typeof iterable.item === 'function') && iterable.toArray)
|
|
|
|
return iterable.toArray();
|
2007-12-20 15:47:32 +00:00
|
|
|
var length = iterable.length || 0, results = new Array(length);
|
2007-10-14 11:12:08 +00:00
|
|
|
while (length--) results[length] = iterable[length];
|
|
|
|
return results;
|
2008-01-22 23:36:47 +00:00
|
|
|
};
|
2007-03-11 13:42:40 +00:00
|
|
|
}
|
|
|
|
|
2008-12-11 10:31:36 +00:00
|
|
|
function $w(string) {
|
|
|
|
if (!Object.isString(string)) return [];
|
|
|
|
string = string.strip();
|
|
|
|
return string ? string.split(/\s+/) : [];
|
|
|
|
}
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-12-11 10:31:36 +00:00
|
|
|
Array.from = $A;
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-12-11 10:31:36 +00:00
|
|
|
(function() {
|
|
|
|
var arrayProto = Array.prototype,
|
|
|
|
slice = arrayProto.slice,
|
|
|
|
_each = arrayProto.forEach; // use native browser JS 1.6 implementation if available
|
|
|
|
|
|
|
|
function each(iterator) {
|
2007-01-18 22:24:27 +00:00
|
|
|
for (var i = 0, length = this.length; i < length; i++)
|
|
|
|
iterator(this[i]);
|
2008-12-11 10:31:36 +00:00
|
|
|
}
|
|
|
|
if (!_each) _each = each;
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-12-11 10:31:36 +00:00
|
|
|
function clear() {
|
2007-01-18 22:24:27 +00:00
|
|
|
this.length = 0;
|
|
|
|
return this;
|
2008-12-11 10:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function first() {
|
2007-01-18 22:24:27 +00:00
|
|
|
return this[0];
|
2008-12-11 10:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function last() {
|
2007-01-18 22:24:27 +00:00
|
|
|
return this[this.length - 1];
|
2008-12-11 10:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function compact() {
|
2007-01-18 22:24:27 +00:00
|
|
|
return this.select(function(value) {
|
|
|
|
return value != null;
|
|
|
|
});
|
2008-12-11 10:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function flatten() {
|
2007-01-18 22:24:27 +00:00
|
|
|
return this.inject([], function(array, value) {
|
2008-12-11 10:31:36 +00:00
|
|
|
if (Object.isArray(value))
|
|
|
|
return array.concat(value.flatten());
|
|
|
|
array.push(value);
|
|
|
|
return array;
|
2007-01-18 22:24:27 +00:00
|
|
|
});
|
2008-12-11 10:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function without() {
|
|
|
|
var values = slice.call(arguments, 0);
|
2007-01-18 22:24:27 +00:00
|
|
|
return this.select(function(value) {
|
|
|
|
return !values.include(value);
|
|
|
|
});
|
2008-12-11 10:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function reverse(inline) {
|
2007-01-18 22:24:27 +00:00
|
|
|
return (inline !== false ? this : this.toArray())._reverse();
|
2008-12-11 10:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function reduce() {
|
2007-01-18 22:24:27 +00:00
|
|
|
return this.length > 1 ? this : this[0];
|
2008-12-11 10:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function uniq(sorted) {
|
2007-02-05 05:09:41 +00:00
|
|
|
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
|
|
|
});
|
2008-12-11 10:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function intersect(array) {
|
2007-07-18 21:07:31 +00:00
|
|
|
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
|
|
|
});
|
2008-12-11 10:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function clone() {
|
|
|
|
return slice.call(this, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function size() {
|
2007-01-18 22:24:27 +00:00
|
|
|
return this.length;
|
2008-12-11 10:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function inspect() {
|
2007-01-18 22:24:27 +00:00
|
|
|
return '[' + this.map(Object.inspect).join(', ') + ']';
|
2008-12-11 10:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function toJSON() {
|
2007-03-09 03:23:24 +00:00
|
|
|
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
|
|
|
}
|
2008-12-11 10:31:36 +00:00
|
|
|
|
|
|
|
function indexOf(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function lastIndexOf(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function concat() {
|
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;
|
2008-12-11 10:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Object.extend(arrayProto, Enumerable);
|
|
|
|
|
|
|
|
if (!arrayProto._reverse)
|
|
|
|
arrayProto._reverse = arrayProto.reverse;
|
|
|
|
|
|
|
|
Object.extend(arrayProto, {
|
|
|
|
_each: _each,
|
|
|
|
clear: clear,
|
|
|
|
first: first,
|
|
|
|
last: last,
|
|
|
|
compact: compact,
|
|
|
|
flatten: flatten,
|
|
|
|
without: without,
|
|
|
|
reverse: reverse,
|
|
|
|
reduce: reduce,
|
|
|
|
uniq: uniq,
|
|
|
|
intersect: intersect,
|
|
|
|
clone: clone,
|
|
|
|
toArray: clone,
|
|
|
|
size: size,
|
|
|
|
inspect: inspect,
|
|
|
|
toJSON: toJSON
|
|
|
|
});
|
|
|
|
|
|
|
|
// use native browser JS 1.6 implementation if available
|
|
|
|
if (!'indexOf' in arrayProto) arrayProto.indexOf = indexOf;
|
|
|
|
if (!'lastIndexOf' in arrayProto) arrayProto.lastIndexOf = lastIndexOf;
|
|
|
|
if (!'concat' in arrayProto) arrayProto.concat = concat;
|
|
|
|
})();
|