Skip to content

Instantly share code, notes, and snippets.

@yoshixmk
Created June 4, 2020 16:17
Show Gist options
  • Save yoshixmk/166e11186d20c77cc7dc9d32e9fd407b to your computer and use it in GitHub Desktop.
Save yoshixmk/166e11186d20c77cc7dc9d32e9fd407b to your computer and use it in GitHub Desktop.
jamashita
export interface IVersion<T extends IVersion<T>> {
// greater than
gt(other: T): boolean;
// greater than or equals
gte(other: T): boolean;
// less than
lt(other: T): boolean;
// less than or equals
lte(other: T): boolean;
// equals
eq(other: T): boolean;
}
class Semver implements IVersion<Semver> {
private mager: number;
private minar: number;
private patch: number;
private constructor(mager: number, minar: number, patch: number) {
this.mager = mager;
this.minar = minar;
this.patch = patch;
}
public static ofString(str: string): Semver {
const versions: string[] = str.split(".");
if (versions === undefined || versions.length !== 3) {
throw Error("Invalid parameter");
}
return new Semver(
Number.parseInt(versions[0]),
Number.parseInt(versions[1]),
Number.parseInt(versions[2]),
);
}
gt(other: Semver): boolean {
if (this.mager > other.mager) {
return true;
}
if (this.minar > other.minar) {
return true;
}
return this.patch > other.patch;
}
gte(other: Semver): boolean {
if (this.gt(other)) {
return true;
}
return this.eq(other);
}
lt(other: Semver): boolean {
if (this.mager < other.mager) {
return true;
}
if (this.minar < other.minar) {
return true;
}
return this.patch < other.patch;
}
lte(other: Semver): boolean {
if (this.lt(other)) {
return true;
}
return this.eq(other);
}
eq(other: Semver): boolean {
return this == other;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment