2021-04-21 02:38:00 -05:00
|
|
|
import { isNumber } from 'lodash';
|
2017-10-20 08:25:19 -05:00
|
|
|
|
|
|
|
const versionPattern = /^(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-([0-9A-Za-z\.]+))?/;
|
2017-10-20 07:50:44 -05:00
|
|
|
|
|
|
|
export class SemVersion {
|
|
|
|
major: number;
|
|
|
|
minor: number;
|
|
|
|
patch: number;
|
|
|
|
meta: string;
|
|
|
|
|
|
|
|
constructor(version: string) {
|
2021-04-15 07:21:06 -05:00
|
|
|
this.major = 0;
|
|
|
|
this.minor = 0;
|
|
|
|
this.patch = 0;
|
|
|
|
this.meta = '';
|
2018-08-26 10:14:40 -05:00
|
|
|
const match = versionPattern.exec(version);
|
2017-10-20 07:50:44 -05:00
|
|
|
if (match) {
|
|
|
|
this.major = Number(match[1]);
|
|
|
|
this.minor = Number(match[2] || 0);
|
|
|
|
this.patch = Number(match[3] || 0);
|
|
|
|
this.meta = match[4];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isGtOrEq(version: string): boolean {
|
2018-08-26 10:14:40 -05:00
|
|
|
const compared = new SemVersion(version);
|
2019-02-06 10:59:28 -06:00
|
|
|
|
|
|
|
for (let i = 0; i < this.comparable.length; ++i) {
|
|
|
|
if (this.comparable[i] > compared.comparable[i]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (this.comparable[i] < compared.comparable[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2017-10-20 07:50:44 -05:00
|
|
|
}
|
2017-10-20 08:25:19 -05:00
|
|
|
|
|
|
|
isValid(): boolean {
|
2021-04-21 02:38:00 -05:00
|
|
|
return isNumber(this.major);
|
2017-10-20 08:25:19 -05:00
|
|
|
}
|
2019-02-06 10:59:28 -06:00
|
|
|
|
|
|
|
get comparable() {
|
|
|
|
return [this.major, this.minor, this.patch];
|
|
|
|
}
|
2017-10-20 07:50:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isVersionGtOrEq(a: string, b: string): boolean {
|
2018-09-03 04:00:46 -05:00
|
|
|
const aSemver = new SemVersion(a);
|
|
|
|
return aSemver.isGtOrEq(b);
|
2017-10-20 07:50:44 -05:00
|
|
|
}
|