-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.ts
67 lines (63 loc) · 2 KB
/
version.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
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
/*
* Version object and comparison.
*/
// Local imports
import { convertUndefinedToCustomValue } from "./other/types";
// Type imports
import type { ComparatorValues } from "./other/genericStringSorter";
import type { DeepReadonly } from "./other/types";
/**
* The Version structure.
*/
export interface Version {
beta?: boolean;
major: number;
minor: number;
patch: number;
}
/**
* @param version The version.
* @param prefix The prefix for the string.
* @returns Version string.
*/
export const getVersionString = (
version: DeepReadonly<Version>,
prefix = "v"
): string =>
`${prefix}${version.major}.${version.minor}.${version.patch}${
version.beta ? "b" : ""
}`;
/**
* @param versionA Version A.
* @param versionB Version B.
* @returns Return 0 if the same version, 1 if version A is newer, -1 if version
* B newer.
*/
export const compareVersions = (
versionA: DeepReadonly<Version>,
versionB: DeepReadonly<Version>
): ComparatorValues => {
const aOlder =
versionA.major < versionB.major ||
(versionA.major === versionB.major && versionA.minor < versionB.minor) ||
(versionA.major === versionB.major &&
versionA.minor === versionB.minor &&
versionA.patch < versionB.patch) ||
(versionA.major === versionB.major &&
versionA.minor === versionB.minor &&
versionA.patch === versionB.patch &&
convertUndefinedToCustomValue(versionA.beta, false) >
convertUndefinedToCustomValue(versionB.beta, false));
const aNewer =
versionA.major > versionB.major ||
(versionA.major === versionB.major && versionA.minor > versionB.minor) ||
(versionA.major === versionB.major &&
versionA.minor === versionB.minor &&
versionA.patch > versionB.patch) ||
(versionA.major === versionB.major &&
versionA.minor === versionB.minor &&
versionA.patch === versionB.patch &&
convertUndefinedToCustomValue(versionA.beta, false) <
convertUndefinedToCustomValue(versionB.beta, false));
return aOlder ? -1 : aNewer ? 1 : 0;
};