-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathversion-utils.ts
46 lines (36 loc) · 1.57 KB
/
version-utils.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
import compareVersions from "compare-versions";
export class VersionUtils {
public static isValidVersion = (version: string): boolean => {
return compareVersions.validate(version);
}
public static isLatestVersionKeyword = (version: string): boolean => {
return version === "latest";
}
public static isVersionsEqual = (firstVersion: string, secondVersion: string): boolean => {
return compareVersions.compare(firstVersion, secondVersion, "=");
}
public static sortVersions = (versions: string[]): string[] => {
return [...versions].sort(compareVersions).reverse();
}
public static normalizeVersion = (version: string): string => {
const versionParts = VersionUtils.splitVersionToParts(version);
while (versionParts.length < 4) {
versionParts.push("x");
}
return VersionUtils.buildVersionFromParts(versionParts);
}
public static countVersionLength = (version: string): number => {
return VersionUtils.splitVersionToParts(version).length;
}
public static cutVersionLength = (version: string, newLength: number): string => {
const versionParts = VersionUtils.splitVersionToParts(version);
const newParts = versionParts.slice(0, newLength);
return VersionUtils.buildVersionFromParts(newParts);
}
private static splitVersionToParts = (version: string): string[] => {
return version.split(".");
}
private static buildVersionFromParts = (versionParts: string[]): string => {
return versionParts.join(".");
}
}