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

map_values_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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../assert/mod.ts";import { mapValues } from "./map_values.ts";
function mapValuesTest<T, O>( input: [Record<string, T>, (value: T) => O], expected: Record<string, O>, message?: string,) { const actual = mapValues(...input); assertEquals(actual, expected, message);}
Deno.test({ name: "mapValues() handles no mutation", fn() { const object = { a: 5, b: true }; mapValues(object, (it) => it ?? "nothing");
assertEquals(object, { a: 5, b: true }); },});
Deno.test({ name: "mapValues() handles empty input", fn() { mapValuesTest( [{}, (it) => it], {}, ); },});
Deno.test({ name: "mapValues() handles identity", fn() { mapValuesTest( [ { foo: true, bar: "lorem", 1: -5, }, (it) => it, ], { foo: true, bar: "lorem", 1: -5, }, ); },});
Deno.test({ name: "mapValues() handles falsy values", fn() { mapValuesTest<number, null | undefined | string | boolean>( [ { "something": 0, "yetanotherkey": 1, "andonemore": 2, "lastone": 3, }, (it) => [null, undefined, "", false][it], ], { "something": null, "yetanotherkey": undefined, "andonemore": "", "lastone": false, }, ); },});
Deno.test({ name: "mapValues() handles normal mappers", fn() { mapValuesTest( [ { "FoodFile": "/home/deno/food.txt", "CalendarFile": "/home/deno/weekend.cal", }, (path) => path.slice(path.lastIndexOf(".")), ], { "FoodFile": ".txt", "CalendarFile": ".cal", }, ); mapValuesTest( [ { "Curry": 20, "Beans": 2, "Rice": 3.5, }, (price) => price.toFixed(2), ], { "Curry": "20.00", "Beans": "2.00", "Rice": "3.50", }, ); },});
Deno.test({ name: "mapValues() preserves key type (Record)", fn() { type Variants = "a" | "b"; const input: Record<Variants, string> = { a: "a", b: "b" }; const actual = mapValues( input, (_: string) => 1, ); const expected = { a: 1, b: 1 };
assertEquals(actual, expected); },});
Deno.test({ name: "mapValues() preserves key type (Partial Record)", fn() { type Variants = "a" | "b"; const input: Partial<Record<Variants, string>> = { a: "a" }; const actual = mapValues( input, (_: string) => 1, ); const expected = { a: 1 };
assertEquals(actual, expected); },});
Deno.test({ name: "mapValues() pass key to transformer", fn() { const key = "key"; const actual = mapValues( { [key]: "value" }, (_, k) => k, ); const expected = { [key]: key };
assertEquals(actual, expected); },});
std

Version Info

Tagged at
3 weeks ago