All files / rusted-chromium/commons ComparableVersion.ts

100% Statements 43/43
100% Branches 29/29
100% Functions 10/10
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 9435x   35x   35x               794x       452x       408x       360x             701x 84x 84x 84x 84x 617x 405x 405x 405x 405x   212x   212x 212x 212x 212x         262x                         154x 99x   45x 38x   34x 27x   23x 14x   8x       17x       2x                
import { Compared } from '../interfaces/enums'
import type { IVersion } from '../interfaces/interfaces'
import { isIVersion } from '../utils/typeguards'
 
export class ComparableVersion implements IVersion {
 
    private _major: number
    private _minor: number
    private _branch: number
    private _patch: number
 
    public get major(): number {
        return this._major
    }
 
    public get minor(): number {
        return this._minor
    }
 
    public get branch(): number {
        return this._branch
    }
 
    public get patch(): number {
        return this._patch
    }
 
    public constructor(input: string)
    public constructor(major: number, minor: number, branch: number, patch: number)
    public constructor({ major, minor, branch, patch }: IVersion)
    public constructor(majorInput: number | string | IVersion, minor?: number, branch?: number, patch?: number) {
        if (isIVersion(majorInput)) {
            this._major = majorInput.major
            this._minor = majorInput.minor
            this._branch = majorInput.branch
            this._patch = majorInput.patch
        } else if (typeof majorInput === 'number') {
            this._major = majorInput
            this._minor = minor || 0
            this._branch = branch || 0
            this._patch = patch || 0
        } else {
            const splitVersion = majorInput.split('.')
 
            this._major = parseInt(splitVersion[0], 10) || 0
            this._minor = parseInt(splitVersion[1], 10) || 0
            this._branch = parseInt(splitVersion[2], 10) || 0
            this._patch = parseInt(splitVersion[3], 10) || 0
        }
    }
 
    public toString(): string {
        return `${this.major}.${this.minor}.${this.branch}.${this.patch}`
    }
 
    /**
     * Compares two ComparableVersions with each other.
     * if version < other, the result is Compared.LESS
     * if version > other, the result is Compared.GREATER
     * if version === other, the result is Compared.EQUAL
     * 
     * @param version 
     * @param other 
     */
    public static compare(version: ComparableVersion, other: ComparableVersion): Compared {
        if (version.major > other.major) { return Compared.GREATER }
        if (version.major < other.major) { return Compared.LESS }
    
        if (version.minor > other.minor) { return Compared.GREATER }
        if (version.minor < other.minor) { return Compared.LESS }
    
        if (version.branch > other.branch) { return Compared.GREATER }
        if (version.branch < other.branch) { return Compared.LESS }
    
        if (version.patch > other.patch) { return Compared.GREATER }
        if (version.patch < other.patch) { return Compared.LESS }
    
        return Compared.EQUAL
    }
 
    public static max(...versions: ComparableVersion[]): ComparableVersion {
        return versions.reduce((currentMax, version) => ComparableVersion.compare(currentMax, version) === Compared.LESS ? version : currentMax)
    }
 
    public static nextMajorVersion(version: ComparableVersion, plus = 1): ComparableVersion {
        return new ComparableVersion({
            major: version.major + plus,
            minor: 0,
            branch: 0,
            patch: 0,
        })
    }
}