-
Notifications
You must be signed in to change notification settings - Fork 1
/
version-utils.ts
32 lines (26 loc) · 978 Bytes
/
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
import semver from 'semver';
export function isStableSemver(value: string) {
return semver.parse(value)?.prerelease.length === 0 || false;
}
function isValidAndStableSemver(value: string) {
const parsed = semver.parse(value);
if (!parsed) return false;
return isStableSemver(value);
}
export function isValidSemVer(value: string) {
return semver.valid(value) !== null;
}
export function canCoerceAsSemver(value: string | null | undefined) {
if (!value) return false;
return semver.valid(semver.coerce(value)) !== null;
}
export function getMajor(value: string) {
if (!isValidAndStableSemver(value))
throw new TypeError(`Tag [${value}] doesn't satisfy semantic versioning specification`);
return value.split('.')[0];
}
export function getMajorAndMinor(value: string) {
if (!isValidAndStableSemver(value))
throw new TypeError(`Tag [${value}] doesn't satisfy semantic versioning specification`);
return value.split('.').slice(0, 2).join('.');
}