/
associate_by_test.ts
125 lines (116 loc) · 2.43 KB
/
associate_by_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
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";
import { associateBy } from "./associate_by.ts";
function associateByTest<T>(
input: [Array<T>, (el: T) => string],
expected: { [x: string]: T },
message?: string,
) {
const actual = associateBy(...input);
assertEquals(actual, expected, message);
}
Deno.test({
name: "[collections/associateBy] no mutation",
fn() {
const arrayA = ["Foo", "Bar"];
associateBy(arrayA, (it) => it.charAt(0));
assertEquals(arrayA, ["Foo", "Bar"]);
},
});
Deno.test({
name: "[collections/associateBy] empty input",
fn() {
associateByTest(
[[], () => "a"],
{},
);
},
});
Deno.test({
name: "[collections/associateBy] constant key",
fn() {
associateByTest(
[[1, 3, 5, 6], () => "a"],
{ a: 6 },
);
},
});
Deno.test({
name: "[collections/associateBy] empty key",
fn() {
associateByTest(
[
["Foo", "b"],
(it) => it.charAt(1),
],
{
"o": "Foo",
"": "b",
},
);
},
});
Deno.test({
name: "[collections/associateBy] duplicate keys",
fn() {
associateByTest(
[
["Anna", "Marija", "Karl", "Arnold", "Martha"],
(it) => it.charAt(0),
],
{
"A": "Arnold",
"M": "Martha",
"K": "Karl",
},
);
associateByTest(
[
[1.2, 2, 2.3, 6.3, 6.9, 6],
(it) => Math.floor(it).toString(),
],
{
"1": 1.2,
"2": 2.3,
"6": 6,
},
);
},
});
Deno.test({
name: "[collections/associateBy] associates",
fn() {
associateByTest(
[
[
{ name: "test", done: false },
{ name: "build", done: true },
{ name: "deploy", done: false },
{ name: "audit", done: true },
],
(it) => it.name,
],
{
"test": { name: "test", done: false },
"build": { name: "build", done: true },
"deploy": { name: "deploy", done: false },
"audit": { name: "audit", done: true },
},
);
associateByTest(
[
[
"anna@example.org",
"josh@example.org",
"kim@example.org",
],
(it) => it.substring(0, it.indexOf("@")),
],
{
"anna": "anna@example.org",
"josh": "josh@example.org",
"kim": "kim@example.org",
},
);
},
});