Skip to content

Commit

Permalink
Adding _.count to count truthy values in an iterator. _.count([1, 2, …
Browse files Browse the repository at this point in the history
…3, 4, 5, 6], function(num){ return num % 2 == 0; }) = 3
  • Loading branch information
samuelclay committed Apr 5, 2011
1 parent a9ac8b1 commit c8e3c04
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
23 changes: 17 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,13 @@ <h2>Table of Contents</h2>
<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="#detect">detect</a>, <a href="#select">select</a>,
<a href="#reject">reject</a>, <a href="#all">all</a>,
<a href="#any">any</a>, <a href="#include">include</a>,
<a href="#invoke">invoke</a>, <a href="#pluck">pluck</a>,
<a href="#max">max</a>, <a href="#min">min</a>,
<a href="#sortBy">sortBy</a>, <a href="#sortedIndex">sortedIndex</a>,
<a href="#toArray">toArray</a>, <a href="#size">size</a></span>
<a href="#reject">reject</a>, <a href="#count">count</a>,
<a href="#all">all</a>, <a href="#any">any</a>,
<a href="#include">include</a>, <a href="#invoke">invoke</a>,
<a href="#pluck">pluck</a>, <a href="#max">max</a>,
<a href="#min">min</a>, <a href="#sortBy">sortBy</a>,
<a href="#sortedIndex">sortedIndex</a>, <a href="#toArray">toArray</a>,
<a href="#size">size</a></span>
</p>

<p>
Expand Down Expand Up @@ -337,6 +338,16 @@ <h2>Collection Functions (Arrays or Objects)</h2>
<pre>
var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
=&gt; [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; });
=&gt; 3
</pre>

<p id="all">
Expand Down
7 changes: 7 additions & 0 deletions test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ $(document).ready(function() {
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() {
ok(_.all([]), 'the empty set');
ok(_.all([true, true, true]), 'all true values');
Expand Down
10 changes: 10 additions & 0 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@
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.
// Delegates to **ECMAScript 5**'s native `every` if available.
// Aliased as `all`.
Expand Down

0 comments on commit c8e3c04

Please sign in to comment.