compare-versions
Advanced tools
Comparing version 2.0.1 to 2.0.2
{ | ||
"name": "compare-versions", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Compare semver version strings to find greater, equal or lesser.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
25
index.js
@@ -13,3 +13,3 @@ /* global define */ | ||
var patchPattern = /-([\w-.]+)/; | ||
var patch = /-([0-9A-Za-z-.]+)/; | ||
@@ -23,2 +23,6 @@ function split(v) { | ||
function tryParse(v) { | ||
return isNaN(Number(v)) ? v : Number(v); | ||
} | ||
return function compareVersions(v1, v2) { | ||
@@ -36,10 +40,15 @@ var s1 = split(v1); | ||
if ((s1[2] + s2[2] + '').indexOf('-') > -1) { | ||
var p1 = (patchPattern.exec(s1[2]) || [''])[0]; | ||
var p2 = (patchPattern.exec(s2[2]) || [''])[0]; | ||
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); | ||
if (p1 === '') return 1; | ||
if (p2 === '') return -1; | ||
if (p1 > p2) return 1; | ||
if (p2 > p1) return -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 (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; | ||
} | ||
@@ -46,0 +55,0 @@ |
{ | ||
"name": "compare-versions", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Compare semver version strings to find greater, equal or lesser.", | ||
@@ -25,5 +25,5 @@ "main": "index.js", | ||
"devDependencies": { | ||
"istanbul": "~0.3.19", | ||
"mocha": "~2.3.1" | ||
"istanbul": "^0.4.3", | ||
"mocha": "^2.5.3" | ||
} | ||
} |
var assert = require('assert'); | ||
var compare = require('..'); | ||
var cmp = { | ||
'1': '>', | ||
'0': '=', | ||
'-1': '<' | ||
}; | ||
@@ -38,14 +43,19 @@ describe('compare versions', function () { | ||
it('should compare pre-release versions', function () { | ||
assert.equal(compare('1.0.0-alpha.1', '1.0.0-alpha'), 1); | ||
assert.equal(compare('1.0.0-alpha', '1.0.0-alpha'), 0); | ||
assert.equal(compare('1.0.0-alpha', '1.0.0-alpha.beta'), -1); | ||
assert.equal(compare('1.0.0-alpha', '1.0.0-beta'), -1); | ||
describe('pre-release versions', function () { | ||
[ | ||
['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 give precendece to normal versions over pre-release', function () { | ||
assert.equal(compare('1.0.0', '1.0.0-alpha'), 1); | ||
assert.equal(compare('1.0.0-beta', '1'), -1); | ||
}); | ||
it('should ignore build metadata', function () { | ||
@@ -52,0 +62,0 @@ assert.equal(compare('1.4.0-build.3928', '1.4.0-build.3928+sha.a8d9d4f'), 0); |
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
10878
236