From e8f7f37a8a6c24bad44ad926373bc3e6b0b1d6d8 Mon Sep 17 00:00:00 2001 From: rolux Date: Sat, 19 May 2012 12:06:49 +0400 Subject: [PATCH] update Collection.js: for functions, make Ox.len return undefined and Ox.isEmpty return false --- source/Ox/js/Collection.js | 81 +++++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 31 deletions(-) diff --git a/source/Ox/js/Collection.js b/source/Ox/js/Collection.js index d7c4d927..b6053e58 100644 --- a/source/Ox/js/Collection.js +++ b/source/Ox/js/Collection.js @@ -15,35 +15,22 @@ Ox.avg = function(obj) { return Ox.sum(obj) / Ox.len(obj); }; -/*@ -Ox.contains 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 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 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 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 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 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 Returns true if a collection is empty +Ox.isEmpty Tests if a value is an empty array, object or string + (value) -> 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 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 Returns the length of an array, function, object or string +Ox.len Returns the length of an array, node list, object or string Not to be confused with Ox.length, which is the length property of the Ox function (1). // 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 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 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() {