From c99316916c79ab0868d8d65c1635e820065f5642 Mon Sep 17 00:00:00 2001 From: rolux Date: Sun, 26 May 2013 11:50:37 +0200 Subject: [PATCH] add Ox.numberOf: sugar for Ox.len(Ox.filter(collection, test)) --- source/Ox/js/Collection.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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