TeaSpeak-Client/modules/shared/version/index.ts

85 lines
2.5 KiB
TypeScript
Raw Normal View History

2019-10-25 19:51:40 -04:00
export class Version {
2020-12-02 12:08:49 -05:00
major: number;
minor: number;
patch: number;
build: number;
timestamp: number;
2019-10-25 19:51:40 -04:00
constructor(major: number, minor: number, patch: number, build: number, timestamp: number) {
this.major = major;
this.minor = minor;
this.patch = patch;
this.build = build;
2020-12-02 12:08:49 -05:00
this.timestamp = timestamp;
2019-10-25 19:51:40 -04:00
}
toString(timestamp: boolean = false) {
let result = "";
result += this.major + ".";
result += this.minor + ".";
result += this.patch;
2020-12-02 12:08:49 -05:00
if(this.build > 0) {
2019-10-25 19:51:40 -04:00
result += "-" + this.build;
2020-12-02 12:08:49 -05:00
}
if(timestamp && this.timestamp > 0) {
2019-10-25 19:51:40 -04:00
result += " [" + this.timestamp + "]";
2020-12-02 12:08:49 -05:00
}
2019-10-25 19:51:40 -04:00
return result;
}
equals(other: Version) : boolean {
if(other == this) return true;
if(typeof(other) != typeof(this)) return false;
if(other.major != this.major) return false;
if(other.minor != this.minor) return false;
if(other.patch != this.patch) return false;
if(other.build != this.build) return false;
2020-12-02 12:08:49 -05:00
return other.timestamp == this.timestamp;
2019-10-25 19:51:40 -04:00
}
2020-12-02 12:08:49 -05:00
newerThan(other: Version) : boolean {
2019-10-25 19:51:40 -04:00
if(other.major > this.major) return false;
else if(other.major < this.major) return true;
if(other.minor > this.minor) return false;
else if(other.minor < this.minor) return true;
else if(other.patch < this.patch) return true;
if(other.patch > this.patch) return false;
if(other.build > this.build) return false;
else if(other.build < this.build) return true;
return false;
}
2020-12-02 12:08:49 -05:00
isDevelopmentVersion() : boolean {
2020-12-05 10:40:34 -05:00
return this.build == 0 && this.major == 0 && this.minor == 0 && this.patch == 0;
2019-10-25 19:51:40 -04:00
}
}
//1.0.0-2 [1000]
2020-12-02 12:08:49 -05:00
export function parseVersion(version: string) : Version {
2019-10-25 19:51:40 -04:00
let result: Version = new Version(0, 0, 0, 0, 0);
const roots = version.split(" ");
{
const parts = roots[0].split("-");
const numbers = parts[0].split(".");
if(numbers.length > 0) result.major = parseInt(numbers[0]);
if(numbers.length > 1) result.minor = parseInt(numbers[1]);
if(numbers.length > 2) result.patch = parseInt(numbers[2]);
if(parts.length > 1) result.build = parseInt(parts[1]);
}
if(roots.length > 1 && ((roots[1] = roots[1].trim()).startsWith("[") && roots[1].endsWith("]"))) {
result.timestamp = parseInt(roots[1].substr(1, roots[1].length - 2));
}
return result;
}