//版本比较大小,返回true就是ver1比ver2
//isJudgeEqual是表示是否需要判断相等
function versionCompare (ver1,ver2,isJudgeEqual = false) {
// 将两个版本号拆成数组
let verArr1 = ver1.split('.');
let verArr2 = ver2.split('.');
//取长度最长的
let maxLen = Math.max(verArr1.length, verArr2.length);
let pos = 0; // 当前比较位
let diff = 0; // 当前位比较是否相等
// 逐个比较如果当前位相等则继续比较下一位
while (pos < maxLen) {
diff = parseInt(verArr1[pos]?verArr1[pos]:0) - parseInt(verArr2[pos]?verArr2[pos]:0);
if (diff != 0) {
break;
}
pos++;
}
if(isJudgeEqual){
return diff >= 0;
}else{
return diff > 0;
}
}
js版本比较
最新推荐文章于 2024-07-01 11:42:33 发布