add missing semicolon; make Ox.toArray an alias of Ox.slice, and handle the IE NodeLists case

This commit is contained in:
rolux 2013-11-29 21:15:02 +01:00
parent 5eb1b96e4a
commit d9b25c6fae

View file

@ -40,7 +40,7 @@ Ox.clone = function(collection, deep) {
? Ox.clone(value, true) : value; ? Ox.clone(value, true) : value;
}); });
} else { } else {
ret = type == 'array' ? collection.slice() : Ox.extend({}, collection) ret = type == 'array' ? collection.slice() : Ox.extend({}, collection);
} }
return ret; return ret;
}; };
@ -254,7 +254,7 @@ Ox.len <f> Returns the length of an array, nodelist, object, storage or string
> Ox.len(function(a, b, c) {}) > Ox.len(function(a, b, c) {})
undefined undefined
@*/ @*/
// FIXME: Ox.size() ? // FIXME: rename to Ox.length
Ox.len = function(collection) { Ox.len = function(collection) {
var ret, type = Ox.typeOf(collection); var ret, type = Ox.typeOf(collection);
if ( if (
@ -444,30 +444,52 @@ Ox.shuffle = function(collection) {
/*@ /*@
Ox.slice <f> Alias for `Array.prototype.slice.call` Ox.slice <f> Alias for `Array.prototype.slice.call`
> (function() { return Ox.slice(arguments, 1, -1); }(1, 2, 3)) (collection[, start[, stop]]) -> <a> Array
[2] collection <a|o|s> Array-like
> (function() { return Ox.slice(arguments, 1); }(1, 2, 3)) start <n> Start position
[2, 3] stop <n> Stop position
> (function() { return Ox.slice(arguments); }(1, 2, 3))
[1, 2, 3]
> Ox.slice('foo', 0, 1);
['f']
> Ox.slice({0: 'f', 1: 'o', 2: 'o', length: 3}, -2)
['o', 'o']
@*/ @*/
Ox.slice = function(value, start, stop) { // FIXME: remove toArray alias
return Array.prototype.slice.call(value, start, stop); Ox.slice = Ox.toArray = function(collection, start, stop) {
return Array.prototype.slice.call(collection, start, stop);
}; };
// IE8 returns an empty array if undefined is passed as stop // IE8 returns an empty array if undefined is passed as stop
// and an array of null values if a string is passed as value. // and an array of null values if a string is passed as value.
// Firefox 3.6 returns an array of undefined values // Firefox 3.6 returns an array of undefined values if a string
// if a string is passed as value. // is passed as value. IE NodeLists may not work with slice.
if ( if (
Ox.slice([0]).length == 0 Ox.slice([0]).length == 0
|| Ox.slice('0')[0] === null || Ox.slice('0')[0] === null
|| Ox.slice('0')[0] === void 0 || Ox.slice('0')[0] === void 0
|| !(function() {
try {
return Ox.slice(document.getElementsByTagName('a'));
} catch () {}
}())
) { ) {
Ox.slice = function(value, start, stop) { // FIXME: remove toArray alias
if (Ox.typeOf(value) == 'string') { Ox.slice = Ox.toArray = function(collection, start, stop) {
value = value.split(''); var args = stop === void 0 ? [start] : [start, stop],
array = [], index, length, ret;
if (Ox.typeOf(collection) == 'string') {
collection = collection.split('');
} }
return stop === void 0 try {
? Array.prototype.slice.call(value, start) ret = Array.prototype.slice.apply(collection, args);
: Array.prototype.slice.call(value, start, stop); } catch () {
length = collection.length;
for (index = 0; index < length; index++) {
array[index] = collection[index];
}
ret = Array.prototype.slice.apply(array, args);
}
return ret;
}; };
} }