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); return Ox.sum(obj) / Ox.len(obj);
}; };
/*@ Ox.break = function() {
Ox.contains <f> Tests if a collection contains a value throw Ox.BreakError;
> 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.BreakError = new SyntaxError('Illegal My.break() statement');
/*@ /*@
Ox.copy <f> Returns a (shallow or deep) copy of an object or array Ox.clone <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]; }()) > (function() { var a = ['v'], b = Ox.clone(a); a[0] = null; return b[0]; }())
'v' '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' 'v'
> Ox.clone(0) > Ox.clone(0)
0 0
@*/ @*/
Ox.copy = Ox.clone = function(col, deep) { Ox.clone = Ox.copy = function(col, deep) {
// fixme: copy or clone? // fixme: copy or clone?
var ret = Ox.isArray(col) ? [] : {}; var ret = Ox.isArray(col) ? [] : {};
if (deep) { if (deep) {
@ -59,6 +46,21 @@ Ox.copy = Ox.clone = function(col, deep) {
return ret; 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> Counts the occurences of values in a collection
> Ox.count(['f', 'o', 'o']) > Ox.count(['f', 'o', 'o'])
@ -135,7 +137,7 @@ Ox.find = function(arr, str) {
var ret = [[], []]; var ret = [[], []];
str = str.toLowerCase(); str = str.toLowerCase();
arr.map(function(v) { arr.map(function(v) {
return v.toLowerCase(); return v.toLowerCase(); // fixme: don't loop twice!!
}).forEach(function(v, i) { }).forEach(function(v, i) {
var index = v.indexOf(str); var index = v.indexOf(str);
index > -1 && ret[index == 0 ? 0 : 1][v == str ? 'unshift' : 'push'](arr[i]); 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 > Ox.test.string
"012abcfoo" "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) { Ox.forEach = function(col, fn, includePrototype) {
var ind = 0, isObject = Ox.isObject(col), key; var ind = 0, isObject = Ox.isObject(col), key;
// Safari will not loop through an arguments array // 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([]) > Ox.isEmpty([])
true true
> Ox.isEmpty({}) > Ox.isEmpty({})
@ -314,8 +320,8 @@ Ox.isEmpty <f> Returns true if a collection is empty
> Ox.isEmpty('') > Ox.isEmpty('')
true true
> Ox.isEmpty(function() {}) > Ox.isEmpty(function() {})
true false
> Ox.isEmpty(function(a) {}) > Ox.isEmpty(false)
false false
> Ox.isEmpty(null) > Ox.isEmpty(null)
false 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 Not to be confused with <code>Ox.length</code>, which is the
<code>length</code> property of the <code>Ox</code> function <code>length</code> property of the <code>Ox</code> function
(<code>1</code>). // FIXME: 1 becomes 67 in DocPanel (<code>1</code>). // FIXME: 1 becomes 67 in DocPanel
> Ox.len((function() { return arguments; }(1, 2, 3)))
3
> Ox.len([1, 2, 3]) > Ox.len([1, 2, 3])
3 3
> Ox.len([,]) > Ox.len([,])
1 1
> Ox.typeOf(Ox.len(document.getElementsByTagName('a')))
'number'
> Ox.len({a: 1, b: 2, c: 3}) > Ox.len({a: 1, b: 2, c: 3})
3 3
> Ox.len(function(a, b, c) {})
3
> Ox.len('abc') > Ox.len('abc')
3 3
> Ox.len(function(a, b, c) {})
undefined
@*/ @*/
Ox.len = function(col) { Ox.len = function(col) {
var type = Ox.typeOf(col); var len, type = Ox.typeOf(col);
return ['array', 'function', 'string'].indexOf(type) > -1 ? col.length if (
: type == 'object' ? Ox.values(col).length : void 0; 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}) > Ox.makeArray({0: "f", 1: "o", 2: "o", length: 3})
["f", "o", "o"] ["f", "o", "o"]
@*/ @*/
// rewrite this so that it uses a try/catch test
Ox.makeArray = /MSIE/.test(navigator.userAgent) Ox.makeArray = /MSIE/.test(navigator.userAgent)
? function(col) { ? function(col) {
var i, len, ret = []; 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('') > Ox.shuffle('123').split('').sort().join('')
'123' '123'
@*/ @*/
// FIXME: this doesn't actually randomize the order
Ox.shuffle = function(col) { Ox.shuffle = function(col) {
var keys, ret, type = Ox.typeOf(col), values; var keys, ret, type = Ox.typeOf(col), values;
function sort() { function sort() {