/
aggregate_groups_test.ts
87 lines (78 loc) · 1.76 KB
/
aggregate_groups_test.ts
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
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";
import { aggregateGroups } from "./aggregate_groups.ts";
function aggregateGroupsTest<T, A>(
input: [
Record<string, Array<T>>,
(current: T, key: string, first: boolean, accumulator?: A) => A,
],
expected: Record<string, A>,
message?: string,
) {
const actual = aggregateGroups(...input);
assertEquals(actual, expected, message);
}
Deno.test({
name: "[collections/aggregateGroups] no mutation",
fn() {
const input = {
Woody: [2, 3, 1, 4],
Buzz: [5, 9],
};
aggregateGroups(input, () => 5);
assertEquals(input, {
Woody: [2, 3, 1, 4],
Buzz: [5, 9],
});
},
});
Deno.test({
name: "[collections/aggregateGroups] string building using all params",
fn() {
aggregateGroupsTest(
[
{
"Curry": ["spicy", "vegan"],
"Omelette": ["creamy", "vegetarian"],
},
(current, key, first, acc) => {
if (first) {
return `${key} is ${current}`;
}
return `${acc} and ${current}`;
},
],
{
"Curry": "Curry is spicy and vegan",
"Omelette": "Omelette is creamy and vegetarian",
},
);
},
});
Deno.test({
name: "[collections/aggregateGroups] sum ignoring non reduce params",
fn() {
aggregateGroupsTest(
[
{
Woody: [1, 2, 3],
Buzz: [5, 6, 7],
},
(current, _key, _first, acc) => current + (acc ?? 0),
],
{
Woody: 6,
Buzz: 18,
},
);
},
});
Deno.test({
name: "[collections/aggregateGroups] empty input",
fn() {
aggregateGroupsTest([
{},
() => 1,
], {});
},
});