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

running_reduce_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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";import { runningReduce } from "./running_reduce.ts";
Deno.test({ name: "[collections/runningReduce] no mutation", fn() { const numbers = [1, 2, 3, 4, 5]; runningReduce(numbers, (sum, current) => sum + current, 0);
assertEquals(numbers, [1, 2, 3, 4, 5]); },});
Deno.test({ name: "[collections/runningReduce] array of numbers initialValue 0", fn() { const numbers = [1, 2, 3, 4, 5]; const result = runningReduce(numbers, (sum, current) => sum + current, 0);
assertEquals(result, [1, 3, 6, 10, 15]); },});
Deno.test({ name: "[collections/runningReduce] array of numbers initialValue 5", fn() { const numbers = [1, 2, 3, 4, 5]; const result = runningReduce(numbers, (sum, current) => sum + current, 5);
assertEquals(result, [6, 8, 11, 15, 20]); },});
Deno.test({ name: `[collections/runningReduce] array of strings initialValue ""`, fn() { const strings = ["a", "b", "c", "d", "e"]; const result = runningReduce(strings, (str, current) => str + current, "");
assertEquals(result, ["a", "ab", "abc", "abcd", "abcde"]); },});
Deno.test({ name: `[collections/runningReduce] array of strings initialValue "foo"`, fn() { const strings = ["a", "b", "c", "d", "e"]; const result = runningReduce( strings, (str, current) => str + current, "foo", );
assertEquals(result, ["fooa", "fooab", "fooabc", "fooabcd", "fooabcde"]); },});
Deno.test({ name: "[collections/runningReduce] empty array initialValue 0", fn() { const result = runningReduce([], (sum, current) => sum + current, 0);
assertEquals(result, []); },});
Deno.test({ name: "[collections/runningReduce] empty array initialValue 5", fn() { const result = runningReduce([], (sum, current) => sum + current, 5);
assertEquals(result, []); },});
Deno.test({ name: "[collections/runningReduce] array of objects initialValue 0", fn() { const medals = [ { country: "USA", count: 113 }, { country: "CHN", count: 88 }, { country: "JPN", count: 58 }, ]; const result = runningReduce( medals, (sum, current) => sum + current.count, 0, );
assertEquals(result, [113, 201, 259]); },});
Deno.test({ name: `[collections/runningReduce] array of objects initialValue ""`, fn() { const medals = [ { country: "USA", count: 113 }, { country: "CHN", count: 88 }, { country: "JPN", count: 58 }, ]; const result = runningReduce( medals, (sum, current) => sum + current.count, "", );
assertEquals(result, ["113", "11388", "1138858"]); },});
Deno.test({ name: "[collections/runningReduce] reduce array of numbers with currentIndex", fn() { const numbers = [1, 2, 3, 4, 5]; const result = runningReduce( numbers, (sum, current, currentIndex) => sum + current + currentIndex, 0, );
assertEquals(result, [1, 4, 9, 16, 25]); },});
Deno.test({ name: "[collections/runningReduce] reduce array of strings with currentIndex", fn() { const strings = ["a", "b", "c", "d", "e"]; const result = runningReduce( strings, (sum, current, currentIndex) => sum + current + currentIndex, "", );
assertEquals(result, ["a0", "a0b1", "a0b1c2", "a0b1c2d3", "a0b1c2d3e4"]); },});
Deno.test({ name: "[collections/runningReduce] reduce array of objects with currentIndex", fn() { const medals = [ { country: "USA", count: 113 }, { country: "CHN", count: 88 }, { country: "JPN", count: 58 }, ]; const result = runningReduce( medals, (_, current, currentIndex) => { return currentIndex + current.country; }, "", );
assertEquals(result, ["0USA", "1CHN", "2JPN"]); },});
std

Version Info

Tagged at
a year ago