mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 18:37:50 +00:00
Adding _.count to count truthy values in an iterator. _.count([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }) = 3
This commit is contained in:
23
index.html
23
index.html
@@ -137,12 +137,13 @@
|
|||||||
<span class="methods"><a href="#each">each</a>, <a href="#map">map</a>,
|
<span class="methods"><a href="#each">each</a>, <a href="#map">map</a>,
|
||||||
<a href="#reduce">reduce</a>, <a href="#reduceRight">reduceRight</a>,
|
<a href="#reduce">reduce</a>, <a href="#reduceRight">reduceRight</a>,
|
||||||
<a href="#detect">detect</a>, <a href="#select">select</a>,
|
<a href="#detect">detect</a>, <a href="#select">select</a>,
|
||||||
<a href="#reject">reject</a>, <a href="#all">all</a>,
|
<a href="#reject">reject</a>, <a href="#count">count</a>,
|
||||||
<a href="#any">any</a>, <a href="#include">include</a>,
|
<a href="#all">all</a>, <a href="#any">any</a>,
|
||||||
<a href="#invoke">invoke</a>, <a href="#pluck">pluck</a>,
|
<a href="#include">include</a>, <a href="#invoke">invoke</a>,
|
||||||
<a href="#max">max</a>, <a href="#min">min</a>,
|
<a href="#pluck">pluck</a>, <a href="#max">max</a>,
|
||||||
<a href="#sortBy">sortBy</a>, <a href="#sortedIndex">sortedIndex</a>,
|
<a href="#min">min</a>, <a href="#sortBy">sortBy</a>,
|
||||||
<a href="#toArray">toArray</a>, <a href="#size">size</a></span>
|
<a href="#sortedIndex">sortedIndex</a>, <a href="#toArray">toArray</a>,
|
||||||
|
<a href="#size">size</a></span>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@@ -337,6 +338,16 @@ var evens = _.select([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
|||||||
<pre>
|
<pre>
|
||||||
var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
||||||
=> [1, 3, 5]
|
=> [1, 3, 5]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p id="count">
|
||||||
|
<b class="header">count</b><code>_.count(list, [iterator], [context])</code>
|
||||||
|
<br />
|
||||||
|
Returns a count of elements which pass the truth test (<b>iterator</b>).
|
||||||
|
</p>
|
||||||
|
<pre>
|
||||||
|
var count = _.count([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
||||||
|
=> 3
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p id="all">
|
<p id="all">
|
||||||
|
|||||||
@@ -122,6 +122,13 @@ $(document).ready(function() {
|
|||||||
equals(odds.join(', '), '1, 3, 5', 'rejected each even number');
|
equals(odds.join(', '), '1, 3, 5', 'rejected each even number');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('collections: count', function() {
|
||||||
|
var count = _.count([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
|
||||||
|
equals(count, 3, 'counted even numbers');
|
||||||
|
var count = _.count([0, 1, 2, 3, 4, 5, 6]);
|
||||||
|
equals(count, 6, 'counted all truthy numbers');
|
||||||
|
});
|
||||||
|
|
||||||
test('collections: all', function() {
|
test('collections: all', function() {
|
||||||
ok(_.all([]), 'the empty set');
|
ok(_.all([]), 'the empty set');
|
||||||
ok(_.all([true, true, true]), 'all true values');
|
ok(_.all([true, true, true]), 'all true values');
|
||||||
|
|||||||
@@ -164,6 +164,16 @@
|
|||||||
return results;
|
return results;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Returns a count of elements which pass a truth test.
|
||||||
|
_.count = function(obj, iterator, context) {
|
||||||
|
var count = 0;
|
||||||
|
iterator = iterator || _.identity;
|
||||||
|
each(obj, function(value, index, list) {
|
||||||
|
if (iterator.call(context, value, index, list)) count += 1;
|
||||||
|
});
|
||||||
|
return count;
|
||||||
|
};
|
||||||
|
|
||||||
// Determine whether all of the elements match a truth test.
|
// Determine whether all of the elements match a truth test.
|
||||||
// Delegates to **ECMAScript 5**'s native `every` if available.
|
// Delegates to **ECMAScript 5**'s native `every` if available.
|
||||||
// Aliased as `all`.
|
// Aliased as `all`.
|
||||||
|
|||||||
Reference in New Issue
Block a user