UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

52 lines (48 loc) 1.06 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); /** * 版本比较 * @param {string} v1 第一个版本 * @param {string} v2 第二个版本 * @returns 比较结果,1 前者大,-1 后者大,0 二者相同 * @example * ```ts * compareVersion('1.1.1', '1.2.1') * // -1 * ``` */ function compareVersion(v1, v2) { if (v1 === void 0) { v1 = ''; } if (v2 === void 0) { v2 = ''; } var v1List = trimVersion(v1).split('.'); var v2List = trimVersion(v2).split('.'); var len = Math.max(v1List.length, v2List.length); while (v1List.length < len) { v1List.push('0'); } while (v2List.length < len) { v2List.push('0'); } for (var i = 0; i < len; i++) { var num1 = parseInt(v1List[i], 10); var num2 = parseInt(v2List[i], 10); if (num1 > num2) { return 1; } if (num1 < num2) { return -1; } } return 0; } function trimVersion(str) { if (str === void 0) { str = ''; } return str.replace(/^[\^|~]/, ''); } exports.compareVersion = compareVersion;