Skip to content

Commit 1fc1892

Browse files
committed
Use the _.iteratee convention in _.collate (#135)
1 parent 4ae7d7e commit 1fc1892

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

docs/underscore.array.builders.js.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ Signature: `_.collate(toSort:array, customOrder:array)`
112112
//=> ['tall', 'tall', 'medium', 'medium', 'short', 'short']
113113
```
114114

115-
You can provide a third argument to `_.collate` to influence how array elements are evaluated while sorting.
115+
You can provide a third argument to `_.collate` to influence how array elements are evaluated while sorting. This argument will be passed through `_.iteratee()` in order to obtain a function that returns the sorting value.
116116

117-
If the third argument is a string, `_.collate` will look for a property with that name on each array element, and that value will be used for sorting.
117+
For example, if the third argument is a string, `_.collate` will look for a property with that name on each array element, and that value will be used for sorting.
118118

119119
Signature: `_collate(toSort:array, customOrder:array, sortKey:string)`
120120

@@ -125,7 +125,7 @@ Signature: `_collate(toSort:array, customOrder:array, sortKey:string)`
125125
//=> [ {name:'Arnold', height:'tall'}, {name:'Danny', height:'short'} ];
126126
```
127127

128-
You may also pass a function as the third argument; it will be executed once for each item (with the original list as `this`, and the item as its argument), with its return used as the item's sort value.
128+
You may also directly pass a function as the third argument; it will be executed once for each item (with the original list as `this`, and the item as its argument), with its return used as the item's sort value.
129129

130130
Signature: `_collate(toSort:array, customOrder:array, sortKey:function)`
131131

underscore.array.builders.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,27 +189,17 @@
189189

190190
// Returns copy or array sorted according to arbitrary ordering
191191
// order must be an array of values; defines the custom sort
192-
// key must be one of: missing/null, a string, or a function;
192+
// key must be a valid argument to _.iteratee
193193
collate: function(array, order, key) {
194194
if (typeof array.length != "number") throw new TypeError("expected an array-like first argument");
195195
if (typeof order.length != "number") throw new TypeError("expected an array-like second argument");
196196

197197
var original = slice.call(array);
198198
var valA, valB;
199+
var cb = _.iteratee(key);
199200
return sort.call(original, function (a, b) {
200-
if(_.isFunction(key)) {
201-
valA = key.call(original, a);
202-
valB = key.call(original, b);
203-
} else if(existy(key)) {
204-
valA = a[key];
205-
valB = b[key];
206-
} else {
207-
valA = a;
208-
valB = b;
209-
}
210-
211-
var rankA = _.indexOf(order, valA);
212-
var rankB = _.indexOf(order, valB);
201+
var rankA = _.indexOf(order, cb(a));
202+
var rankB = _.indexOf(order, cb(b));
213203

214204
if(rankA === -1) return 1;
215205
if(rankB === -1) return -1;

0 commit comments

Comments
 (0)