如果可以实现记得点赞分享,谢谢老铁~
/**版本对比 */
export const changeVersion = (a: any, b: any) => {
// 获取数字
function toNumber(num: { toString: () => any }) {
const n = num.toString();
const n_arr = n.split(/\D/);
if (n_arr[n_arr.length - 1] == '') {
n_arr.pop();
}
const n_replace = ['', '0', '00', '000', '0000'];
const r_n_replace = n_replace.reverse();
for (let i = 0; i < n_arr.length; i++) {
let l = n_arr[i].length;
n_arr[i] = r_n_replace[i] + n_arr[i];
}
const res = n_arr.join('');
return Number(res);
}
// 获取字母
function toString(str: { toString: () => any }) {
const s = str.toString();
const reg = s.match(/[a-zA-Z]/g);
if (reg !== null) {
return reg;
} else {
return s;
}
}
let a1: any = toNumber(a);
const a2: any = toString(a);
const b1: any = toNumber(b);
const b2: any = toString(b);
if (a1 < b1) {
return b;
} else if (a1 > b1) {
return a;
} else if ((a1 = b1)) {
// 版本号相同时,对比字母,将其转为16进制,进行对比
let a2_str = a2 && a2[0].toString(16);
const b2_str = b2 && b2[0].toString(16);
if (a2_str < b2_str) {
return b;
} else if (a2_str > b2_str) {
return a;
} else if ((a2_str = b2_str)) {
console.log('两个版本号相同!最新版本号为:' + a);
return a;
}
}
};