Total Complexity | 25 |
Complexity/F | 5 |
Lines of Code | 60 |
Function Count | 5 |
Duplicated Lines | 0 |
Ratio | 0 % |
Coverage | 100% |
Changes | 0 |
1 | // Comparison functions for different data types |
||
2 | 1 | export function compareSortablePrimitives( |
|
3 | a: unknown, |
||
4 | b: unknown, |
||
5 | ascending: boolean |
||
6 | ): number { |
||
7 | 168 | if (typeof a === 'string' && typeof b === 'string') { |
|
8 | 40 | return ascending ? a.localeCompare(b) : b.localeCompare(a); |
|
9 | } |
||
10 | |||
11 | 128 | if (typeof a === 'number' && typeof b === 'number') { |
|
12 | 83 | return compareNumbers(a, b, ascending); |
|
13 | } |
||
14 | |||
15 | 45 | if (typeof a === 'boolean' && typeof b === 'boolean') { |
|
16 | 21 | return compareBooleans(a, b, ascending); |
|
17 | } |
||
18 | |||
19 | 24 | return 0; // Maintain order for other primitives |
|
20 | } |
||
21 | |||
22 | 1 | export function compareNumbers( |
|
23 | a: number, |
||
24 | b: number, |
||
25 | ascending: boolean |
||
26 | ): number { |
||
27 | 83 | if (Number.isNaN(a) && Number.isNaN(b)) return 0; |
|
28 | 81 | if (Number.isNaN(a)) return 1; |
|
29 | 77 | if (Number.isNaN(b)) return -1; |
|
30 | |||
31 | 71 | return ascending ? a - b : b - a; |
|
32 | } |
||
33 | |||
34 | 1 | export function compareBooleans( |
|
35 | a: boolean, |
||
36 | b: boolean, |
||
37 | ascending: boolean |
||
38 | ): number { |
||
39 | 21 | if (a === b) return 0; |
|
40 | 13 | if (a) return ascending ? 1 : -1; |
|
41 | 6 | return ascending ? -1 : 1; |
|
42 | } |
||
43 | |||
44 | 1 | export function compareObjectKeys( |
|
45 | keyA: string | symbol, |
||
46 | keyB: string | symbol, |
||
47 | ascending: boolean |
||
48 | ): number { |
||
49 | 102 | if (typeof keyA === 'symbol' && typeof keyB === 'symbol') return 0; |
|
50 | 100 | if (typeof keyA === 'symbol') return 1; |
|
51 | 95 | if (typeof keyB === 'symbol') return -1; |
|
52 | |||
53 | 95 | const stringA = keyA as string; |
|
54 | 95 | const stringB = keyB as string; |
|
55 | |||
56 | 95 | return ascending |
|
57 | ? stringA.localeCompare(stringB) |
||
58 | : stringB.localeCompare(stringA); |
||
59 | } |
||
60 |