Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixes #864 -- add a default iterator to groupBy and countBy

  • Loading branch information...
commit 0a2adcb0d19b42b61e8ab63d3a5dee6ef2bcce48 1 parent c42a3e6
@jashkenas authored
Showing with 11 additions and 1 deletion.
  1. +10 −0 test/collections.js
  2. +1 −1  underscore.js
View
10 test/collections.js
@@ -348,6 +348,11 @@ $(document).ready(function() {
var array = [{}];
_.groupBy(array, function(value, index, obj){ ok(obj === array); });
+
+ var array = [1, 2, 1, 2, 3];
+ var grouped = _.groupBy(array);
+ equal(grouped['1'].length, 2);
+ equal(grouped['3'].length, 1);
});
test('countBy', function() {
@@ -372,6 +377,11 @@ $(document).ready(function() {
var array = [{}];
_.countBy(array, function(value, index, obj){ ok(obj === array); });
+
+ var array = [1, 2, 1, 2, 3];
+ var grouped = _.countBy(array);
+ equal(grouped['1'], 2);
+ equal(grouped['3'], 1);
});
test('sortedIndex', function() {
View
2  underscore.js
@@ -316,7 +316,7 @@
// An internal function used for aggregate "group by" operations.
var group = function(obj, value, context, behavior) {
var result = {};
- var iterator = lookupIterator(value);
+ var iterator = lookupIterator(value || _.identity);
each(obj, function(value, index) {
var key = iterator.call(context, value, index, obj);
behavior(result, key, value);
Please sign in to comment.
Something went wrong with that request. Please try again.