Blame view

apps/utils/version.js 786 Bytes
yyq authored
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
function toVersion(str = '') {
    const ver = str.split('.').map(i => Number(i));

    if (!ver[0]) {
        ver[0] = 0;
    }

    if (!ver[1]) {
        ver[1] = 0;
    }

    if (!ver[2]) {
        ver[2] = 0;
    }

    return ver;
}

function toNumber(ver) {
    const major = ver[0] * 10000;
    const minor = ver[1] * 100;
    const patch = ver[2] * 1;

    return major + minor + patch;
}

function compare(left, right) {
    if (left > right) {
        return 1;
    } else if (left < right) {
        return -1;
    } else {
        return 0;
    }
}

export default function (left, right) {
    const leftVersion = toNumber(toVersion(left));
    const rightVersion = toNumber(toVersion(right));
    const result = compare(leftVersion, rightVersion);

    return result;
};