compare-versions
Advanced tools
Comparing version 3.4.0 to 3.5.0
{ | ||
"name": "compare-versions", | ||
"version": "3.4.0", | ||
"version": "3.5.0", | ||
"description": "Compare semver version strings to find greater, equal or lesser.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# Changelog | ||
## [3.5.0](https://github.com/omichelsen/compare-versions/releases/tag/v3.5.0) - 2019-06-22 | ||
- Add api returning true or false given a comparison operator. | ||
## [3.4.0](https://github.com/omichelsen/compare-versions/releases/tag/v3.4.0) - 2018-08-30 | ||
@@ -4,0 +7,0 @@ - Show rejected version in error message. |
@@ -1,3 +0,42 @@ | ||
declare namespace compareVersions { } | ||
declare function compareVersions(firstVersion: string, secondVersion: string): 1 | 0 | -1; | ||
declare namespace compareVersions { | ||
/** | ||
* Allowed arithmetic operators | ||
*/ | ||
type CompareOperator = '>' | '>=' | '=' | '<' | '<='; | ||
} | ||
declare const compareVersions: { | ||
/** | ||
* Compare [semver](https://semver.org/) version strings to find greater, equal or lesser. | ||
* This library supports the full semver specification, including comparing versions with different number of digits like `1.0.0`, `1.0`, `1`, and pre-release versions like `1.0.0-alpha`. | ||
* @param firstVersion - First version to compare | ||
* @param secondVersion - Second version to compare | ||
* @returns Numeric value compatible with the [Array.sort(fn) interface](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Parameters). | ||
*/ | ||
(firstVersion: string, secondVersion: string): 1 | 0 | -1; | ||
/** | ||
* Compare [semver](https://semver.org/) version strings using the specified operator. | ||
* | ||
* @param firstVersion First version to compare | ||
* @param secondVersion Second version to compare | ||
* @param operator Allowed arithmetic operator to use | ||
* @returns `true` if the comparison between the firstVersion and the secondVersion satisfies the operator, `false` otherwise. | ||
* | ||
* @example | ||
* ``` | ||
* compareVersions.compare('10.1.8', '10.0.4', '>'); // return true | ||
* compareVersions.compare('10.0.1', '10.0.1', '='); // return true | ||
* compareVersions.compare('10.1.1', '10.2.2', '<'); // return true | ||
* compareVersions.compare('10.1.1', '10.2.2', '<='); // return true | ||
* compareVersions.compare('10.1.1', '10.2.2', '>='); // return false | ||
* ``` | ||
*/ | ||
compare( | ||
firstVersion: string, | ||
secondVersion: string, | ||
operator: compareVersions.CompareOperator | ||
): boolean; | ||
}; | ||
export = compareVersions; |
40
index.js
@@ -40,3 +40,3 @@ /* global define */ | ||
return function compareVersions(v1, v2) { | ||
function compareVersions(v1, v2) { | ||
[v1, v2].forEach(validate); | ||
@@ -76,2 +76,40 @@ | ||
var allowedOperators = [ | ||
'>', | ||
'>=', | ||
'=', | ||
'<', | ||
'<=' | ||
]; | ||
function validateOperator(op) { | ||
if (typeof op !== 'string') { | ||
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('|')); | ||
} | ||
} | ||
compareVersions.compare = function (v1, v2, operator) { | ||
// Validate operator | ||
validateOperator(operator); | ||
// TODO: there might be a better way instead of doing this | ||
switch(operator) { | ||
case '>': | ||
return compareVersions(v1, v2) > 0; | ||
case '>=': | ||
return compareVersions(v1, v2) >= 0; | ||
case '<': | ||
return compareVersions(v1, v2) < 0; | ||
case '<=': | ||
return compareVersions(v1, v2) <= 0; | ||
default: | ||
// Since validateOperator already checks the operator, this case in the switch checks for the '=' operator | ||
return compareVersions(v1, v2) === 0; | ||
} | ||
} | ||
return compareVersions; | ||
})); |
{ | ||
"name": "compare-versions", | ||
"version": "3.4.0", | ||
"version": "3.5.0", | ||
"description": "Compare semver version strings to find greater, equal or lesser.", | ||
@@ -31,5 +31,5 @@ "repository": { | ||
"devDependencies": { | ||
"mocha": "^5.2.0", | ||
"nyc": "^12.0.2" | ||
"mocha": "^6.1.4", | ||
"nyc": "^14.1.1" | ||
} | ||
} |
@@ -28,3 +28,3 @@ # compare-versions | ||
// ES6/TypeScript | ||
import * as compareVersions from 'compare-versions'; | ||
import compareVersions from 'compare-versions'; | ||
@@ -59,4 +59,25 @@ // Node | ||
*/ | ||
var sortDescending = versions.sort(compareVersions).reverse(); | ||
/* | ||
[ | ||
'1.5.19' | ||
'1.5.5', | ||
'1.2.3', | ||
] | ||
*/ | ||
``` | ||
### "Human Readable" Compare | ||
The normal compare function doesn't return a self-explanatory value (using `1`, `0` and `-1`). | ||
This version returns the boolean which fulfills the specified operator. | ||
```js | ||
compareVersions.compare('10.1.8', '10.0.4', '>'); // return true | ||
compareVersions.compare('10.0.1', '10.0.1', '='); // return true | ||
compareVersions.compare('10.1.1', '10.2.2', '<'); // return true | ||
compareVersions.compare('10.1.1', '10.2.2', '<='); // return true | ||
compareVersions.compare('10.1.1', '10.2.2', '>='); // return false | ||
``` | ||
### Browser | ||
@@ -63,0 +84,0 @@ |
@@ -181,2 +181,52 @@ const assert = require('assert'); | ||
]); | ||
}); | ||
}); | ||
describe('human readable compare versions', () => { | ||
it('should throw if the operator is not a string', () => { | ||
assert.throws(() => { compare.compare('3.2.1', '3.2.0', null); }, /Invalid operator type, expected string but got /); | ||
assert.throws(() => { compare.compare('3.2.1', '3.2.0', undefined); }, /Invalid operator type, expected string but got /); | ||
assert.throws(() => { compare.compare('3.2.1', '3.2.0', true); }, /Invalid operator type, expected string but got boolean/); | ||
assert.throws(() => { compare.compare('3.2.1', '3.2.0', 1); }, /Invalid operator type, expected string but got number/); | ||
assert.throws(() => { compare.compare('3.2.1', '3.2.0', { foo:'bar' }); }, /Invalid operator type, expected string but got object/); | ||
assert.throws(() => { compare.compare('3.2.1', '3.2.0', () => {}); }, /Invalid operator type, expected string but got function/); | ||
}); | ||
it('should throw if the operator is not in the allowed operators', () => { | ||
assert.throws(() => { compare.compare('3.2.1', '3.2.0', ''); }, /Invalid operator, expected one of /); | ||
assert.throws(() => { compare.compare('3.2.1', '3.2.0', 'foo'); }, /Invalid operator, expected one of /); | ||
assert.throws(() => { compare.compare('3.2.1', '3.2.0', '> '); }, /Invalid operator, expected one of /); | ||
}); | ||
it('should throw the same Errors thrown by the main function', () => { | ||
[ | ||
[42, /Invalid argument expected string/], | ||
[{}, /Invalid argument expected string/], | ||
[[], /Invalid argument expected string/], | ||
[() => undefined, /Invalid argument expected string/], | ||
['6.3.', /Invalid argument not valid semver/], | ||
['1.2.3a', /Invalid argument not valid semver/], | ||
['1.2.-3a', /Invalid argument not valid semver/], | ||
].forEach(([v1, exception]) => { | ||
assert.throws(() => { compare.compare(v1, v1, '>'); }, exception); | ||
}); | ||
}); | ||
it('should return the expected results when everything is ok', () => { | ||
[ | ||
{first: '10.1.8', second: '10.0.4', operator: '>', expected: true}, | ||
{first: '10.1.8', second: '10.0.4', operator: '>=', expected: true}, | ||
{first: '10.0.1', second: '10.0.1', operator: '=', expected: true}, | ||
{first: '10.0.1', second: '10.1.*', operator: '=', expected: false}, | ||
{first: '10.1.1', second: '10.2.2', operator: '<', expected: true}, | ||
{first: '10.1.1', second: '10.0.2', operator: '<', expected: false}, | ||
{first: '10.1.1', second: '10.2.2', operator: '<=', expected: true}, | ||
{first: '10.1.1', second: '10.1.1', operator: '<=', expected: true}, | ||
{first: '10.1.1', second: '10.0.2', operator: '<=', expected: false}, | ||
{first: '10.1.1', second: '10.0.2', operator: '>=', expected: true}, | ||
{first: '10.1.1', second: '10.1.1', operator: '>=', expected: true}, | ||
{first: '10.1.1', second: '10.2.2', operator: '>=', expected: false}, | ||
].forEach(testCtx => { | ||
assert.strictEqual(compare.compare(testCtx.first, testCtx.second, testCtx.operator), testCtx.expected); | ||
}) | ||
}); | ||
}); |
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
21516
427
91