-
-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathUtilities-test.js
More file actions
412 lines (346 loc) · 11.3 KB
/
Utilities-test.js
File metadata and controls
412 lines (346 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import * as utils from '../Utilities';
/* eslint-disable no-magic-numbers */
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const fixtureData = [
['name', 'gender', 'colour', 'birthday', 'trials', 'successes'],
['Nick', 'male', 'blue', '1982-11-07', 103, 12],
['Jane', 'female', 'red', '1982-11-08', 95, 25],
['John', 'male', 'blue', '1982-12-08', 112, 30],
['Carol', 'female', 'yellow', '1983-12-08', 102, 14],
];
describe(' utils', function() {
describe('.PivotData()', function() {
describe('with no options', function() {
const aoaInput = [
['a', 'b'],
[1, 2],
[3, 4],
];
const pd = new utils.PivotData({data: aoaInput});
it('has the correct grand total value', () =>
expect(pd.getAggregator([], []).value()).toBe(2));
});
describe('with array-of-array input', function() {
const aoaInput = [
['a', 'b'],
[1, 2],
[3, 4],
];
const pd = new utils.PivotData({
data: aoaInput,
aggregatorName: 'Sum over Sum',
vals: ['a', 'b'],
});
it('has the correct grand total value', () =>
expect(pd.getAggregator([], []).value()).toBe((1 + 3) / (2 + 4)));
});
describe('with array-of-object input', function() {
const aosInput = [
{a: 1, b: 2},
{a: 3, b: 4},
];
const pd = new utils.PivotData({
data: aosInput,
aggregatorName: 'Sum over Sum',
vals: ['a', 'b'],
});
it('has the correct grand total value', () =>
expect(pd.getAggregator([], []).value()).toBe((1 + 3) / (2 + 4)));
});
describe('with ragged array-of-object input', function() {
const raggedAosInput = [{a: 1}, {b: 4}, {a: 3, b: 2}];
const pd = new utils.PivotData({
data: raggedAosInput,
aggregatorName: 'Sum over Sum',
vals: ['a', 'b'],
});
it('has the correct grand total value', () =>
expect(pd.getAggregator([], []).value()).toBe((1 + 3) / (2 + 4)));
});
describe('with function input', function() {
const functionInput = function(record) {
record({a: 1, b: 2});
record({a: 3, b: 4});
};
const pd = new utils.PivotData({
data: functionInput,
aggregatorName: 'Sum over Sum',
vals: ['a', 'b'],
});
it('has the correct grand total value', () =>
expect(pd.getAggregator([], []).value()).toBe((1 + 3) / (2 + 4)));
});
describe('with rows/cols', function() {
const pd = new utils.PivotData({
data: fixtureData,
rows: ['name', 'colour'],
cols: ['trials', 'successes'],
});
it('has correctly-ordered row keys', () =>
expect(pd.getRowKeys()).toEqual([
['Carol', 'yellow'],
['Jane', 'red'],
['John', 'blue'],
['Nick', 'blue'],
]));
it('has correctly-ordered col keys', () =>
expect(pd.getColKeys()).toEqual([
[95, 25],
[102, 14],
[103, 12],
[112, 30],
]));
it('can be iterated over', function() {
let numNotNull = 0;
let numNull = 0;
for (const r of Array.from(pd.getRowKeys())) {
for (const c of Array.from(pd.getColKeys())) {
if (pd.getAggregator(r, c).value() !== null) {
numNotNull++;
} else {
numNull++;
}
}
}
expect(numNotNull).toBe(4);
expect(numNull).toBe(12);
});
it('returns matching records', function() {
const records = [];
pd.forEachMatchingRecord({gender: 'male'}, x => records.push(x.name));
expect(records).toEqual(['Nick', 'John']);
});
it('has a correct spot-checked aggregator', function() {
const agg = pd.getAggregator(['Carol', 'yellow'], [102, 14]);
const val = agg.value();
expect(val).toBe(1);
expect(agg.format(val)).toBe('1');
});
it('returns empty aggregator when key not found', function() {
// inexistent row key, empty col key
let agg = pd.getAggregator(['x', 'y'], []);
let val = agg.value();
expect(val).toBe(null);
// empty row key, inexistent col key
agg = pd.getAggregator([], [1, 2]);
val = agg.value();
expect(val).toBe(null);
// inexistent row key, non empty col key
agg = pd.getAggregator(['x', 'y'], [1, 2]);
val = agg.value();
expect(val).toBe(null);
});
it('has a correct grand total aggregator', function() {
const agg = pd.getAggregator([], []);
const val = agg.value();
expect(val).toBe(4);
expect(agg.format(val)).toBe('4');
});
});
});
describe('.aggregatorTemplates', function() {
const getVal = (agg, vals) => {
return new utils.PivotData({
data: fixtureData,
aggregators: {agg},
aggregatorName: 'agg',
vals,
})
.getAggregator([], [])
.value();
};
const tpl = utils.aggregatorTemplates;
describe('.count', () =>
it('works', () => expect(getVal(tpl.count(), [])).toBe(4)));
describe('.countUnique', () =>
it('works', () => expect(getVal(tpl.countUnique(), ['gender'])).toBe(2)));
describe('.listUnique', () =>
it('works', () =>
expect(getVal(tpl.listUnique(), ['gender'])).toBe('male,female')));
describe('.average', () =>
it('works', () => expect(getVal(tpl.average(), ['trials'])).toBe(103)));
describe('.sum', () =>
it('works', () => expect(getVal(tpl.sum(), ['trials'])).toBe(412)));
describe('.min', () =>
it('works', () => expect(getVal(tpl.min(), ['trials'])).toBe(95)));
describe('.max', () =>
it('works', () => expect(getVal(tpl.max(), ['trials'])).toBe(112)));
describe('.first', () =>
it('works', () => expect(getVal(tpl.first(), ['name'])).toBe('Carol')));
describe('.last', () =>
it('works', () => expect(getVal(tpl.last(), ['name'])).toBe('Nick')));
describe('.average', () =>
it('works', () => expect(getVal(tpl.average(), ['trials'])).toBe(103)));
describe('.median', () =>
it('works', () => expect(getVal(tpl.median(), ['trials'])).toBe(102.5)));
describe('.quantile', () =>
it('works', function() {
expect(getVal(tpl.quantile(0), ['trials'])).toBe(95);
expect(getVal(tpl.quantile(0.1), ['trials'])).toBe(98.5);
expect(getVal(tpl.quantile(0.25), ['trials'])).toBe(98.5);
expect(getVal(tpl.quantile(1 / 3), ['trials'])).toBe(102);
expect(getVal(tpl.quantile(1), ['trials'])).toBe(112);
}));
describe('.var', () =>
it('works', () =>
expect(getVal(tpl.var(), ['trials'])).toBe(48.666666666666686)));
describe('.stdev', () =>
it('works', () =>
expect(getVal(tpl.stdev(), ['trials'])).toBe(6.976149845485451)));
describe('.sumOverSum', () =>
it('works', () =>
expect(getVal(tpl.sumOverSum(), ['successes', 'trials'])).toBe(
(12 + 25 + 30 + 14) / (95 + 102 + 103 + 112)
)));
describe('.fractionOf', () =>
it('works', () =>
expect(getVal(tpl.fractionOf(tpl.sum()), ['trials'])).toBe(1)));
});
describe('.naturalSort()', function() {
const {naturalSort} = utils;
const sortedArr = [
null,
NaN,
-Infinity,
'-Infinity',
-3,
'-3',
-2,
'-2',
-1,
'-1',
0,
'2e-1',
1,
'01',
'1',
2,
'002',
'002e0',
'02',
'2',
'2e-0',
3,
10,
'10',
'11',
'12',
'1e2',
'112',
Infinity,
'Infinity',
'1a',
'2a',
'12a',
'20a',
'A',
'A',
'NaN',
'a',
'a',
'a01',
'a012',
'a02',
'a1',
'a2',
'a12',
'a12',
'a21',
'a21',
'b',
'c',
'd',
'null',
];
it('sorts naturally (null, NaN, numbers & numbery strings, Alphanum for text strings)', () =>
expect(sortedArr.slice().sort(naturalSort)).toEqual(sortedArr));
});
describe('.sortAs()', function() {
const {sortAs} = utils;
it('sorts with unknown values sorted at the end', () =>
expect([5, 2, 3, 4, 1].sort(sortAs([4, 3, 2]))).toEqual([4, 3, 2, 1, 5]));
it('sorts lowercase after uppercase', () =>
expect(['Ab', 'aA', 'aa', 'ab'].sort(sortAs(['Ab', 'Aa']))).toEqual([
'Ab',
'ab',
'aa',
'aA',
]));
});
describe('.numberFormat()', function() {
const {numberFormat} = utils;
it('formats numbers', function() {
const nf = numberFormat();
expect(nf(1234567.89123456)).toEqual('1,234,567.89');
});
it('formats booleans', function() {
const nf = numberFormat();
expect(nf(true)).toEqual('1.00');
});
it('formats numbers in strings', function() {
const nf = numberFormat();
expect(nf('1234567.89123456')).toEqual('1,234,567.89');
});
it("doesn't formats strings", function() {
const nf = numberFormat();
expect(nf('hi there')).toEqual('');
});
it("doesn't formats objects", function() {
const nf = numberFormat();
expect(nf({a: 1})).toEqual('');
});
it('formats percentages', function() {
const nf = numberFormat({scaler: 100, suffix: '%'});
expect(nf(0.12345)).toEqual('12.35%');
});
it('adds separators', function() {
const nf = numberFormat({thousandsSep: 'a', decimalSep: 'b'});
expect(nf(1234567.89123456)).toEqual('1a234a567b89');
});
it('adds prefixes and suffixes', function() {
const nf = numberFormat({prefix: 'a', suffix: 'b'});
expect(nf(1234567.89123456)).toEqual('a1,234,567.89b');
});
it('scales and rounds', function() {
const nf = numberFormat({digitsAfterDecimal: 3, scaler: 1000});
expect(nf(1234567.89123456)).toEqual('1,234,567,891.235');
});
});
describe('.derivers', function() {
describe('.dateFormat()', function() {
const df = utils.derivers.dateFormat(
'x',
'abc % %% %%% %a %y %m %n %d %w %x %H %M %S',
true
);
it('formats date objects', () =>
expect(df({x: new Date('2015-01-02T23:43:11Z')})).toBe(
'abc % %% %%% %a 2015 01 Jan 02 Fri 5 23 43 11'
));
it('formats input parsed by Date.parse()', function() {
expect(df({x: '2015-01-02T23:43:11Z'})).toBe(
'abc % %% %%% %a 2015 01 Jan 02 Fri 5 23 43 11'
);
expect(df({x: 'bla'})).toBe('');
});
});
describe('.bin()', function() {
const binner = utils.derivers.bin('x', 10);
it('bins numbers', function() {
expect(binner({x: 11})).toBe(10);
expect(binner({x: 9})).toBe(0);
expect(binner({x: 111})).toBe(110);
});
it('bins booleans', () => expect(binner({x: true})).toBe(0));
it('bins negative numbers', () => expect(binner({x: -12})).toBe(-10));
it("doesn't bin strings", () => expect(binner({x: 'a'})).toBeNaN());
it("doesn't bin objects", () => expect(binner({x: {a: 1}})).toBeNaN());
});
});
});