deno.land / std@0.157.0 / collections / map_keys_test.ts

map_keys_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
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";import { mapKeys } from "./map_keys.ts";
function mapKeysTest<T>( input: [Record<string, T>, (key: string) => string], expected: Record<string, T>, message?: string,) { const actual = mapKeys(...input); assertEquals(actual, expected, message);}
Deno.test({ name: "[collections/mapKeys] no mutation", fn() { const object = { a: 5, b: true }; mapKeys(object, (it) => `${it}a`);
assertEquals(object, { a: 5, b: true }); },});
Deno.test({ name: "[collections/mapKeys] empty input", fn() { mapKeysTest( [{}, (it) => it], {}, ); },});
Deno.test({ name: "[collections/mapKeys] identity", fn() { mapKeysTest( [ { foo: true, bar: "lorem", 1: -5, }, (it) => it, ], { foo: true, bar: "lorem", 1: -5, }, ); },});
Deno.test({ name: "[collections/mapKeys] to constant key", fn() { mapKeysTest( [ { test: "foo", "": [] }, () => "a", ], { a: [] }, ); },});
Deno.test({ name: "[collections/mapKeys] overlapping keys", fn() { mapKeysTest( [ { "Anna": 22, "Kim": 24, "Karen": 33, "Claudio": 11, "Karl": 45, }, (name) => name.charAt(0), ], { "A": 22, "K": 45, "C": 11, }, ); mapKeysTest( [ { "ad04": "foo", "ad28": "bar", "100f": "dino", }, (it) => it.substr(0, 2), ], { "ad": "bar", "10": "dino", }, ); },});
Deno.test({ name: "[collections/mapKeys] empty key", fn() { mapKeysTest( [ { "ab": 22, "a": 24, "bcd": 33, "d": 11, }, (key) => key.substr(1), ], { "b": 22, "": 11, "cd": 33, }, ); },});
Deno.test({ name: "[collections/mapKeys] normal mappers", fn() { mapKeysTest( [ { "/home/deno/food.txt": "Plants, preferably fruit", "/home/deno/other-dinos.txt": "Noderaptor, Pythonoctorus", }, (path) => path.split("/").slice(-1)[0], ], { "food.txt": "Plants, preferably fruit", "other-dinos.txt": "Noderaptor, Pythonoctorus", }, ); mapKeysTest( [ { "EUR": 1200, "USD": 1417, "JPY": 1563, }, (currencyCode) => ({ EUR: "Euro", USD: "US Dollar", JPY: "Japanese Yen" })[ currencyCode ] ?? "_", ], { "Euro": 1200, "US Dollar": 1417, "Japanese Yen": 1563, }, ); },});
std

Version Info

Tagged at
a year ago