deno.land / std@0.224.0 / collections / reduce_groups_test.ts

reduce_groups_test.ts
View Documentation
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../assert/mod.ts";import { reduceGroups } from "./reduce_groups.ts";
function reduceGroupsTest<T, A>( input: [ record: Record<string, ReadonlyArray<T>>, reducer: (accumulator: A, current: T) => A, initialValue: A, ], expected: Record<string, A>, message?: string,) { const actual = reduceGroups(...input); assertEquals(actual, expected, message);}
Deno.test({ name: "mapEntries() handles no mutation", fn() { const input = { Woody: [2, 3, 1, 4], Buzz: [5, 9], };
reduceGroups(input, (sum, it) => sum + it, 0);
assertEquals(input, { Woody: [2, 3, 1, 4], Buzz: [5, 9], }); },});
Deno.test({ name: "mapEntries() handles array of numbers", fn() { reduceGroupsTest( [ { Woody: [2, 3, 1, 4], Buzz: [5, 9], }, (sum, it) => sum + it, 0, ], { Woody: 10, Buzz: 14, }, ); },});
Deno.test({ name: "mapEntries() handles array of strings", fn() { reduceGroupsTest( [ { Woody: ["foo", "bar"], Buzz: ["foo", "bar", "baz"], }, (sum, it) => sum + it + " ", "", ], { Woody: "foo bar ", Buzz: "foo bar baz ", }, ); },});
Deno.test({ name: "mapEntries() handles mapper", fn() { reduceGroupsTest( [ { Woody: [{ val: 14 }, { val: 24 }], Buzz: [{ val: 1 }, { val: 2 }, { val: 3 }, { val: 4 }], }, (sum, it) => sum + it.val, 0, ], { Woody: 38, Buzz: 10, }, ); },});
Deno.test({ name: "mapEntries() handles initial value", fn() { reduceGroupsTest( [ { Woody: [], Buzz: [], }, (sum, it) => sum + it, 24, ], { Woody: 24, Buzz: 24, }, ); },});
Deno.test({ name: "mapEntries() handles empty input", fn() { reduceGroupsTest([{}, () => 0, 0], {}); },});
std

Version Info

Tagged at
6 months ago