4 constructor(version: string) {
5 this.version = this.getSemanticVersion(version);
8 private getSemanticVersion(version: string): number[] {
9 const array = version.split('.').map(Number);
10 // remove trailing .0 if any
11 while (array.at(-1) === 0) {
18 * Compares the version number stored in this instance with a given version number string.
20 * @param {string} comparison - The version number to compare against in the format 'major.minor.patch'.
21 * @returns {-1 | 0 | 1} - Returns -1 if this version is lower, 1 if it is higher, or 0 if they are equal.
23 private compare(comparison: string): -1 | 0 | 1 {
24 const comparedVersion = this.getSemanticVersion(comparison);
26 // Loop through each segment of the version numbers.
27 for (let i = 0; true; i++) {
28 // If the end of both versions is reached simultaneously, they are equal.
29 if (i >= comparedVersion.length) {
30 return i >= this.version.length ? 0 : 1;
32 // If this version ends before the compared version, this version is lower.
33 if (i >= this.version.length) {
36 // Calculate the difference between the same segment of the two versions.
37 const diff = this.version[i] - comparedVersion[i];
38 // If there is a difference, determine which version is greater.
40 return diff > 0 ? 1 : -1;
45 public isEqualTo(comparison: string): boolean {
46 return this.compare(comparison) === 0;
49 public isGreaterThan(comparison: string): boolean {
50 return this.compare(comparison) === 1;
53 public isSmallerThan(comparison: string): boolean {
54 return this.compare(comparison) === -1;
57 public isGreaterThanOrEqual(comparison: string): boolean {
58 return this.isGreaterThan(comparison) || this.isEqualTo(comparison);
61 public isSmallerThanOrEqual(comparison: string): boolean {
62 return !this.isGreaterThan(comparison) || this.isEqualTo(comparison);