Skip to content

Instantly share code, notes, and snippets.

@Arathi
Created November 30, 2023 02:19
Show Gist options
  • Save Arathi/e9c1b0a6969a931fce768b0b13a9a7f1 to your computer and use it in GitHub Desktop.
Save Arathi/e9c1b0a6969a931fce768b0b13a9a7f1 to your computer and use it in GitHub Desktop.
版本号
const regexVersion = /(\d+)\.(\d+)(\.(\d+))?(-([0-9A-Za-z]+))?(\+([0-9A-Za-z]+))?/;
class Version {
major: number = 0;
minor: number = 0;
patch?: number;
preRelease?: string;
build?: string;
constructor(
major: number,
minor: number,
patch?: number,
preRelease?: string,
build?: string
) {
this.major = major;
this.minor = minor;
this.patch = patch;
this.preRelease = preRelease;
this.build = build;
}
get value(): number {
let value = 0;
value += this.major * 100000;
value += this.minor * 1000;
if (this.patch != undefined) value += this.patch * 10;
if (this.preRelease == undefined) value += 1;
return value;
}
toString(): string {
let version = `${this.major}.${this.minor}`;
if (this.patch != undefined) version += `.${this.patch}`;
if (this.preRelease != undefined) version += `-${this.preRelease}`;
if (this.build != undefined) version += `+${this.build}`;
return version;
}
}
const vs = regexVersion.exec("1.20.1-Snapshot");
if (vs != null) {
const version = new Version(
Number(vs[1]),
Number(vs[2]),
vs[4] != undefined ? Number(vs[4]) : undefined,
vs[6],
vs[8]
);
console.log(version.value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment