deno.land / std@0.177.1 / collections / sort_by_test.ts

sort_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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";import { sortBy } from "./sort_by.ts";
Deno.test({ name: "[collections/sortBy] no mutation", fn() { const array = ["a", "abc", "ba"]; sortBy(array, (it) => it.length);
assertEquals(array, ["a", "abc", "ba"]); },});
Deno.test({ name: "[collections/sortBy] calls the selector function once", fn() { let callCount = 0; const array = [0, 1, 2]; sortBy(array, (it) => { callCount++; return it; });
assertEquals(callCount, array.length); },});
Deno.test({ name: "[collections/sortBy] empty input", fn() { assertEquals(sortBy([], () => 5), []); },});
Deno.test({ name: "[collections/sortBy] identity selector", fn() { assertEquals(sortBy([2, 3, 1], (it) => it), [1, 2, 3]); },});
Deno.test({ name: "[collections/sortBy] stable sort", fn() { assertEquals( sortBy([ { id: 1, date: "February 1, 2022" }, { id: 2, date: "December 17, 1995" }, { id: 3, date: "June 12, 2012" }, { id: 4, date: "December 17, 1995" }, { id: 5, date: "June 12, 2012" }, ], (it) => new Date(it.date)), [ { id: 2, date: "December 17, 1995" }, { id: 4, date: "December 17, 1995" }, { id: 3, date: "June 12, 2012" }, { id: 5, date: "June 12, 2012" }, { id: 1, date: "February 1, 2022" }, ], );
assertEquals( sortBy([ { id: 1, str: "c" }, { id: 2, str: "a" }, { id: 3, str: "b" }, { id: 4, str: "a" }, { id: 5, str: "b" }, ], (it) => it.str), [ { id: 2, str: "a" }, { id: 4, str: "a" }, { id: 3, str: "b" }, { id: 5, str: "b" }, { id: 1, str: "c" }, ], ); },});
Deno.test({ name: "[collections/sortBy] special number values", fn() { assertEquals( sortBy([ 1, Number.POSITIVE_INFINITY, 2, Number.NEGATIVE_INFINITY, 3, Number.NaN, 4, Number.NaN, ], (it) => it), [ Number.NEGATIVE_INFINITY, 1, 2, 3, 4, Number.POSITIVE_INFINITY, Number.NaN, Number.NaN, ], );
assertEquals( sortBy([ Number.NaN, 1, Number.POSITIVE_INFINITY, Number.NaN, 7, Number.NEGATIVE_INFINITY, Number.NaN, 2, 6, 5, 9, ], (it) => it), [ Number.NEGATIVE_INFINITY, 1, 2, 5, 6, 7, 9, Number.POSITIVE_INFINITY, Number.NaN, Number.NaN, Number.NaN, ], );
// Test that NaN sort is stable. const nanArray = [ { id: 1, nan: Number.NaN }, { id: 2, nan: Number.NaN }, { id: 3, nan: Number.NaN }, { id: 4, nan: Number.NaN }, ]; assertEquals(sortBy(nanArray, ({ nan }) => nan), nanArray); },});
Deno.test({ name: "[collections/sortBy] sortings", fn() { const testArray = [ { name: "benchmark", stage: 3 }, { name: "test", stage: 2 }, { name: "build", stage: 1 }, { name: "deploy", stage: 4 }, ];
assertEquals(sortBy(testArray, (it) => it.stage), [ { name: "build", stage: 1 }, { name: "test", stage: 2 }, { name: "benchmark", stage: 3 }, { name: "deploy", stage: 4 }, ]);
assertEquals(sortBy(testArray, (it) => it.name), [ { name: "benchmark", stage: 3 }, { name: "build", stage: 1 }, { name: "deploy", stage: 4 }, { name: "test", stage: 2 }, ]);
assertEquals( sortBy([ "9007199254740999", "9007199254740991", "9007199254740995", ], (it) => BigInt(it)), [ "9007199254740991", "9007199254740995", "9007199254740999", ], );
assertEquals( sortBy([ "February 1, 2022", "December 17, 1995", "June 12, 2012", ], (it) => new Date(it)), [ "December 17, 1995", "June 12, 2012", "February 1, 2022", ], ); },});
std

Version Info

Tagged at
10 months ago