add Ox.numberOf: sugar for Ox.len(Ox.filter(collection, test))

This commit is contained in:
rolux 2013-05-26 11:50:37 +02:00
parent 4b01e4984b
commit c99316916c

View file

@ -343,6 +343,25 @@ Ox.min = function(collection) {
return ret;
};
/*@
Ox.numberOf <f> Returns the number of elements in a collection that pass a test
(collection, test) -> <n> Number of elements
collection <a|o|s> Collection
test <f> Test function
value <*> Value
key <n|s> Key
collection <a|o|s> 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 <f> Removes an element from an array or object and returns it
(collection, element) -> <*> Element, or undefined if not found