/
index.ts
155 lines (128 loc) · 3.2 KB
/
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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { regEx } from '../../util/regex';
import type { NewValueConfig, VersioningApi } from '../types';
export const id = 'ubuntu';
export const displayName = 'Ubuntu';
export const urls = ['https://changelogs.ubuntu.com/meta-release'];
export const supportsRanges = false;
// #12509
const temporarilyUnstable = ['22.04'];
// validation
function isValid(input: string): string | boolean | null {
return (
typeof input === 'string' &&
regEx(/^(0[4-5]|[6-9]|[1-9][0-9])\.[0-9][0-9](\.[0-9]{1,2})?$/).test(input)
);
}
function isVersion(input: string): string | boolean | null {
return isValid(input);
}
function isCompatible(
version: string,
_range?: string
): string | boolean | null {
return isValid(version);
}
function isSingleVersion(version: string): string | boolean | null {
return isValid(version) ? true : null;
}
function isStable(version: string): boolean {
if (!isValid(version)) {
return false;
}
if (temporarilyUnstable.includes(version)) {
return false;
}
return regEx(/^\d?[02468]\.04/).test(version);
}
// digestion of version
function getMajor(version: string): null | number {
if (isValid(version)) {
const [major] = version.split('.') || [];
return parseInt(major, 10);
}
return null;
}
function getMinor(version: string): null | number {
if (isValid(version)) {
const [, minor] = version.split('.') || [];
return parseInt(minor, 10);
}
return null;
}
function getPatch(version: string): null | number {
if (isValid(version)) {
const [, , patch] = version.split('.') || [];
return patch ? parseInt(patch, 10) : null;
}
return null;
}
// comparison
function equals(version: string, other: string): boolean {
return isVersion(version) && isVersion(other) && version === other;
}
function isGreaterThan(version: string, other: string): boolean {
const xMajor = getMajor(version);
const yMajor = getMajor(other);
if (xMajor > yMajor) {
return true;
}
if (xMajor < yMajor) {
return false;
}
const xMinor = getMinor(version);
const yMinor = getMinor(other);
if (xMinor > yMinor) {
return true;
}
if (xMinor < yMinor) {
return false;
}
const xPatch = getPatch(version) || 0;
const yPatch = getPatch(other) || 0;
return xPatch > yPatch;
}
function getSatisfyingVersion(
versions: string[],
range: string
): string | null {
return versions.find((version) => equals(version, range)) ? range : null;
}
function minSatisfyingVersion(
versions: string[],
range: string
): string | null {
return getSatisfyingVersion(versions, range);
}
function getNewValue(newValueConfig: NewValueConfig): string {
return newValueConfig.newVersion;
}
function sortVersions(version: string, other: string): number {
if (equals(version, other)) {
return 0;
}
if (isGreaterThan(version, other)) {
return 1;
}
return -1;
}
function matches(version: string, range: string): boolean {
return equals(version, range);
}
export const api: VersioningApi = {
isCompatible,
isSingleVersion,
isStable,
isValid,
isVersion,
getMajor,
getMinor,
getPatch,
equals,
isGreaterThan,
getSatisfyingVersion,
minSatisfyingVersion,
getNewValue,
sortVersions,
matches,
};
export default api;