js 版本比较

2018-10-27  本文已影响3人  Zszen

如果当前版本大于等于需求版本则返回true,如果当前版本小于需求版本则返回false

public requireVersion(nowVersion:string,requestVersion:string){
    let reg=/(\d+)\.(\d+)\.(\d+)/;
    let nowArr=[];
    let requestArr=[];
    //参数检查
    try{
        if(nowVersion.match(reg).length !== 4 || requestVersion.match(reg).length !== 4){
        console.log("Error compareVersion","param");
        return ;
    }
    }
    catch(e){
        console.log("Error compareVersion","param");
        return ;
    }
    
    nowArr = nowVersion.match(reg).slice(1,4);
    requestArr = requestVersion.match(reg).slice(1,4);
    
    //console.log(nowArr,requestArr);
    for(let i = 0;i < 3;i++){
        //console.log(i);
        if(nowArr[i] < requestArr[i]){
        //console.log("requestVersion is bigger.");
            return false;
        }else if(nowArr[i] > requestArr[i]){
            return true;
        }      
    }
    return true;
}

https://blog.csdn.net/alwxkxk/article/details/72579747

上一篇下一篇

猜你喜欢

热点阅读