3
var vows = require("vows"),
4
assert = require("assert");
6
var suite = vows.describe("d3.nest");
13
"returns an array of each distinct key in arbitrary order": function(nest) {
15
.key(function(d) { return d.foo; })
16
.entries([{foo: 1}, {foo: 1}, {foo: 2}])
17
.map(function(d) { return d.key; })
19
assert.deepEqual(keys, ["1", "2"]);
21
"each entry is a key-values object, with values in input order": function(nest) {
23
.key(function(d) { return d.foo; })
24
.entries([{foo: 1, bar: 0}, {foo: 2}, {foo: 1, bar: 1}]);
25
assert.deepEqual(entries, [
26
{key: "1", values: [{foo: 1, bar: 0}, {foo: 1, bar: 1}]},
27
{key: "2", values: [{foo: 2}]}
30
"keys can be sorted using an optional comparator": function(nest) {
32
.key(function(d) { return d.foo; }).sortKeys(d3.descending)
33
.entries([{foo: 1}, {foo: 1}, {foo: 2}])
34
.map(function(d) { return d.key; });
35
assert.deepEqual(keys, ["2", "1"]);
37
"values can be sorted using an optional comparator": function(nest) {
39
.key(function(d) { return d.foo; })
40
.sortValues(function(a, b) { return a.bar - b.bar; })
41
.entries([{foo: 1, bar: 2}, {foo: 1, bar: 0}, {foo: 1, bar: 1}, {foo: 2}]);
42
assert.deepEqual(entries, [
43
{key: "1", values: [{foo: 1, bar: 0}, {foo: 1, bar: 1}, {foo: 1, bar: 2}]},
44
{key: "2", values: [{foo: 2}]}
47
"values can be aggregated using an optional rollup": function(nest) {
49
.key(function(d) { return d.foo; })
50
.rollup(function(values) { return d3.sum(values, function(d) { return d.bar; }); })
51
.entries([{foo: 1, bar: 2}, {foo: 1, bar: 0}, {foo: 1, bar: 1}, {foo: 2}]);
52
assert.deepEqual(entries, [
53
{key: "1", values: 3},
57
"multiple key functions can be specified": function(nest) {
59
.key(function(d) { return d[0]; }).sortKeys(d3.ascending)
60
.key(function(d) { return d[1]; }).sortKeys(d3.ascending)
61
.entries([[0, 1], [0, 2], [1, 1], [1, 2], [0, 2]]);
62
assert.deepEqual(entries, [
64
{key: "1", values: [[0, 1]]},
65
{key: "2", values: [[0, 2], [0, 2]]}
68
{key: "1", values: [[1, 1]]},
69
{key: "2", values: [[1, 2]]}
73
"the rollup function only applies to leaf values": function(nest) {
75
.key(function(d) { return d[0]; }).sortKeys(d3.ascending)
76
.key(function(d) { return d[1]; }).sortKeys(d3.ascending)
77
.rollup(function(values) { return values.length; })
78
.entries([[0, 1], [0, 2], [1, 1], [1, 2], [0, 2]]);
79
assert.deepEqual(entries, [
81
{key: "1", values: 1},
85
{key: "1", values: 1},
90
"the value comparator only applies to leaf values": function(nest) {
92
.key(function(d) { return d[0]; }).sortKeys(d3.ascending)
93
.key(function(d) { return d[1]; }).sortKeys(d3.ascending)
94
.sortValues(function(a, b) { return a[2] - b[2]; })
95
.entries([[0, 1], [0, 2, 1], [1, 1], [1, 2], [0, 2, 0]]);
96
assert.deepEqual(entries, [
98
{key: "1", values: [[0, 1]]},
99
{key: "2", values: [[0, 2, 0], [0, 2, 1]]}
102
{key: "1", values: [[1, 1]]},
103
{key: "2", values: [[1, 2]]}
107
"the key comparator only applies to the last-specified key": function(nest) {
109
.key(function(d) { return d[0]; }).sortKeys(d3.ascending)
110
.key(function(d) { return d[1]; }).sortKeys(d3.descending)
111
.entries([[0, 1], [0, 2], [1, 1], [1, 2], [0, 2]]);
112
assert.deepEqual(entries, [
114
{key: "2", values: [[0, 2], [0, 2]]},
115
{key: "1", values: [[0, 1]]}
118
{key: "2", values: [[1, 2]]},
119
{key: "1", values: [[1, 1]]}
123
.key(function(d) { return d[0]; }).sortKeys(d3.descending)
124
.key(function(d) { return d[1]; }).sortKeys(d3.ascending)
125
.entries([[0, 1], [0, 2], [1, 1], [1, 2], [0, 2]]);
126
assert.deepEqual(entries, [
128
{key: "1", values: [[1, 1]]},
129
{key: "2", values: [[1, 2]]}
132
{key: "1", values: [[0, 1]]},
133
{key: "2", values: [[0, 2], [0, 2]]}
137
"if no keys are specified, the input array is returned": function(nest) {
138
var array = [new Object()];
139
assert.strictEqual(nest().entries(array), array);
149
"returns a map of each distinct key": function(nest) {
151
.key(function(d) { return d.foo; })
152
.map([{foo: 1, bar: 0}, {foo: 2}, {foo: 1, bar: 1}]);
153
assert.deepEqual(map, {
154
"1": [{foo: 1, bar: 0}, {foo: 1, bar: 1}],
158
"values can be sorted using an optional comparator": function(nest) {
160
.key(function(d) { return d.foo; })
161
.sortValues(function(a, b) { return a.bar - b.bar; })
162
.map([{foo: 1, bar: 2}, {foo: 1, bar: 0}, {foo: 1, bar: 1}, {foo: 2}]);
163
assert.deepEqual(map, {
164
"1": [{foo: 1, bar: 0}, {foo: 1, bar: 1}, {foo: 1, bar: 2}],
168
"values can be aggregated using an optional rollup": function(nest) {
170
.key(function(d) { return d.foo; })
171
.rollup(function(values) { return d3.sum(values, function(d) { return d.bar; }); })
172
.map([{foo: 1, bar: 2}, {foo: 1, bar: 0}, {foo: 1, bar: 1}, {foo: 2}]);
173
assert.deepEqual(map, {
178
"multiple key functions can be specified": function(nest) {
180
.key(function(d) { return d[0]; }).sortKeys(d3.ascending)
181
.key(function(d) { return d[1]; }).sortKeys(d3.ascending)
182
.map([[0, 1], [0, 2], [1, 1], [1, 2], [0, 2]]);
183
assert.deepEqual(map, {
186
"2": [[0, 2], [0, 2]]
194
"the rollup function only applies to leaf values": function(nest) {
196
.key(function(d) { return d[0]; }).sortKeys(d3.ascending)
197
.key(function(d) { return d[1]; }).sortKeys(d3.ascending)
198
.rollup(function(values) { return values.length; })
199
.map([[0, 1], [0, 2], [1, 1], [1, 2], [0, 2]]);
200
assert.deepEqual(map, {
211
"the value comparator only applies to leaf values": function(nest) {
213
.key(function(d) { return d[0]; }).sortKeys(d3.ascending)
214
.key(function(d) { return d[1]; }).sortKeys(d3.ascending)
215
.sortValues(function(a, b) { return a[2] - b[2]; })
216
.map([[0, 1], [0, 2, 1], [1, 1], [1, 2], [0, 2, 0]]);
217
assert.deepEqual(map, {
220
"2": [[0, 2, 0], [0, 2, 1]]
228
"if no keys are specified, the input array is returned": function(nest) {
229
var array = [new Object()];
230
assert.strictEqual(nest().map(array), array);
232
"handles keys that are built-in prototype properties": function(nest) {
235
.map(["hasOwnProperty"]);
236
assert.deepEqual(keys, {hasOwnProperty: ["hasOwnProperty"]});
241
suite.export(module);