Skip to content

Commit e81efeb

Browse files
author
Einar Norðfjörð
committed
remove the strange '-' syntax in sortBy, and use d3.ascending as the default sorter allowing users to use their own functions to do the sort
1 parent d2993a6 commit e81efeb

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

reductio.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,26 +1071,24 @@ var pluck_n = function (n) {
10711071
};
10721072
};
10731073

1074+
function ascending(a, b) {
1075+
return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
1076+
}
1077+
10741078
var comparer = function (accessor, ordering) {
10751079
return function (a, b) {
1076-
var aVal = accessor(a);
1077-
var bVal = accessor(b);
1078-
if(aVal > bVal) return ordering == 'asc' ? 1 : -1;
1079-
if(aVal < bVal) return ordering == 'asc' ? -1 : 1;
1080-
return 0;
1080+
return ordering(accessor(a), accessor(b));
10811081
};
10821082
};
10831083

10841084
var type = {}.toString;
10851085

10861086
module.exports = function (prior) {
1087-
return function (value) {
1088-
var ordering = 'asc';
1089-
if(type.call(value) === '[object String]' && value.indexOf('-') === 0){
1090-
value = value.substr(1);
1091-
ordering = 'desc';
1087+
return function (value, order) {
1088+
if (arguments.length === 1) {
1089+
order = ascending;
10921090
}
1093-
return prior().sort(comparer(pluck_n(value), ordering));
1091+
return prior().sort(comparer(pluck_n(value), order));
10941092
};
10951093
};
10961094

reductio.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/sortBy.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,23 @@ var pluck_n = function (n) {
1515
};
1616
};
1717

18+
function ascending(a, b) {
19+
return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
20+
}
21+
1822
var comparer = function (accessor, ordering) {
1923
return function (a, b) {
20-
var aVal = accessor(a);
21-
var bVal = accessor(b);
22-
if(aVal > bVal) return ordering == 'asc' ? 1 : -1;
23-
if(aVal < bVal) return ordering == 'asc' ? -1 : 1;
24-
return 0;
24+
return ordering(accessor(a), accessor(b));
2525
};
2626
};
2727

2828
var type = {}.toString;
2929

3030
module.exports = function (prior) {
31-
return function (value) {
32-
var ordering = 'asc';
33-
if(type.call(value) === '[object String]' && value.indexOf('-') === 0){
34-
value = value.substr(1);
35-
ordering = 'desc';
31+
return function (value, order) {
32+
if (arguments.length === 1) {
33+
order = ascending;
3634
}
37-
return prior().sort(comparer(pluck_n(value), ordering));
35+
return prior().sort(comparer(pluck_n(value), order));
3836
};
3937
};

test/sortBy.spec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
function descending(a, b) {
2+
return a < b ? 1 : a > b ? -1 : a >= b ? 0 : NaN;
3+
}
4+
15
describe('Reductio sortBy', function () {
26
var group, reducer;
37

@@ -28,7 +32,7 @@ describe('Reductio sortBy', function () {
2832
});
2933

3034
it('orders correctly', function () {
31-
var all = group.post().sortBy('-value.sum')();
35+
var all = group.post().sortBy('value.sum', descending)();
3236
for(var i = 0; i < all.length; ++i){
3337
expect(all[i].value.sum).toBe(6-i);
3438
}
@@ -54,7 +58,7 @@ describe('Reductio sortBy', function () {
5458
});
5559

5660
it('works with cap', function(){
57-
var all = group.post().sortBy('-value.sum').cap(3)();
61+
var all = group.post().sortBy('value.sum', descending).cap(3)();
5862
expect(all[0].value.sum).toBe(6);
5963
expect(all[1].value.sum).toBe(5);
6064
expect(all[2].value.sum).toBe(10);

0 commit comments

Comments
 (0)