Files
taimed-international-app/uni_modules/uni-upgrade-center-app/utils/tools.js

32 lines
751 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 比较版本号
* @param {string} v1 - 版本号1
* @param {string} v2 - 版本号2
* @returns {number} - 1表示v1大于v2-1表示v1小于v20表示相等
*/
export function compareVersion(v1, v2) {
const arr1 = v1.split('.').map(Number)
const arr2 = v2.split('.').map(Number)
const maxLen = Math.max(arr1.length, arr2.length)
for (let i = 0; i < maxLen; i++) {
const n1 = arr1[i] ?? 0
const n2 = arr2[i] ?? 0
if (n1 > n2) return 1
if (n1 < n2) return -1
}
return 0
}
/**
* 获取较大版本号
* @param {string} v1 - 版本号1
* @param {string} v2 - 版本号2
* @returns {string} - 较大的版本号
*/
export function getMaxVersion(v1, v2) {
return compareVersion(v1, v2) >= 0 ? v1 : v2
}