| 1 |
function $A(iterable) { |
|---|
| 2 |
if (!iterable) return []; |
|---|
| 3 |
if (iterable.toArray) return iterable.toArray(); |
|---|
| 4 |
else { |
|---|
| 5 |
var results = []; |
|---|
| 6 |
for (var i = 0, length = iterable.length; i < length; i++) |
|---|
| 7 |
results.push(iterable[i]); |
|---|
| 8 |
return results; |
|---|
| 9 |
} |
|---|
| 10 |
} |
|---|
| 11 |
|
|---|
| 12 |
if (Prototype.Browser.WebKit) { |
|---|
| 13 |
function $A(iterable) { |
|---|
| 14 |
if (!iterable) return []; |
|---|
| 15 |
if (!(Object.isFunction(iterable) && iterable == '[object NodeList]') && |
|---|
| 16 |
iterable.toArray) { |
|---|
| 17 |
return iterable.toArray(); |
|---|
| 18 |
} else { |
|---|
| 19 |
var results = []; |
|---|
| 20 |
for (var i = 0, length = iterable.length; i < length; i++) |
|---|
| 21 |
results.push(iterable[i]); |
|---|
| 22 |
return results; |
|---|
| 23 |
} |
|---|
| 24 |
} |
|---|
| 25 |
} |
|---|
| 26 |
|
|---|
| 27 |
Array.from = $A; |
|---|
| 28 |
|
|---|
| 29 |
Object.extend(Array.prototype, Enumerable); |
|---|
| 30 |
|
|---|
| 31 |
if (!Array.prototype._reverse) Array.prototype._reverse = Array.prototype.reverse; |
|---|
| 32 |
|
|---|
| 33 |
Object.extend(Array.prototype, { |
|---|
| 34 |
_each: function(iterator) { |
|---|
| 35 |
for (var i = 0, length = this.length; i < length; i++) |
|---|
| 36 |
iterator(this[i]); |
|---|
| 37 |
}, |
|---|
| 38 |
|
|---|
| 39 |
clear: function() { |
|---|
| 40 |
this.length = 0; |
|---|
| 41 |
return this; |
|---|
| 42 |
}, |
|---|
| 43 |
|
|---|
| 44 |
first: function() { |
|---|
| 45 |
return this[0]; |
|---|
| 46 |
}, |
|---|
| 47 |
|
|---|
| 48 |
last: function() { |
|---|
| 49 |
return this[this.length - 1]; |
|---|
| 50 |
}, |
|---|
| 51 |
|
|---|
| 52 |
compact: function() { |
|---|
| 53 |
return this.select(function(value) { |
|---|
| 54 |
return value != null; |
|---|
| 55 |
}); |
|---|
| 56 |
}, |
|---|
| 57 |
|
|---|
| 58 |
flatten: function() { |
|---|
| 59 |
return this.inject([], function(array, value) { |
|---|
| 60 |
return array.concat(Object.isArray(value) ? |
|---|
| 61 |
value.flatten() : [value]); |
|---|
| 62 |
}); |
|---|
| 63 |
}, |
|---|
| 64 |
|
|---|
| 65 |
without: function() { |
|---|
| 66 |
var values = $A(arguments); |
|---|
| 67 |
return this.select(function(value) { |
|---|
| 68 |
return !values.include(value); |
|---|
| 69 |
}); |
|---|
| 70 |
}, |
|---|
| 71 |
|
|---|
| 72 |
reverse: function(inline) { |
|---|
| 73 |
return (inline !== false ? this : this.toArray())._reverse(); |
|---|
| 74 |
}, |
|---|
| 75 |
|
|---|
| 76 |
reduce: function() { |
|---|
| 77 |
return this.length > 1 ? this : this[0]; |
|---|
| 78 |
}, |
|---|
| 79 |
|
|---|
| 80 |
uniq: function(sorted) { |
|---|
| 81 |
return this.inject([], function(array, value, index) { |
|---|
| 82 |
if (0 == index || (sorted ? array.last() != value : !array.include(value))) |
|---|
| 83 |
array.push(value); |
|---|
| 84 |
return array; |
|---|
| 85 |
}); |
|---|
| 86 |
}, |
|---|
| 87 |
|
|---|
| 88 |
intersect: function(array) { |
|---|
| 89 |
return this.uniq().findAll(function(item) { |
|---|
| 90 |
return array.include(item); |
|---|
| 91 |
}); |
|---|
| 92 |
}, |
|---|
| 93 |
|
|---|
| 94 |
clone: function() { |
|---|
| 95 |
return [].concat(this); |
|---|
| 96 |
}, |
|---|
| 97 |
|
|---|
| 98 |
size: function() { |
|---|
| 99 |
return this.length; |
|---|
| 100 |
}, |
|---|
| 101 |
|
|---|
| 102 |
inspect: function() { |
|---|
| 103 |
return '[' + this.map(Object.inspect).join(', ') + ']'; |
|---|
| 104 |
}, |
|---|
| 105 |
|
|---|
| 106 |
toJSON: function() { |
|---|
| 107 |
var results = []; |
|---|
| 108 |
this.each(function(object) { |
|---|
| 109 |
var value = Object.toJSON(object); |
|---|
| 110 |
if (value !== undefined) results.push(value); |
|---|
| 111 |
}); |
|---|
| 112 |
return '[' + results.join(', ') + ']'; |
|---|
| 113 |
} |
|---|
| 114 |
}); |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
if (Object.isFunction(Array.prototype.forEach)) |
|---|
| 118 |
Array.prototype._each = Array.prototype.forEach; |
|---|
| 119 |
|
|---|
| 120 |
if (!Array.prototype.indexOf) Array.prototype.indexOf = function(item, i) { |
|---|
| 121 |
i || (i = 0); |
|---|
| 122 |
var length = this.length; |
|---|
| 123 |
if (i < 0) i = length + i; |
|---|
| 124 |
for (; i < length; i++) |
|---|
| 125 |
if (this[i] === item) return i; |
|---|
| 126 |
return -1; |
|---|
| 127 |
}; |
|---|
| 128 |
|
|---|
| 129 |
if (!Array.prototype.lastIndexOf) Array.prototype.lastIndexOf = function(item, i) { |
|---|
| 130 |
i = isNaN(i) ? this.length : (i < 0 ? this.length + i : i) + 1; |
|---|
| 131 |
var n = this.slice(0, i).reverse().indexOf(item); |
|---|
| 132 |
return (n < 0) ? n : i - n - 1; |
|---|
| 133 |
}; |
|---|
| 134 |
|
|---|
| 135 |
Array.prototype.toArray = Array.prototype.clone; |
|---|
| 136 |
|
|---|
| 137 |
function $w(string) { |
|---|
| 138 |
string = string.strip(); |
|---|
| 139 |
return string ? string.split(/\s+/) : []; |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
if (Prototype.Browser.Opera){ |
|---|
| 143 |
Array.prototype.concat = function() { |
|---|
| 144 |
var array = []; |
|---|
| 145 |
for (var i = 0, length = this.length; i < length; i++) array.push(this[i]); |
|---|
| 146 |
for (var i = 0, length = arguments.length; i < length; i++) { |
|---|
| 147 |
if (Object.isArray(arguments[i])) { |
|---|
| 148 |
for (var j = 0, arrayLength = arguments[i].length; j < arrayLength; j++) |
|---|
| 149 |
array.push(arguments[i][j]); |
|---|
| 150 |
} else { |
|---|
| 151 |
array.push(arguments[i]); |
|---|
| 152 |
} |
|---|
| 153 |
} |
|---|
| 154 |
return array; |
|---|
| 155 |
}; |
|---|
| 156 |
} |
|---|