Skip to content
This repository has been archived by the owner on Nov 11, 2018. It is now read-only.

Commit

Permalink
Change nest.rollup to return entry.value.
Browse files Browse the repository at this point in the history
Fixes #3.
  • Loading branch information
mbostock committed May 15, 2016
1 parent 253e095 commit d071a88
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
21 changes: 7 additions & 14 deletions src/nest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export default function() {
nest;

function apply(array, depth, createResult, setResult) {
if (depth >= keys.length) return rollup
? rollup(array) : (sortValues
if (depth >= keys.length) return rollup != null
? rollup(array) : (sortValues != null
? array.sort(sortValues)
: array);

Expand Down Expand Up @@ -38,18 +38,11 @@ export default function() {
}

function entries(map, depth) {
if (depth >= keys.length) return map;

var array = [],
sortKey = sortKeys[depth++];

map.each(function(value, key) {
array.push({key: key, values: entries(value, depth)});
});

return sortKey
? array.sort(function(a, b) { return sortKey(a.key, b.key); })
: array;
if (++depth > keys.length) return map;
var array, sortKey = sortKeys[depth - 1];
if (rollup != null && depth >= keys.length) array = map.entries();
else array = [], map.each(function(v, k) { array.push({key: k, values: entries(v, depth)}); });
return sortKey != null ? array.sort(function(a, b) { return sortKey(a.key, b.key); }) : array;
}

return nest = {
Expand Down
2 changes: 1 addition & 1 deletion test/nest-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ tape("nest.key(key).rollup(rollup).entries(array) aggregates values per key usin
var a = {foo: 1},
b = {foo: 1},
c = {foo: 2};
test.deepEqual(collection.nest().key(function(d) { return d.foo; }).rollup(function(values) { return values.length; }).entries([a, b, c]), [{key: "1", values: 2}, {key: "2", values: 1}]);
test.deepEqual(collection.nest().key(function(d) { return d.foo; }).rollup(function(values) { return values.length; }).entries([a, b, c]), [{key: "1", value: 2}, {key: "2", value: 1}]);
test.end();
});

Expand Down

0 comments on commit d071a88

Please sign in to comment.