mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2026-01-10 20:12:48 +00:00
33 lines
901 B
TypeScript
33 lines
901 B
TypeScript
export class SemVer {
|
|
major = 0;
|
|
minor = 0;
|
|
patch = 0;
|
|
preRelease?: string;
|
|
meta?: string;
|
|
|
|
constructor(versionString: string) {
|
|
const result =
|
|
/^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+([0-9A-Za-z-]+))?$/.exec(
|
|
versionString,
|
|
);
|
|
if (!result) {
|
|
console.error("Invalid version string:", versionString);
|
|
} else {
|
|
const [, major, minor, patch, preRelease, meta] = result;
|
|
this.major = Number.parseInt(major ?? "NaN");
|
|
this.minor = Number.parseInt(minor ?? "NaN");
|
|
this.patch = Number.parseInt(patch ?? "NaN");
|
|
if (preRelease) this.preRelease = preRelease;
|
|
if (meta) this.meta = meta;
|
|
}
|
|
}
|
|
|
|
toString() {
|
|
return (
|
|
`${this.major}.${this.minor}.${this.patch}` +
|
|
(this.preRelease ? `-${this.preRelease}` : "") +
|
|
(this.meta ? `+${this.meta}` : "")
|
|
);
|
|
}
|
|
}
|