update Collection.js: for functions, make Ox.len return undefined and Ox.isEmpty return false

This commit is contained in:
rolux 2012-05-19 12:06:49 +04:00
parent 373549a25a
commit e8f7f37a8a

View file

@ -15,35 +15,22 @@ Ox.avg = function(obj) {
return Ox.sum(obj) / Ox.len(obj);
};
/*@
Ox.contains <f> Tests if a collection contains a value
> Ox.contains(['foo', 'bar'], 'foo')
true
> Ox.contains({foo: 'bar'}, 'bar')
true
> Ox.contains({foo: 'bar'}, 'foo')
false
> Ox.contains("foobar", "bar")
true
@*/
Ox.contains = Ox.in = function(col, val) {
/*
// fixme: rename to Ox.has or Ox.in?
// then it'd become convenient for arrays
*/
return (Ox.isObject(col) ? Ox.values(col) : col).indexOf(val) > -1;
Ox.break = function() {
throw Ox.BreakError;
};
Ox.BreakError = new SyntaxError('Illegal My.break() statement');
/*@
Ox.copy <f> Returns a (shallow or deep) copy of an object or array
> (function() { var a = ['v'], b = Ox.copy(a); a[0] = null; return b[0]; }())
Ox.clone <f> Returns a (shallow or deep) copy of an object or array
> (function() { var a = ['v'], b = Ox.clone(a); a[0] = null; return b[0]; }())
'v'
> (function() { var a = {k: 'v'}, b = Ox.copy(a); a.k = null; return b.k; }())
> (function() { var a = {k: 'v'}, b = Ox.clone(a); a.k = null; return b.k; }())
'v'
> Ox.clone(0)
0
@*/
Ox.copy = Ox.clone = function(col, deep) {
Ox.clone = Ox.copy = function(col, deep) {
// fixme: copy or clone?
var ret = Ox.isArray(col) ? [] : {};
if (deep) {
@ -59,6 +46,21 @@ Ox.copy = Ox.clone = function(col, deep) {
return ret;
};
/*@
Ox.contains <f> Tests if a collection contains a value
> Ox.contains(['foo', 'bar'], 'foo')
true
> Ox.contains({foo: 'bar'}, 'bar')
true
> Ox.contains({foo: 'bar'}, 'foo')
false
> Ox.contains("foobar", "bar")
true
@*/
Ox.contains = Ox.in = function(col, val) {
return (Ox.isObject(col) ? Ox.values(col) : col).indexOf(val) > -1;
};
/*@
Ox.count <f> Counts the occurences of values in a collection
> Ox.count(['f', 'o', 'o'])
@ -135,7 +137,7 @@ Ox.find = function(arr, str) {
var ret = [[], []];
str = str.toLowerCase();
arr.map(function(v) {
return v.toLowerCase();
return v.toLowerCase(); // fixme: don't loop twice!!
}).forEach(function(v, i) {
var index = v.indexOf(str);
index > -1 && ret[index == 0 ? 0 : 1][v == str ? 'unshift' : 'push'](arr[i]);
@ -167,6 +169,8 @@ Ox.forEach <f> forEach loop
> Ox.test.string
"012abcfoo"
@*/
// fixme: see http://stackoverflow.com/questions/2641347/javascript-array-foreach-howto-break
// maybe throwing an exception (Ox.break()?) is better than returning false
Ox.forEach = function(col, fn, includePrototype) {
var ind = 0, isObject = Ox.isObject(col), key;
// Safari will not loop through an arguments array
@ -306,7 +310,9 @@ Ox.getset = function(obj, args, callback, context) {
}
/*@
Ox.isEmpty <f> Returns true if a collection is empty
Ox.isEmpty <f> Tests if a value is an empty array, object or string
(value) -> <b> True if the value is an empty array, object or string
value <*> Any value
> Ox.isEmpty([])
true
> Ox.isEmpty({})
@ -314,8 +320,8 @@ Ox.isEmpty <f> Returns true if a collection is empty
> Ox.isEmpty('')
true
> Ox.isEmpty(function() {})
true
> Ox.isEmpty(function(a) {})
false
> Ox.isEmpty(false)
false
> Ox.isEmpty(null)
false
@ -379,25 +385,36 @@ Ox.last = function(arr, val) {
};
/*@
Ox.len <f> Returns the length of an array, function, object or string
Ox.len <f> Returns the length of an array, node list, object or string
Not to be confused with <code>Ox.length</code>, which is the
<code>length</code> property of the <code>Ox</code> function
(<code>1</code>). // FIXME: 1 becomes 67 in DocPanel
> Ox.len((function() { return arguments; }(1, 2, 3)))
3
> Ox.len([1, 2, 3])
3
> Ox.len([,])
1
> Ox.typeOf(Ox.len(document.getElementsByTagName('a')))
'number'
> Ox.len({a: 1, b: 2, c: 3})
3
> Ox.len(function(a, b, c) {})
3
> Ox.len('abc')
3
> Ox.len(function(a, b, c) {})
undefined
@*/
Ox.len = function(col) {
var type = Ox.typeOf(col);
return ['array', 'function', 'string'].indexOf(type) > -1 ? col.length
: type == 'object' ? Ox.values(col).length : void 0;
var len, type = Ox.typeOf(col);
if (
type == 'arguments' || type == 'array'
|| type == 'nodelist' || type == 'string'
) {
len = col.length;
} else if (type == 'object') {
len = Object.keys(col).length;
}
return len;
};
/*@
@ -412,6 +429,7 @@ Ox.makeArray <f> Takes an array-like object and returns a true array
> Ox.makeArray({0: "f", 1: "o", 2: "o", length: 3})
["f", "o", "o"]
@*/
// rewrite this so that it uses a try/catch test
Ox.makeArray = /MSIE/.test(navigator.userAgent)
? function(col) {
var i, len, ret = [];
@ -567,6 +585,7 @@ Ox.shuffle <f> Randomizes the order of values within a collection
> Ox.shuffle('123').split('').sort().join('')
'123'
@*/
// FIXME: this doesn't actually randomize the order
Ox.shuffle = function(col) {
var keys, ret, type = Ox.typeOf(col), values;
function sort() {