@wocker/ws
Version:
Docker workspace for web projects
60 lines (59 loc) • 1.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Version = void 0;
class Version {
constructor(data) {
Object.assign(this, data);
}
parts() {
return [
this.major,
this.minor,
this.patch,
this.build ?? 0
];
}
compare(version) {
if (typeof version === "string") {
version = Version.parse(version);
}
const a = this.parts(), b = version.parts();
for (let i = 0; i < Math.max(a.length, b.length); i++) {
if (i === 3) {
if (this.tag && !version.tag)
return -1;
else if (!this.tag && version.tag)
return 1;
else if (this.tag !== version.tag)
return this.tag < version.tag ? -1 : 1;
}
const diff = (a[i] ?? 0) - (b[i] ?? 0);
if (diff !== 0) {
return diff > 0 ? 1 : -1;
}
}
return 0;
}
static valid(version) {
return Version.REGEXP.test(version);
}
static parse(version) {
if (!Version.cache.has(version)) {
if (!Version.REGEXP.test(version)) {
throw new RangeError("Invalid version format");
}
const [, major, minor, patch, tag, build] = Version.REGEXP.exec(version) || [];
Version.cache.set(version, new Version({
major: parseInt(major),
minor: parseInt(minor),
patch: parseInt(patch),
tag,
build: build ? parseInt(build) : undefined
}));
}
return Version.cache.get(version);
}
}
exports.Version = Version;
Version.REGEXP = /^v?(\d+)\.(\d+)\.(\d+)(?:-([a-zA-Z]+(?:-[a-zA-Z0-9]+)*)(?:\.(\d+))?)?/;
Version.cache = new Map();