-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
this type allows pretty printing, version comparison, and runtime checks, to detect such errors as the one fixed in the previous commit also use the proper compare methods, where we previously did it by hand
- Loading branch information
Showing
8 changed files
with
127 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
export type VersionArray = [number, number, number]; | ||
|
||
const versionNames = ["major", "minor", "patch"] as const; | ||
|
||
export class Version { | ||
constructor(private readonly version: VersionArray) { | ||
const isValid = Version.isValidVersion(this.version); | ||
|
||
if (isValid !== true) { | ||
throw isValid; | ||
} | ||
} | ||
|
||
/** This checks if any type is a valid version "object" at runtime | ||
* | ||
* @param version the version toc heck | ||
*/ | ||
private static isValidVersion(version: Version | any): true | Error { | ||
if (Array.isArray(version)) { | ||
return new Error("Version object is not an Array"); | ||
} | ||
|
||
if (version.length !== 3) { | ||
if (Array.isArray(version)) { | ||
return new Error(`Version array has ${version.length} entries, but expected 3`); | ||
} | ||
} | ||
|
||
for (const index in version as VersionArray) { | ||
const num = version[index]; | ||
if (!Number.isInteger(num)) { | ||
const name = versionNames[index]; | ||
return new Error(`${name} version component is not a number: '${num}'`); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** This compares two versions | ||
* - if the first one is bigger, a value > 0 is returned | ||
* - if they are the same, 0 is returned | ||
* - if the first one is smaller, a value < 0 is returned | ||
* @param version1 | ||
* @param version2 | ||
*/ | ||
private static compareImpl([major1, minor1, patch1]: VersionArray, [major2, minor2, patch2]: VersionArray): number { | ||
if (major1 !== major2) { | ||
return major1 - major2; | ||
} | ||
|
||
if (minor1 !== minor2) { | ||
return minor1 - minor2; | ||
} | ||
|
||
return patch1 - patch2; | ||
} | ||
|
||
compareWithOther(otherVersion: Version): number { | ||
return Version.compareImpl(this.version, otherVersion.version); | ||
} | ||
|
||
compare(otherVersion: VersionArray): number { | ||
return Version.compareImpl(this.version, otherVersion); | ||
} | ||
|
||
private static versionToString([major, minor, patch]: VersionArray): string { | ||
return `${major}.${minor}.${patch}`; | ||
} | ||
|
||
toString(): string { | ||
return Version.versionToString(this.version); | ||
} | ||
} |