compare-versions
Advanced tools
Comparing version 4.0.2 to 4.1.0
@@ -20,3 +20,3 @@ declare namespace compareVersions { | ||
* Compare [semver](https://semver.org/) version strings using the specified operator. | ||
* | ||
* | ||
* @param firstVersion First version to compare | ||
@@ -36,5 +36,5 @@ * @param secondVersion Second version to compare | ||
*/ | ||
compare( | ||
firstVersion: string, | ||
secondVersion: string, | ||
compare( | ||
firstVersion: string, | ||
secondVersion: string, | ||
operator: compareVersions.CompareOperator | ||
@@ -45,3 +45,3 @@ ): boolean; | ||
* Validate [semver](https://semver.org/) version strings. | ||
* | ||
* | ||
* @param version Version number to validate | ||
@@ -57,7 +57,20 @@ * @returns `true` if the version number is a valid semver version number, `false` otherwise. | ||
*/ | ||
validate( | ||
version: string | ||
): boolean; | ||
validate(version: string): boolean; | ||
/** | ||
* Match [npm semver](https://docs.npmjs.com/cli/v6/using-npm/semver) version range. | ||
* | ||
* @param version Version number to match | ||
* @param range Range pattern for version | ||
* @returns `true` if the version number is within the range, `false` otherwise. | ||
* | ||
* @example | ||
* ``` | ||
* satisfies('1.1.0', '^1.0.0'); // return true | ||
* satisfies('1.1.0', '~1.0.0'); // return false | ||
* ``` | ||
*/ | ||
satisfies(version: string, range: string): boolean; | ||
}; | ||
export = compareVersions; | ||
export = compareVersions; |
102
index.js
@@ -11,6 +11,6 @@ /* global define */ | ||
} | ||
}(this, function () { | ||
})(this, function () { | ||
var semver = | ||
/^[v^~<>=]*?(\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+))?(?:-([\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i; | ||
var semver = /^v?(?:\d+)(\.(?:[x*]|\d+)(\.(?:[x*]|\d+)(\.(?:[x*]|\d+))?(?:-[\da-z\-]+(?:\.[\da-z\-]+)*)?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i; | ||
function indexOrEnd(str, q) { | ||
@@ -29,16 +29,41 @@ return str.indexOf(q) === -1 ? str.length : str.indexOf(q); | ||
function tryParse(v) { | ||
return isNaN(Number(v)) ? v : Number(v); | ||
var n = parseInt(v, 10); | ||
return isNaN(n) ? v : n; | ||
} | ||
function validate(version) { | ||
if (typeof version !== 'string') { | ||
function validateAndParse(v) { | ||
if (typeof v !== 'string') { | ||
throw new TypeError('Invalid argument expected string'); | ||
} | ||
if (!semver.test(version)) { | ||
throw new Error('Invalid argument not valid semver (\''+version+'\' received)'); | ||
var match = v.match(semver); | ||
if (!match) { | ||
throw new Error( | ||
"Invalid argument not valid semver ('" + v + "' received)" | ||
); | ||
} | ||
match.shift(); | ||
return match; | ||
} | ||
function forceType(a, b) { | ||
return typeof a !== typeof b ? [String(a), String(b)] : [a, b]; | ||
} | ||
function compareStrings(a, b) { | ||
var [ap, bp] = forceType(tryParse(a), tryParse(b)); | ||
if (ap > bp) return 1; | ||
if (ap < bp) return -1; | ||
return 0; | ||
} | ||
function compareSegments(a, b) { | ||
for (var i = 0; i < Math.max(a.length, b.length); i++) { | ||
var r = compareStrings(a[i] || 0, b[i] || 0); | ||
if (r !== 0) return r; | ||
} | ||
return 0; | ||
} | ||
function compareVersions(v1, v2) { | ||
[v1, v2].forEach(validate); | ||
[v1, v2].forEach(validateAndParse); | ||
@@ -64,4 +89,12 @@ var s1 = split(v1); | ||
for (i = 0; i < Math.max(p1.length, p2.length); i++) { | ||
if (p1[i] === undefined || typeof p2[i] === 'string' && typeof p1[i] === 'number') return -1; | ||
if (p2[i] === undefined || typeof p1[i] === 'string' && typeof p2[i] === 'number') return 1; | ||
if ( | ||
p1[i] === undefined || | ||
(typeof p2[i] === 'string' && typeof p1[i] === 'number') | ||
) | ||
return -1; | ||
if ( | ||
p2[i] === undefined || | ||
(typeof p1[i] === 'string' && typeof p2[i] === 'number') | ||
) | ||
return 1; | ||
@@ -76,11 +109,5 @@ if (p1[i] > p2[i]) return 1; | ||
return 0; | ||
}; | ||
} | ||
var allowedOperators = [ | ||
'>', | ||
'>=', | ||
'=', | ||
'<', | ||
'<=' | ||
]; | ||
var allowedOperators = ['>', '>=', '=', '<', '<=']; | ||
@@ -92,3 +119,3 @@ var operatorResMap = { | ||
'<=': [-1, 0], | ||
'<': [-1] | ||
'<': [-1], | ||
}; | ||
@@ -98,12 +125,16 @@ | ||
if (typeof op !== 'string') { | ||
throw new TypeError('Invalid operator type, expected string but got ' + typeof op); | ||
throw new TypeError( | ||
'Invalid operator type, expected string but got ' + typeof op | ||
); | ||
} | ||
if (allowedOperators.indexOf(op) === -1) { | ||
throw new TypeError('Invalid operator, expected one of ' + allowedOperators.join('|')); | ||
throw new TypeError( | ||
'Invalid operator, expected one of ' + allowedOperators.join('|') | ||
); | ||
} | ||
} | ||
compareVersions.validate = function(version) { | ||
compareVersions.validate = function (version) { | ||
return typeof version === 'string' && semver.test(version); | ||
} | ||
}; | ||
@@ -118,5 +149,24 @@ compareVersions.compare = function (v1, v2, operator) { | ||
return operatorResMap[operator].indexOf(res) > -1; | ||
} | ||
}; | ||
compareVersions.satisfies = function (v, r) { | ||
// if no range operator then "=" | ||
var match = r.match(/^([<>=~^]+)/); | ||
var op = match ? match[1] : '='; | ||
// if gt/lt/eq then operator compare | ||
if (op !== '^' && op !== '~') return compareVersions.compare(v, r, op); | ||
// else range of either "~" or "^" is assumed | ||
var [v1, v2, v3] = validateAndParse(v); | ||
var [m1, m2, m3] = validateAndParse(r); | ||
if (compareStrings(v1, m1) !== 0) return false; | ||
if (op === '^') { | ||
return compareSegments([v2, v3], [m2, m3]) >= 0; | ||
} | ||
if (compareStrings(v2, m2) !== 0) return false; | ||
return compareStrings(v3, m3) >= 0; | ||
}; | ||
return compareVersions; | ||
})); | ||
}); |
{ | ||
"name": "compare-versions", | ||
"version": "4.0.2", | ||
"version": "4.1.0", | ||
"description": "Compare semver version strings to find greater, equal or lesser.", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -52,10 +52,2 @@ # compare-versions | ||
*/ | ||
const sortDescending = versions.sort(compareVersions).reverse(); | ||
/* | ||
[ | ||
'1.5.19' | ||
'1.5.5', | ||
'1.2.3', | ||
] | ||
*/ | ||
``` | ||
@@ -77,2 +69,20 @@ | ||
### Version ranges | ||
The `satisfies` function accepts a range to compare, compatible with [npm package versioning](https://docs.npmjs.com/cli/v6/using-npm/semver): | ||
```js | ||
import { satisfies } from 'compare-versions'; | ||
satisfies('10.0.1', '~10.0.0'); // true | ||
satisfies('10.1.0', '~10.0.0'); // false | ||
satisfies('10.1.2', '^10.0.0'); // true | ||
satisfies('11.0.0', '^10.0.0'); // false | ||
satisfies('10.1.8', '>10.0.4'); // true | ||
satisfies('10.0.1', '=10.0.1'); // true | ||
satisfies('10.1.1', '<10.2.2'); // true | ||
satisfies('10.1.1', '<=10.2.2'); // true | ||
satisfies('10.1.1', '>=10.2.2'); // false | ||
``` | ||
### Validate version numbers | ||
@@ -79,0 +89,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15575
302
110