forked from 0x2620/oxjs
swap the meaning of makeArray and toArray: makeArray, like makeObject, is a helper function for arguments processing (that wraps any non-array in an array), toArray, like in other libraries, is an alias for Array.prototype.slice.call
This commit is contained in:
parent
5cabb679f9
commit
5692195509
21 changed files with 88 additions and 88 deletions
|
|
@ -174,7 +174,7 @@ Ox.forEach <f> forEach loop
|
|||
Ox.forEach = function(col, fn, includePrototype) {
|
||||
var ind = 0, isObject = Ox.isObject(col), key;
|
||||
// Safari will not loop through an arguments array
|
||||
col = Ox.isArguments(col) ? Ox.makeArray(col) : col;
|
||||
col = Ox.isArguments(col) ? Ox.toArray(col) : col;
|
||||
for (key in col) {
|
||||
key = isObject ? key : parseInt(key);
|
||||
// fixme: fn.call(context, obj[key], key, obj) may be more standard...
|
||||
|
|
@ -418,35 +418,23 @@ Ox.len = function(col) {
|
|||
};
|
||||
|
||||
/*@
|
||||
Ox.makeArray <f> Takes an array-like object and returns a true array
|
||||
Alias for <code>Array.prototype.slice.call</code>
|
||||
(value) -> <a> True array
|
||||
value <*> Array-like object
|
||||
> (function() { return Ox.makeArray(arguments); }("foo", "bar"))
|
||||
["foo", "bar"]
|
||||
> Ox.makeArray("foo")
|
||||
["f", "o", "o"]
|
||||
> Ox.makeArray({0: "f", 1: "o", 2: "o", length: 3})
|
||||
["f", "o", "o"]
|
||||
Ox.makeArray <f> Wraps any non-array in an array.
|
||||
> Ox.makeArray('foo')
|
||||
['foo']
|
||||
> Ox.makeArray(['foo'])
|
||||
['foo']
|
||||
@*/
|
||||
// rewrite this so that it uses a try/catch test
|
||||
Ox.makeArray = /MSIE/.test(navigator.userAgent)
|
||||
? function(col) {
|
||||
var i, len, ret = [];
|
||||
try {
|
||||
ret = Array.prototype.slice.call(col);
|
||||
} catch(e) {
|
||||
// handle MSIE NodeLists
|
||||
len = col.length;
|
||||
for (i = 0; i < len; i++) {
|
||||
ret[i] = col[i];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
: function(col) {
|
||||
return Array.prototype.slice.call(col);
|
||||
};
|
||||
Ox.makeArray = function(obj) {
|
||||
var arr;
|
||||
if (Ox.isArray(obj)) {
|
||||
arr = obj;
|
||||
} else if (Ox.isArguments(obj)) {
|
||||
arr = Ox.toArray(obj);
|
||||
} else {
|
||||
arr = [obj];
|
||||
}
|
||||
return arr;
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.makeObject <f> Takes an array and returns an object
|
||||
|
|
@ -672,7 +660,7 @@ Ox.sum <f> Returns the sum of the values of a collection
|
|||
@*/
|
||||
Ox.sum = function(col) {
|
||||
var sum = 0;
|
||||
col = arguments.length > 1 ? Ox.makeArray(arguments) : col;
|
||||
col = arguments.length > 1 ? Ox.toArray(arguments) : col;
|
||||
Ox.forEach(col, function(val) {
|
||||
val = +val;
|
||||
sum += Ox.isNumber(val) ? val : 0;
|
||||
|
|
@ -681,23 +669,35 @@ Ox.sum = function(col) {
|
|||
};
|
||||
|
||||
/*@
|
||||
Ox.toArray <f> Wraps any non-array in an array.
|
||||
> Ox.toArray('foo')
|
||||
['foo']
|
||||
> Ox.toArray(['foo'])
|
||||
['foo']
|
||||
Ox.toArray <f> Takes an array-like object and returns a true array
|
||||
Alias for <code>Array.prototype.slice.call</code>
|
||||
(value) -> <a> True array
|
||||
value <*> Array-like object
|
||||
> (function() { return Ox.toArray(arguments); }("foo", "bar"))
|
||||
["foo", "bar"]
|
||||
> Ox.toArray("foo")
|
||||
["f", "o", "o"]
|
||||
> Ox.toArray({0: "f", 1: "o", 2: "o", length: 3})
|
||||
["f", "o", "o"]
|
||||
@*/
|
||||
Ox.toArray = function(obj) {
|
||||
var arr;
|
||||
if (Ox.isArray(obj)) {
|
||||
arr = obj;
|
||||
} else if (Ox.isArguments(obj)) {
|
||||
arr = Ox.makeArray(obj);
|
||||
} else {
|
||||
arr = [obj];
|
||||
}
|
||||
return arr;
|
||||
};
|
||||
// rewrite this so that it uses a try/catch test
|
||||
Ox.toArray = /MSIE/.test(navigator.userAgent)
|
||||
? function(col) {
|
||||
var i, len, ret = [];
|
||||
try {
|
||||
ret = Array.prototype.slice.call(col);
|
||||
} catch(e) {
|
||||
// handle MSIE NodeLists
|
||||
len = col.length;
|
||||
for (i = 0; i < len; i++) {
|
||||
ret[i] = col[i];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
: function(col) {
|
||||
return Array.prototype.slice.call(col);
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.values <f> Returns the values of a collection
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue