-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.test.ts
More file actions
49 lines (35 loc) · 1.45 KB
/
sort.test.ts
File metadata and controls
49 lines (35 loc) · 1.45 KB
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
import { describe, expect, it } from 'vitest';
import { byTimestamp, byLocaleCompare, bySimpleComparison, bySubtraction } from '../src/sort.js';
describe('byLocaleCompare()', () => {
it('compares by string localeCompare', () => {
expect(['c', 'b', 'a'].sort(byLocaleCompare)).toEqual(['a', 'b', 'c']);
});
});
describe('byTimestamp()', () => {
it('sorts by timestamp', () => {
const now = new Date();
const past = new Date();
past.setDate(past.getDate() - 10);
const data = [now, 0, past, now.toISOString(), past.toISOString(), 1];
expect(data.sort(byTimestamp)).toEqual([0, 1, past, past.toISOString(), now, now.toISOString()]);
});
it('Handles bad dates', () => {
const now = new Date();
const bad = Date.parse('not a date');
const data = [now, bad];
expect(data.sort(byTimestamp)).toEqual([bad, now]);
});
});
describe('bySimpleComparison()', () => {
it('compares using operators', () => {
expect([1, 0, 3, 2].sort(bySimpleComparison)).toEqual([0, 1, 2, 3]);
expect([true, false, true].sort(bySimpleComparison)).toEqual([false, true, true]);
expect(['c', 'b', 1, 'a', 'x', 'z', 'y', 1].sort(bySimpleComparison)).toEqual(['a', 1, 1, 'b', 'c', 'x', 'y', 'z']);
});
});
describe('bySubtraction()', () => {
it('compares using subtraction', () => {
expect([1, 0, 3, 2].sort(bySubtraction)).toEqual([0, 1, 2, 3]);
expect(['1', 0n, 3, 2].sort(bySubtraction)).toEqual([0n, '1', 2, 3]);
});
});