All files / rusted-chromium/commons ComparableVersion.ts

81.39% Statements 35/43
75.86% Branches 22/29
70% Functions 7/10
91.17% Lines 31/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 944x   4x   4x               234x       88x       88x       84x             85x 4x 4x 4x 4x 81x 26x 26x 26x 26x   55x   55x 55x 55x 55x         80x                         50x 27x   2x 2x   2x 2x   1x 1x                                    
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 }
    
        Iif (version.minor > other.minor) { return Compared.GREATER }
        Iif (version.minor < other.minor) { return Compared.LESS }
    
        Iif (version.branch > other.branch) { return Compared.GREATER }
        if (version.branch < other.branch) { return Compared.LESS }
    
        Iif (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,
        })
    }
}