-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomparators.test.ts
More file actions
113 lines (90 loc) · 2.9 KB
/
comparators.test.ts
File metadata and controls
113 lines (90 loc) · 2.9 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
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
import {
byBoolean,
byCodeUnit,
byDate,
byNumber,
byString,
byStringWithoutCaseSensitivity,
} from './comparators';
describe('byNumber', () => {
it('compares by numeric value', () => {
expect(byNumber(5, 9000)).toBeLessThan(0);
expect(byNumber(9000, 5)).toBeGreaterThan(0);
});
it('defines equality', () => {
expect(byNumber(42, 42)).toBe(0);
});
it('sorts numbers correctly', () => {
const result = [1, 3, 5, 2, 4].sort(byNumber);
expect(result).toEqual([1, 2, 3, 4, 5]);
});
});
describe('byString', () => {
it('compares by lexicographic order', () => {
expect(byString('a', 'b')).toBeLessThan(0);
expect(byString('b', 'a')).toBeGreaterThan(0);
});
it('defines equality', () => {
expect(byString('foobar', 'foobar')).toBe(0);
});
it('sorts strings correctly', () => {
const input = ['B', 'b', 'A', 'a'];
const expected = [...input].sort((a, b) => a.localeCompare(b));
const result = [...input].sort(byString);
expect(result).toEqual(expected);
});
});
describe('byStringWithoutCaseSensitivity', () => {
it('compares by lexicographic order', () => {
expect(byString('a', 'B')).toBeLessThan(0);
expect(byString('B', 'a')).toBeGreaterThan(0);
});
it('defines equality', () => {
expect(byStringWithoutCaseSensitivity('a', 'A')).toBe(0);
});
});
describe('byCodeUnit', () => {
it('compares by code unit', () => {
expect(byCodeUnit('\x00', '\x01')).toBeLessThan(0);
expect(byCodeUnit('\x01', '\x00')).toBeGreaterThan(0);
});
it('defines equality', () => {
expect(byCodeUnit('\ud83d\udc96', '\ud83d\udc96')).toBe(0);
});
it('sorts code units correctly', () => {
const input = ['[', 'a', ']', ' ', '_', '^', 'Z'];
const result = [...input].sort(byCodeUnit);
expect(result).toEqual([' ', 'Z', '[', ']', '^', '_', 'a']);
});
});
describe('byDate', () => {
const oneDay = 24 * 60 * 60 * 1000;
const today = new Date();
const yesterday = new Date(+today - oneDay);
const tomorrow = new Date(+today + oneDay);
it('compares by chronological order', () => {
expect(byDate(yesterday, today)).toBeLessThan(0);
expect(byDate(tomorrow, today)).toBeGreaterThan(0);
});
it('defines equality', () => {
expect(byDate(today, today)).toBe(0);
});
it('sorts dates correctly', () => {
const result = [tomorrow, yesterday, today].sort(byDate);
expect(result).toEqual([yesterday, today, tomorrow]);
});
});
describe('byBoolean', () => {
it('compares by truth', () => {
expect(byBoolean(false, true)).toBeLessThan(0);
expect(byBoolean(true, false)).toBeGreaterThan(0);
});
it('defines equality', () => {
expect(byBoolean(false, false)).toBe(0);
expect(byBoolean(true, true)).toBe(0);
});
it('sorts booleans correctly', () => {
const result = [false, true, false, true, false].sort(byBoolean);
expect(result).toEqual([false, false, false, true, true]);
});
});