-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathcomparators.ts
More file actions
52 lines (48 loc) · 1.56 KB
/
Copy pathcomparators.ts
File metadata and controls
52 lines (48 loc) · 1.56 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
import type { Logger } from '../logger';
import { getNumericPrefix } from './fileNameUtils';
export function localeCompareStringsNumerically(a: string, b: string): number {
return a.localeCompare(b, undefined, {
usage: 'sort',
numeric: true,
sensitivity: 'variant',
ignorePunctuation: true,
});
}
/**
* Compares two file names by their numeric prefix.
* 17 digit numbers are interpreted as UTC date and converted to the number
* representation of that date. 1...4 digit numbers are interpreted as index
* based naming scheme.
*
* @param a the first file name to compare
* @param b the second file name to compare
* @param logger the logger to use for logging
* @returns a negative value if `a` is less than `b`, 0 if they are equal, and a positive value if `a` is greater than `b`
*/
export function compareFileNamesByTimestamp(
a: string,
b: string,
logger?: Logger
): number {
const aTimestamp = getNumericPrefix(a, logger);
const bTimestamp = getNumericPrefix(b, logger);
return aTimestamp - bTimestamp;
}
/**
* Compares two file names by their numeric prefix and locale.
*
* @param a the first file name to compare
* @param b the second file name to compare
* @param logger the logger to use for logging
* @returns a negative value if `a` is less than `b`, 0 if they are equal, and a positive value if `a` is greater than `b`
*/
export function compareMigrationFileNames(
a: string,
b: string,
logger?: Logger
): number {
return (
compareFileNamesByTimestamp(a, b, logger) ||
localeCompareStringsNumerically(a, b)
);
}