|
1 require("../env"); |
|
2 require("../../d3"); |
|
3 |
|
4 var vows = require("vows"), |
|
5 assert = require("assert"); |
|
6 |
|
7 var suite = vows.describe("d3.nest"); |
|
8 |
|
9 suite.addBatch({ |
|
10 "entries": { |
|
11 topic: function() { |
|
12 return d3.nest; |
|
13 }, |
|
14 "returns an array of each distinct key in arbitrary order": function(nest) { |
|
15 var keys = nest() |
|
16 .key(function(d) { return d.foo; }) |
|
17 .entries([{foo: 1}, {foo: 1}, {foo: 2}]) |
|
18 .map(function(d) { return d.key; }) |
|
19 .sort(d3.ascending); |
|
20 assert.deepEqual(keys, ["1", "2"]); |
|
21 }, |
|
22 "each entry is a key-values object, with values in input order": function(nest) { |
|
23 var entries = nest() |
|
24 .key(function(d) { return d.foo; }) |
|
25 .entries([{foo: 1, bar: 0}, {foo: 2}, {foo: 1, bar: 1}]); |
|
26 assert.deepEqual(entries, [ |
|
27 {key: "1", values: [{foo: 1, bar: 0}, {foo: 1, bar: 1}]}, |
|
28 {key: "2", values: [{foo: 2}]} |
|
29 ]); |
|
30 }, |
|
31 "keys can be sorted using an optional comparator": function(nest) { |
|
32 var keys = nest() |
|
33 .key(function(d) { return d.foo; }).sortKeys(d3.descending) |
|
34 .entries([{foo: 1}, {foo: 1}, {foo: 2}]) |
|
35 .map(function(d) { return d.key; }); |
|
36 assert.deepEqual(keys, ["2", "1"]); |
|
37 }, |
|
38 "values can be sorted using an optional comparator": function(nest) { |
|
39 var entries = nest() |
|
40 .key(function(d) { return d.foo; }) |
|
41 .sortValues(function(a, b) { return a.bar - b.bar; }) |
|
42 .entries([{foo: 1, bar: 2}, {foo: 1, bar: 0}, {foo: 1, bar: 1}, {foo: 2}]); |
|
43 assert.deepEqual(entries, [ |
|
44 {key: "1", values: [{foo: 1, bar: 0}, {foo: 1, bar: 1}, {foo: 1, bar: 2}]}, |
|
45 {key: "2", values: [{foo: 2}]} |
|
46 ]); |
|
47 }, |
|
48 "values can be aggregated using an optional rollup": function(nest) { |
|
49 var entries = nest() |
|
50 .key(function(d) { return d.foo; }) |
|
51 .rollup(function(values) { return d3.sum(values, function(d) { return d.bar; }); }) |
|
52 .entries([{foo: 1, bar: 2}, {foo: 1, bar: 0}, {foo: 1, bar: 1}, {foo: 2}]); |
|
53 assert.deepEqual(entries, [ |
|
54 {key: "1", values: 3}, |
|
55 {key: "2", values: 0} |
|
56 ]); |
|
57 }, |
|
58 "multiple key functions can be specified": function(nest) { |
|
59 var entries = nest() |
|
60 .key(function(d) { return d[0]; }).sortKeys(d3.ascending) |
|
61 .key(function(d) { return d[1]; }).sortKeys(d3.ascending) |
|
62 .entries([[0, 1], [0, 2], [1, 1], [1, 2], [0, 2]]); |
|
63 assert.deepEqual(entries, [ |
|
64 {key: "0", values: [ |
|
65 {key: "1", values: [[0, 1]]}, |
|
66 {key: "2", values: [[0, 2], [0, 2]]} |
|
67 ]}, |
|
68 {key: "1", values: [ |
|
69 {key: "1", values: [[1, 1]]}, |
|
70 {key: "2", values: [[1, 2]]} |
|
71 ]} |
|
72 ]); |
|
73 }, |
|
74 "the rollup function only applies to leaf values": function(nest) { |
|
75 var entries = nest() |
|
76 .key(function(d) { return d[0]; }).sortKeys(d3.ascending) |
|
77 .key(function(d) { return d[1]; }).sortKeys(d3.ascending) |
|
78 .rollup(function(values) { return values.length; }) |
|
79 .entries([[0, 1], [0, 2], [1, 1], [1, 2], [0, 2]]); |
|
80 assert.deepEqual(entries, [ |
|
81 {key: "0", values: [ |
|
82 {key: "1", values: 1}, |
|
83 {key: "2", values: 2} |
|
84 ]}, |
|
85 {key: "1", values: [ |
|
86 {key: "1", values: 1}, |
|
87 {key: "2", values: 1} |
|
88 ]} |
|
89 ]); |
|
90 }, |
|
91 "the value comparator only applies to leaf values": function(nest) { |
|
92 var entries = nest() |
|
93 .key(function(d) { return d[0]; }).sortKeys(d3.ascending) |
|
94 .key(function(d) { return d[1]; }).sortKeys(d3.ascending) |
|
95 .sortValues(function(a, b) { return a[2] - b[2]; }) |
|
96 .entries([[0, 1], [0, 2, 1], [1, 1], [1, 2], [0, 2, 0]]); |
|
97 assert.deepEqual(entries, [ |
|
98 {key: "0", values: [ |
|
99 {key: "1", values: [[0, 1]]}, |
|
100 {key: "2", values: [[0, 2, 0], [0, 2, 1]]} |
|
101 ]}, |
|
102 {key: "1", values: [ |
|
103 {key: "1", values: [[1, 1]]}, |
|
104 {key: "2", values: [[1, 2]]} |
|
105 ]} |
|
106 ]); |
|
107 }, |
|
108 "the key comparator only applies to the last-specified key": function(nest) { |
|
109 var entries = nest() |
|
110 .key(function(d) { return d[0]; }).sortKeys(d3.ascending) |
|
111 .key(function(d) { return d[1]; }).sortKeys(d3.descending) |
|
112 .entries([[0, 1], [0, 2], [1, 1], [1, 2], [0, 2]]); |
|
113 assert.deepEqual(entries, [ |
|
114 {key: "0", values: [ |
|
115 {key: "2", values: [[0, 2], [0, 2]]}, |
|
116 {key: "1", values: [[0, 1]]} |
|
117 ]}, |
|
118 {key: "1", values: [ |
|
119 {key: "2", values: [[1, 2]]}, |
|
120 {key: "1", values: [[1, 1]]} |
|
121 ]} |
|
122 ]); |
|
123 var entries = nest() |
|
124 .key(function(d) { return d[0]; }).sortKeys(d3.descending) |
|
125 .key(function(d) { return d[1]; }).sortKeys(d3.ascending) |
|
126 .entries([[0, 1], [0, 2], [1, 1], [1, 2], [0, 2]]); |
|
127 assert.deepEqual(entries, [ |
|
128 {key: "1", values: [ |
|
129 {key: "1", values: [[1, 1]]}, |
|
130 {key: "2", values: [[1, 2]]} |
|
131 ]}, |
|
132 {key: "0", values: [ |
|
133 {key: "1", values: [[0, 1]]}, |
|
134 {key: "2", values: [[0, 2], [0, 2]]} |
|
135 ]} |
|
136 ]); |
|
137 }, |
|
138 "if no keys are specified, the input array is returned": function(nest) { |
|
139 var array = [new Object()]; |
|
140 assert.strictEqual(nest().entries(array), array); |
|
141 } |
|
142 } |
|
143 }); |
|
144 |
|
145 suite.addBatch({ |
|
146 "map": { |
|
147 topic: function() { |
|
148 return d3.nest; |
|
149 }, |
|
150 "returns a map of each distinct key": function(nest) { |
|
151 var map = nest() |
|
152 .key(function(d) { return d.foo; }) |
|
153 .map([{foo: 1, bar: 0}, {foo: 2}, {foo: 1, bar: 1}]); |
|
154 assert.deepEqual(map, { |
|
155 "1": [{foo: 1, bar: 0}, {foo: 1, bar: 1}], |
|
156 "2": [{foo: 2}] |
|
157 }); |
|
158 }, |
|
159 "values can be sorted using an optional comparator": function(nest) { |
|
160 var map = nest() |
|
161 .key(function(d) { return d.foo; }) |
|
162 .sortValues(function(a, b) { return a.bar - b.bar; }) |
|
163 .map([{foo: 1, bar: 2}, {foo: 1, bar: 0}, {foo: 1, bar: 1}, {foo: 2}]); |
|
164 assert.deepEqual(map, { |
|
165 "1": [{foo: 1, bar: 0}, {foo: 1, bar: 1}, {foo: 1, bar: 2}], |
|
166 "2": [{foo: 2}] |
|
167 }); |
|
168 }, |
|
169 "values can be aggregated using an optional rollup": function(nest) { |
|
170 var map = nest() |
|
171 .key(function(d) { return d.foo; }) |
|
172 .rollup(function(values) { return d3.sum(values, function(d) { return d.bar; }); }) |
|
173 .map([{foo: 1, bar: 2}, {foo: 1, bar: 0}, {foo: 1, bar: 1}, {foo: 2}]); |
|
174 assert.deepEqual(map, { |
|
175 "1": 3, |
|
176 "2": 0 |
|
177 }); |
|
178 }, |
|
179 "multiple key functions can be specified": function(nest) { |
|
180 var map = nest() |
|
181 .key(function(d) { return d[0]; }).sortKeys(d3.ascending) |
|
182 .key(function(d) { return d[1]; }).sortKeys(d3.ascending) |
|
183 .map([[0, 1], [0, 2], [1, 1], [1, 2], [0, 2]]); |
|
184 assert.deepEqual(map, { |
|
185 "0": { |
|
186 "1": [[0, 1]], |
|
187 "2": [[0, 2], [0, 2]] |
|
188 }, |
|
189 "1": { |
|
190 "1": [[1, 1]], |
|
191 "2": [[1, 2]] |
|
192 } |
|
193 }); |
|
194 }, |
|
195 "the rollup function only applies to leaf values": function(nest) { |
|
196 var map = nest() |
|
197 .key(function(d) { return d[0]; }).sortKeys(d3.ascending) |
|
198 .key(function(d) { return d[1]; }).sortKeys(d3.ascending) |
|
199 .rollup(function(values) { return values.length; }) |
|
200 .map([[0, 1], [0, 2], [1, 1], [1, 2], [0, 2]]); |
|
201 assert.deepEqual(map, { |
|
202 "0": { |
|
203 "1": 1, |
|
204 "2": 2 |
|
205 }, |
|
206 "1": { |
|
207 "1": 1, |
|
208 "2": 1 |
|
209 } |
|
210 }); |
|
211 }, |
|
212 "the value comparator only applies to leaf values": function(nest) { |
|
213 var map = nest() |
|
214 .key(function(d) { return d[0]; }).sortKeys(d3.ascending) |
|
215 .key(function(d) { return d[1]; }).sortKeys(d3.ascending) |
|
216 .sortValues(function(a, b) { return a[2] - b[2]; }) |
|
217 .map([[0, 1], [0, 2, 1], [1, 1], [1, 2], [0, 2, 0]]); |
|
218 assert.deepEqual(map, { |
|
219 "0": { |
|
220 "1": [[0, 1]], |
|
221 "2": [[0, 2, 0], [0, 2, 1]] |
|
222 }, |
|
223 "1": { |
|
224 "1": [[1, 1]], |
|
225 "2": [[1, 2]] |
|
226 } |
|
227 }); |
|
228 }, |
|
229 "if no keys are specified, the input array is returned": function(nest) { |
|
230 var array = [new Object()]; |
|
231 assert.strictEqual(nest().map(array), array); |
|
232 } |
|
233 } |
|
234 }); |
|
235 |
|
236 suite.export(module); |