this repo has no description
1function parseVersion(input) { 2 const data = { 3 version: input.toLowerCase() 4 }; 5 const parts = input.toLowerCase().split('.').filter((p)=>!!p); 6 // Only parse single parts that are actual numbers. 7 // This check will prevent `parseInt` from pasing the leading chars if they are 8 // valid numbers. 9 if (parts.length <= 1 && !/^\d+$/.test(parts[0])) { 10 return data; 11 } 12 const major = parseInt(parts[0], 10); 13 const minor = parseInt(parts[1], 10); 14 const patch = parseInt(parts[2], 10); 15 // Only add converted versions if `major` part was valid 16 if (!Number.isNaN(major)) { 17 data.major = major; 18 if (!Number.isNaN(minor)) data.minor = minor; 19 if (!Number.isNaN(patch)) data.patch = patch; 20 } 21 return data; 22} 23/** 24 * Check if the given value is a complete Version struct. 25 */ // eslint-disable-next-line @typescript-eslint/no-explicit-any 26function isVersion(value) { 27 var _value, _value1; 28 return typeof ((_value = value) === null || _value === void 0 ? void 0 : _value.major) === 'number' && typeof ((_value1 = value) === null || _value1 === void 0 ? void 0 : _value1.minor) === 'number'; 29} 30/** 31 * Compare two version numbers together. 32 * 33 * NOTE: This only supports the first 3 segments (major, minor, patch) and does not 34 * do a full SemVer compare. 35 * 36 * @example 37 * ```javascript 38 * compareVersion('1.2.3', '1.2.4'); 39 * // => -1 40 * ``` 41 */ function compareVersion(base, comp) { 42 let baseList = toNumbers(base); 43 let compList = toNumbers(comp); 44 // Right pad versions with zeros to make them equal length 45 const versionLength = Math.max(baseList.length, compList.length); 46 baseList = baseList.concat(Array(versionLength).fill(0)).slice(0, versionLength); 47 compList = compList.concat(Array(versionLength).fill(0)).slice(0, versionLength); 48 /** Constrain the given value to the output range of this function. */ const constrain = (value)=>{ 49 if (value <= -1) return -1; 50 else if (value >= 1) return 1; 51 else return 0; 52 }; 53 for(let index = 0; index < versionLength; index++){ 54 const aValue = baseList[index]; 55 const bValue = compList[index]; 56 if (aValue !== bValue) { 57 return constrain(aValue - bValue); 58 } 59 } 60 return 0; 61} 62function eq(base, comp) { 63 return compareVersion(base, comp) === 0; 64} 65function gt(base, comp) { 66 return compareVersion(base, comp) > 0; 67} 68function gte(base, comp) { 69 const result = compareVersion(base, comp); 70 return result > 0 || result === 0; 71} 72function lt(base, comp) { 73 return compareVersion(base, comp) < 0; 74} 75function lte(base, comp) { 76 const result = compareVersion(base, comp); 77 return result < 0 || result === 0; 78} 79function toNumbers(value) { 80 if (Array.isArray(value)) { 81 return value; 82 } else if (typeof value === 'number') { 83 return [ 84 value 85 ]; 86 } else if (typeof value === 'string') { 87 return toNumbers(parseVersion(value)); 88 } else { 89 const values = [ 90 value.major, 91 value.minor, 92 value.patch 93 ]; 94 const uidx = values.indexOf(undefined); 95 return uidx === -1 ? values : values.slice(0, uidx); 96 } 97} 98 99export { compareVersion, eq, gt, gte, isVersion, lt, lte, parseVersion };