diff --git a/source/Ox/js/Collection.js b/source/Ox/js/Collection.js index bfcbbd47..6789272a 100644 --- a/source/Ox/js/Collection.js +++ b/source/Ox/js/Collection.js @@ -343,6 +343,25 @@ Ox.min = function(collection) { return ret; }; +/*@ +Ox.numberOf Returns the number of elements in a collection that pass a test + (collection, test) -> Number of elements + collection Collection + test Test function + value <*> Value + key Key + collection Collection + > Ox.numberOf([0, 1, 0, 1], function(v) { return v; }) + 2 + > Ox.numberOf({a: 'a', b: 'c'}, function(v, k) { return v == k; }) + 1 + > Ox.numberOf('foo', function(v, k, c) { return v == c[k - 1]; }) + 1 +@*/ +Ox.numberOf = function(collection, test) { + return Ox.len(Ox.filter(collection, test)); +}; + /*@ Ox.remove Removes an element from an array or object and returns it (collection, element) -> <*> Element, or undefined if not found