Permalink
Browse files

Merge pull request #780 from braddunbar/cleanup

Clean up groupBy/countBy/sortBy.
  • Loading branch information...
2 parents 233b8db + 0a00bd3 commit 7eca0daa87fb9d1bee28b6108f7046abb5f905f0 @jashkenas committed Sep 18, 2012
Showing with 26 additions and 23 deletions.
  1. +6 −0 test/collections.js
  2. +20 −23 underscore.js
View
@@ -280,6 +280,9 @@ $(document).ready(function() {
equal(grouped['3'].join(' '), 'one two six ten');
equal(grouped['4'].join(' '), 'four five nine');
equal(grouped['5'].join(' '), 'three seven eight');
+
+ var context = {};
+ _.groupBy([{}], function(){ ok(this === context); }, context);
});
test('countBy', function() {
@@ -292,6 +295,9 @@ $(document).ready(function() {
equal(grouped['3'], 4);
equal(grouped['4'], 3);
equal(grouped['5'], 3);
+
+ var context = {};
+ _.countBy([{}], function(){ ok(this === context); }, context);
});
test('sortedIndex', function() {
View
@@ -271,58 +271,55 @@
return shuffled;
};
+ // An internal function to generate lookup iterators.
+ var lookupIterator = function(value) {
+ return _.isFunction(value) ? value : function(obj){ return obj[value]; };
+ };
+
// Sort the object's values by a criterion produced by an iterator.
- _.sortBy = function(obj, val, context) {
- var iterator = lookupIterator(obj, val);
+ _.sortBy = function(obj, value, context) {
+ var iterator = lookupIterator(value);
return _.pluck(_.map(obj, function(value, index, list) {
return {
value : value,
index : index,
criteria : iterator.call(context, value, index, list)
};
}).sort(function(left, right) {
- var a = left.criteria, b = right.criteria;
- var ai = left.index, bi = right.index;
+ var a = left.criteria;
+ var b = right.criteria;
if (a !== b) {
- if (a > b || a === void 0) {
- return 1;
- } else if (a < b || b === void 0) {
- return -1;
- }
+ if (a > b || a === void 0) return 1;
+ if (a < b || b === void 0) return -1;
}
- return ai < bi ? -1 : 1;
+ return left.index < right.index ? -1 : 1;
}), 'value');
};
- // An internal function to generate lookup iterators.
- var lookupIterator = function(obj, val) {
- return _.isFunction(val) ? val : function(obj) { return obj[val]; };
- };
-
// An internal function used for aggregate "group by" operations.
- var group = function(obj, val, behavior) {
+ var group = function(obj, value, context, behavior) {
var result = {};
- var iterator = lookupIterator(obj, val);
+ var iterator = lookupIterator(value);
each(obj, function(value, index) {
- var key = iterator(value, index);
+ var key = iterator.call(context, value, index);
behavior(result, key, value);
});
return result;
};
// Groups the object's values by a criterion. Pass either a string attribute
// to group by, or a function that returns the criterion.
- _.groupBy = function(obj, val) {
- return group(obj, val, function(result, key, value) {
+ _.groupBy = function(obj, value, context) {
+ return group(obj, value, context, function(result, key, value) {
(result[key] || (result[key] = [])).push(value);
});
};
// Counts instances of an object that group by a certain criterion. Pass
// either a string attribute to count by, or a function that returns the
// criterion.
- _.countBy = function(obj, val) {
- return group(obj, val, function(result, key, value) {
+ _.countBy = function(obj, value, context) {
+ return group(obj, value, context, function(result, key, value) {
result[key] || (result[key] = 0);
result[key]++;
});
@@ -1160,4 +1157,4 @@
});
-}).call(this);
+}).call(this);

0 comments on commit 7eca0da

Please sign in to comment.