比较版本号大小

const compareVersion = (source = '10.2.3.beta', target = '10.2.13') => {
    const targetArray = target.split('.')
    const sourceArray = source.split('.')
    const len = targetArray.length > sourceArray.length ? targetArray.length : sourceArray.length
    const getValue = (value) => {
        return value ? Number.isNaN(Number(value)) ? value : Number(value) : ''
    }
    for (let i = 0; i < len; i++) {
        const tv = getValue(targetArray[i])
        const sv = getValue(sourceArray[i])
        if (sv > tv) {
            return -1
        } else if (sv < tv) {
            return 1
        }
    }
    return 0
}

  

posted @ 2020-08-14 21:28  671_MrSix  阅读(159)  评论(0编辑  收藏  举报