-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
34 lines (28 loc) · 783 Bytes
/
index.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
type CompareResult = number
type CompareFunction<T> = (a: T, b: T) => CompareResult
export function compareStrings(sA: string, sB: string) {
return sA > sB ? 1 : sA < sB ? -1 : 0
}
export function compareNumbers(sA: number, sB: number) {
return sA - sB
}
export function reverse(r: CompareResult) {
return -r
}
export function compareStringsCaseInsensitive(sA: string, sB: string) {
return sA.localeCompare(sB, undefined, {
sensitivity: 'base',
}) as CompareResult
}
export function sortFunction<T>(
gen: (a: T, b: T) => IterableIterator<CompareResult>
): CompareFunction<T> {
return (a: T, b: T) => {
for (let comp of gen(a, b)) {
if (comp) {
return comp
}
}
return 0
}
}