Socket
Socket
Sign inDemoInstall

compare-versions

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

compare-versions - npm Package Compare versions

Comparing version 3.1.0 to 3.2.0

2

bower.json
{
"name": "compare-versions",
"version": "3.1.0",
"version": "3.2.0",
"description": "Compare semver version strings to find greater, equal or lesser.",

@@ -5,0 +5,0 @@ "main": "index.js",

# Changelog
## [3.2.0](https://github.com/omichelsen/compare-versions/releases/tag/v3.2.0) - 2018-05-13
- Support Chromium version numbers.
## [3.1.0](https://github.com/omichelsen/compare-versions/releases/tag/v3.1.0) - 2017-09-25

@@ -4,0 +7,0 @@ - Ignore leading zero in numbers.

/* global define */
(function (root, factory) {
/* istanbul ignore next */
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.compareVersions = factory();
}
/* istanbul ignore next */
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.compareVersions = factory();
}
}(this, function () {
var semver = /^v?(?:\d+)(\.(?:[x*]|\d+)(\.(?:[x*]|\d+)(?:-[\da-z\-]+(?:\.[\da-z\-]+)*)?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i;
var patch = /-([0-9A-Za-z-.]+)/;
var semver = /^v?(?:\d+)(\.(?:[x*]|\d+)(\.(?:[x*]|\d+)(\.(?:[x*]|\d+))?(?:-[\da-z\-]+(?:\.[\da-z\-]+)*)?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i;
function split(v) {
var temp = v.replace(/^v/, '').split('.');
var arr = temp.splice(0, 2);
arr.push(temp.join('.'));
return arr;
}
function indexOrEnd(str, q) {
return str.indexOf(q) === -1 ? str.length : str.indexOf(q);
}
function tryParse(v) {
return isNaN(Number(v)) ? v : Number(v);
}
function split(v) {
var patchIndex = indexOrEnd(v, '-');
var arr = v.replace(/^v/, '').substring(0, patchIndex).split('.');
arr.push(v.substring(patchIndex + 1));
return arr;
}
function validate(version) {
if (typeof version !== 'string') {
throw new TypeError('Invalid argument expected string');
}
if (!semver.test(version)) {
throw new Error('Invalid argument not valid semver');
}
function tryParse(v) {
return isNaN(Number(v)) ? v : Number(v);
}
function validate(version) {
if (typeof version !== 'string') {
throw new TypeError('Invalid argument expected string');
}
if (!semver.test(version)) {
throw new Error('Invalid argument not valid semver');
}
}
return function compareVersions(v1, v2) {
[v1, v2].forEach(validate);
return function compareVersions(v1, v2) {
[v1, v2].forEach(validate);
var s1 = split(v1);
var s2 = split(v2);
var s1 = split(v1);
var s2 = split(v2);
for (var i = 0; i < 3; i++) {
var n1 = parseInt(s1[i] || 0, 10);
var n2 = parseInt(s2[i] || 0, 10);
for (var i = 0; i < Math.max(s1.length - 1, s2.length - 1); i++) {
var n1 = parseInt(s1[i] || 0, 10);
var n2 = parseInt(s2[i] || 0, 10);
if (n1 > n2) return 1;
if (n2 > n1) return -1;
}
if (n1 > n2) return 1;
if (n2 > n1) return -1;
}
if ([s1[2], s2[2]].every(patch.test.bind(patch))) {
var p1 = patch.exec(s1[2])[1].split('.').map(tryParse);
var p2 = patch.exec(s2[2])[1].split('.').map(tryParse);
var sp1 = s1[s1.length - 1];
var sp2 = s2[s2.length - 1];
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 (sp1 && sp2) {
var p1 = sp1.substring(0, indexOrEnd(sp1, '+')).split('.').map(tryParse);
var p2 = sp2.substring(0, indexOrEnd(sp2, '+')).split('.').map(tryParse);
if (p1[i] > p2[i]) return 1;
if (p2[i] > p1[i]) return -1;
}
} else if ([s1[2], s2[2]].some(patch.test.bind(patch))) {
return patch.test(s1[2]) ? -1 : 1;
}
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;
return 0;
};
if (p1[i] > p2[i]) return 1;
if (p2[i] > p1[i]) return -1;
}
} else if (sp1 || sp2) {
return sp1 ? -1 : 1;
}
return 0;
};
}));
{
"name": "compare-versions",
"version": "3.1.0",
"version": "3.2.0",
"description": "Compare semver version strings to find greater, equal or lesser.",
"main": "index.js",
"repository": {
"type": "git",
"url": "git@github.com:omichelsen/compare-versions.git"
"url": "git+https://github.com/omichelsen/compare-versions.git"
},
"scripts": {
"test": "mocha",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/",
"coverage": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/"
"author": "Ole Michelsen",
"license": "MIT",
"bugs": {
"url": "https://github.com/omichelsen/compare-versions/issues"
},
"homepage": "https://github.com/omichelsen/compare-versions#readme",
"keywords": [

@@ -22,8 +22,13 @@ "semver",

],
"author": "Ole Bjørn Michelsen <ole@michelsen.dk>",
"license": "MIT",
"scripts": {
"test": "nyc mocha"
},
"main": "index.js",
"directories": {
"test": "test"
},
"devDependencies": {
"istanbul": "^0.4.3",
"mocha": "^3.0.2"
"mocha": "^5.1.1",
"nyc": "^11.7.3"
}
}

@@ -6,18 +6,17 @@ # compare-versions

Compare [semver](http://semver.org/) version strings to find greater, equal or lesser. Runs in the browser as well as Node.js/React Native etc. Has no dependencies and is tiny (<600 bytes gzipped).
Compare [semver](https://semver.org/) version strings to find greater, equal or lesser. Runs in the browser as well as Node.js/React Native etc. Has no dependencies and is tiny (<600 bytes gzipped).
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`. Also supports wildcards for minor and patch version like `1.0.x` or `1.0.*`. Any leading `v` is ignored. Numbers with leading zero is handled as normal numbers ignoring the zero.
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`. Additionally supports the following variations:
- Supports wildcards for minor and patch version like `1.0.x` or `1.0.*`.
- Supports [Chromium version numbers](https://www.chromium.org/developers/version-numbers) with 4 parts, e.g. version `25.0.1364.126`.
- Any leading `v` is ignored, e.g. `v1.0` is interpreted as `1.0`.
- Leading zero is ignored, e.g. `1.01.1` is interpreted as `1.1.1`.
## Install
Install with `npm` or `bower`:
```bash
$ npm install compare-versions --save
$ npm install compare-versions
```
```bash
$ bower install compare-versions --save
```
## Usage

@@ -37,17 +36,25 @@

var versions = [
'1.5.19',
'1.2.3',
'1.5.5'
];
console.log(versions.sort(compareVersions));
'1.5.19',
'1.2.3',
'1.5.5'
]
var sorted = versions.sort(compareVersions);
/*
[
'1.2.3',
'1.5.5',
'1.5.19'
]
*/
```
Outputs:
### Browser
```javascript
[
'1.2.3',
'1.5.5',
'1.5.19'
]
If included directly in the browser, `compareVersions()` is available on the global window:
```html
<script src="compare-versions/index.js"></script>
<script>
window.compareVersions('10.0.0', '10.1.0');
</script>
```
var assert = require('assert');
var compare = require('..');
var cmp = {
'1': '>',
'0': '=',
'-1': '<'
'1': '>',
'0': '=',
'-1': '<'
};
describe('compare versions', function () {
it('should compare three-segment versions correctly', function () {
assert.equal(compare('10.1.8', '10.0.4'), 1);
assert.equal(compare('10.0.1', '10.0.1'), 0);
assert.equal(compare('10.1.1', '10.2.2'), -1);
});
it('should compare three-segment versions correctly', function () {
assert.equal(compare('10.1.8', '10.0.4'), 1);
assert.equal(compare('10.0.1', '10.0.1'), 0);
assert.equal(compare('10.1.1', '10.2.2'), -1);
});
it('should compare two-segment versions correctly', function () {
assert.equal(compare('10.8', '10.4'), 1);
assert.equal(compare('10.1', '10.1'), 0);
assert.equal(compare('10.1', '10.2'), -1);
});
it('should compare two-segment versions correctly', function () {
assert.equal(compare('10.8', '10.4'), 1);
assert.equal(compare('10.1', '10.1'), 0);
assert.equal(compare('10.1', '10.2'), -1);
});
it('should compare single-segment versions correctly', function () {
assert.equal(compare('10', '9'), 1);
assert.equal(compare('10', '10'), 0);
assert.equal(compare('9', '10'), -1);
});
it('should compare single-segment versions correctly', function () {
assert.equal(compare('10', '9'), 1);
assert.equal(compare('10', '10'), 0);
assert.equal(compare('9', '10'), -1);
});
it('should compare versions with different number of digits in same group', function () {
assert.equal(compare('11.0.10', '11.0.2'), 1);
assert.equal(compare('11.0.2', '11.0.10'), -1);
describe('four-segment versions', () => {
[
['1.0.0.0', '1', 0],
['1.0.0.0', '1.0', 0],
['1.0.0.0', '1.0.0', 0],
['1.0.0.0', '1.0.0.0', 0],
['1.2.3.4', '1.2.3.4', 0],
['1.2.3.4', '1.2.3.04', 0],
['v1.2.3.4', '01.2.3.4', 0],
['1.2.3.4', '1.2.3.5', -1],
['1.2.3.5', '1.2.3.4', 1],
['1.0.0.0-alpha', '1.0.0-alpha', 0],
['1.0.0.0-alpha', '1.0.0.0-beta', -1],
].forEach(([v1, v2, expected]) => {
it(`${v1} ${cmp[expected]} ${v2}`, () => {
assert.equal(compare(v1, v2), expected);
});
});
});
it('should compare versions with different number of digits in different groups', function () {
assert.equal(compare('11.1.10', '11.0'), 1);
});
it('should compare versions with different number of digits in same group', function () {
assert.equal(compare('11.0.10', '11.0.2'), 1);
assert.equal(compare('11.0.2', '11.0.10'), -1);
});
it('should compare versions with different number of digits', function () {
assert.equal(compare('1.1.1', '1'), 1);
assert.equal(compare('1.0.0', '1'), 0);
assert.equal(compare('1.0', '1.4.1'), -1);
});
it('should compare versions with different number of digits in different groups', function () {
assert.equal(compare('11.1.10', '11.0'), 1);
});
describe('pre-release versions', function () {
[
['1.0.0-alpha.1', '1.0.0-alpha', 1],
['1.0.0-alpha', '1.0.0-alpha.1', -1],
['1.0.0-alpha.1', '1.0.0-alpha.beta', -1],
['1.0.0-alpha.beta', '1.0.0-beta', -1],
['1.0.0-beta', '1.0.0-beta.2', -1],
['1.0.0-beta.2', '1.0.0-beta.11', -1],
['1.0.0-beta.11', '1.0.0-rc.1', -1],
['1.0.0-rc.1', '1.0.0', -1],
['1.0.0-alpha', '1', -1]
].forEach(function (data) {
it('should return ' + data[0] + ' ' + cmp[data[2]] + ' ' + data[1], function () {
assert.equal(compare(data[0], data[1]), data[2]);
});
});
});
it('should compare versions with different number of digits', function () {
assert.equal(compare('1.1.1', '1'), 1);
assert.equal(compare('1.0.0', '1'), 0);
assert.equal(compare('1.0', '1.4.1'), -1);
});
it('should ignore build metadata', function () {
assert.equal(compare('1.4.0-build.3928', '1.4.0-build.3928+sha.a8d9d4f'), 0);
assert.equal(compare('1.4.0-build.3928+sha.b8dbdb0', '1.4.0-build.3928+sha.a8d9d4f'), 0);
describe('pre-release versions', function () {
[
['1.0.0-alpha.1', '1.0.0-alpha', 1],
['1.0.0-alpha', '1.0.0-alpha.1', -1],
['1.0.0-alpha.1', '1.0.0-alpha.beta', -1],
['1.0.0-alpha.beta', '1.0.0-beta', -1],
['1.0.0-beta', '1.0.0-beta.2', -1],
['1.0.0-beta.2', '1.0.0-beta.11', -1],
['1.0.0-beta.11', '1.0.0-rc.1', -1],
['1.0.0-rc.1', '1.0.0', -1],
['1.0.0-alpha', '1', -1]
].forEach(([v1, v2, expected]) => {
it(`${v1} ${cmp[expected]} ${v2}`, () => {
assert.equal(compare(v1, v2), expected);
});
});
});
it('should ignore leading `v`', function () {
assert.equal(compare('v1.0.0', '1.0.0'), 0);
assert.equal(compare('v1.0.0', 'v1.0.0'), 0);
assert.equal(compare('v1.0.0', 'v1.0.0'), 0);
assert.equal(compare('v1.0.0-alpha', '1.0.0-alpha'), 0);
});
it('should ignore build metadata', function () {
assert.equal(compare('1.4.0-build.3928', '1.4.0-build.3928+sha.a8d9d4f'), 0);
assert.equal(compare('1.4.0-build.3928+sha.b8dbdb0', '1.4.0-build.3928+sha.a8d9d4f'), 0);
});
it('should ignore leading `0`', function () {
[
['01.0.0', '1.0.0', 0],
['1.01.0', '1.01.0', 0],
['1.0.03', '1.0.3', 0],
['1.0.03-alpha', '1.0.3-alpha', 0],
['v01.0.0', '1.0.0', 0],
['v01.0.0', '2.0.0', -1],
].forEach(function (data) {
assert.equal(compare(data[0], data[1]), data[2]);
});
it('should ignore leading `v`', function () {
assert.equal(compare('v1.0.0', '1.0.0'), 0);
assert.equal(compare('v1.0.0', 'v1.0.0'), 0);
assert.equal(compare('v1.0.0', 'v1.0.0'), 0);
assert.equal(compare('v1.0.0-alpha', '1.0.0-alpha'), 0);
});
describe('ignore leading `0`', () => {
[
['01.0.0', '1', 0],
['01.0.0', '1.0.0', 0],
['1.01.0', '1.01.0', 0],
['1.0.03', '1.0.3', 0],
['1.0.03-alpha', '1.0.3-alpha', 0],
['v01.0.0', '1.0.0', 0],
['v01.0.0', '2.0.0', -1],
].forEach(([v1, v2, expected]) => {
it(`${v1} ${cmp[expected]} ${v2}`, () => {
assert.equal(compare(v1, v2), expected);
});
});
});
it('should throw on invalid input', function () {
[
[42, /Invalid argument expected string/],
[{}, /Invalid argument expected string/],
[[], /Invalid argument expected string/],
[function () {}, /Invalid argument expected string/],
['6.3.', /Invalid argument not valid semver/],
].forEach(function (data) {
assert.throws(function () {
compare(data[0], data[0]);
}, data[1]);
});
describe('invalid input', function () {
[
[42, /Invalid argument expected string/],
[{}, /Invalid argument expected string/],
[[], /Invalid argument expected string/],
[function () {}, /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]) => {
it(`should throw on ${v1}`, () => {
assert.throws(() => { compare(v1, v1); }, exception);
});
});
});
});

@@ -5,54 +5,54 @@ var assert = require('assert');

describe('greater than', function () {
it('should evaluate to greater than (1)', function () {
[
['0.1.20', '0.1.5'],
['0.6.1-1', '0.6.1-0'],
['0.7.x', '0.6.0'],
['0.7.x', '0.6.0-asdf'],
['0.7.x', '0.6.2'],
['0.7.x', '0.7.0-asdf'],
['1', '0.0.0-beta'],
['1', '0.2.3'],
['1', '0.2.4'],
['1', '1.0.0-0'],
['1', '1.0.0-beta'],
['1.0', '0.0.0'],
['1.0', '0.1.0'],
['1.0', '0.1.2'],
['1.0.0', '0.0.0'],
['1.0.0', '0.0.1'],
['1.0.0', '0.2.3'],
['1.0.0-beta.2', '1.0.0-beta.1'],
['1.2.*', '1.1.3'],
['1.2.*', '1.1.9999'],
['1.2.2', '1.2.1'],
['1.2.x', '1.0.0'],
['1.2.x', '1.1.0'],
['1.2.x', '1.1.3'],
['2', '1.0.0'],
['2', '1.0.0-beta'],
['2', '1.9999.9999'],
['2.*.*', '1.0.1'],
['2.*.*', '1.1.3'],
['2.0.0', '1.0.0'],
['2.0.0', '1.1.1'],
['2.0.0', '1.2.9'],
['2.0.0', '1.9999.9999'],
['2.3', '2.2.1'],
['2.3', '2.2.2'],
['2.4', '2.3.0'],
['2.4', '2.3.5'],
['2.x.x', '1.0.0'],
['2.x.x', '1.1.3'],
['3.2.1', '2.3.2'],
['3.2.1', '3.2.0'],
['v0.5.4-pre', '0.5.4-alpha'],
['v3.2.1', 'v2.3.2']
].forEach(function (tuple) {
var v1 = tuple[0];
var v2 = tuple[1];
var msg = 'compare(' + v1 + ', ' + v2 + ')';
assert.equal(compare(v1, v2), 1, msg);
});
it('should evaluate to greater than (1)', function () {
[
['0.1.20', '0.1.5'],
['0.6.1-1', '0.6.1-0'],
['0.7.x', '0.6.0'],
['0.7.x', '0.6.0-asdf'],
['0.7.x', '0.6.2'],
['0.7.x', '0.7.0-asdf'],
['1', '0.0.0-beta'],
['1', '0.2.3'],
['1', '0.2.4'],
['1', '1.0.0-0'],
['1', '1.0.0-beta'],
['1.0', '0.0.0'],
['1.0', '0.1.0'],
['1.0', '0.1.2'],
['1.0.0', '0.0.0'],
['1.0.0', '0.0.1'],
['1.0.0', '0.2.3'],
['1.0.0-beta.2', '1.0.0-beta.1'],
['1.2.*', '1.1.3'],
['1.2.*', '1.1.9999'],
['1.2.2', '1.2.1'],
['1.2.x', '1.0.0'],
['1.2.x', '1.1.0'],
['1.2.x', '1.1.3'],
['2', '1.0.0'],
['2', '1.0.0-beta'],
['2', '1.9999.9999'],
['2.*.*', '1.0.1'],
['2.*.*', '1.1.3'],
['2.0.0', '1.0.0'],
['2.0.0', '1.1.1'],
['2.0.0', '1.2.9'],
['2.0.0', '1.9999.9999'],
['2.3', '2.2.1'],
['2.3', '2.2.2'],
['2.4', '2.3.0'],
['2.4', '2.3.5'],
['2.x.x', '1.0.0'],
['2.x.x', '1.1.3'],
['3.2.1', '2.3.2'],
['3.2.1', '3.2.0'],
['v0.5.4-pre', '0.5.4-alpha'],
['v3.2.1', 'v2.3.2']
].forEach(function (tuple) {
var v1 = tuple[0];
var v2 = tuple[1];
var msg = 'compare(' + v1 + ', ' + v2 + ')';
assert.equal(compare(v1, v2), 1, msg);
});
});
});

@@ -5,57 +5,57 @@ var assert = require('assert');

describe('sort versions', function () {
it('should sort versions', function () {
var versions = [
'1.2.3',
'4.11.6',
'4.2.0',
'1.5.19',
'1.5.5',
'4.1.3',
'2.3.1',
'10.5.5',
'11.3.0'
];
it('should sort versions', function () {
var versions = [
'1.2.3',
'4.11.6',
'4.2.0',
'1.5.19',
'1.5.5',
'4.1.3',
'2.3.1',
'10.5.5',
'11.3.0'
];
assert.deepEqual(versions.sort(compare), [
'1.2.3',
'1.5.5',
'1.5.19',
'2.3.1',
'4.1.3',
'4.2.0',
'4.11.6',
'10.5.5',
'11.3.0'
]);
});
assert.deepEqual(versions.sort(compare), [
'1.2.3',
'1.5.5',
'1.5.19',
'2.3.1',
'4.1.3',
'4.2.0',
'4.11.6',
'10.5.5',
'11.3.0'
]);
});
it('should sort different digits', function () {
var versions = [
'1.0',
'1.0.0',
'1.0.1'
];
it('should sort different digits', function () {
var versions = [
'1.0',
'1.0.0',
'1.0.1'
];
assert.deepEqual(versions.sort(compare), [
'1.0',
'1.0.0',
'1.0.1'
]);
});
assert.deepEqual(versions.sort(compare), [
'1.0',
'1.0.0',
'1.0.1'
]);
});
it('should sort pre-release', function () {
var versions = [
'1.0.0',
'1.0.1',
'1.0.1-gamma',
'1.0.1-alpha'
];
it('should sort pre-release', function () {
var versions = [
'1.0.0',
'1.0.1',
'1.0.1-gamma',
'1.0.1-alpha'
];
assert.deepEqual(versions.sort(compare), [
'1.0.0',
'1.0.1-alpha',
'1.0.1-gamma',
'1.0.1'
]);
});
assert.deepEqual(versions.sort(compare), [
'1.0.0',
'1.0.1-alpha',
'1.0.1-gamma',
'1.0.1'
]);
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc