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