From 7982702ff4bb7fa64885e627d2f50246f3c0001b Mon Sep 17 00:00:00 2001 From: Suneil Nyamathi Date: Mon, 19 Jun 2017 15:53:48 -0700 Subject: [PATCH] Add special handling for major version 0 --- package.json | 2 +- semver-intersect.js | 17 +++++++++++++++++ tests/unit/semver-intersect.js | 20 ++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index ca718a9..61af042 100644 --- a/package.json +++ b/package.json @@ -25,5 +25,5 @@ "lint": "eslint .", "test": "jenkins-mocha tests/unit --recursive" }, - "version": "1.0.0" + "version": "1.1.0" } diff --git a/semver-intersect.js b/semver-intersect.js index 9afc0d8..4447024 100644 --- a/semver-intersect.js +++ b/semver-intersect.js @@ -14,6 +14,23 @@ function createShorthand (range) { } const [ min, max ] = match.slice(1); + + // Special handling for major version 0 + if (semver.major(min) === 0 && semver.major(max) === 0) { + // ^0.0.5 + if (semver.minor(min) === 0 && semver.minor(max) === 0) { + return `^${min}`; + } + + // ~0.0.5 + if (semver.minor(min) === 0) { + return `~${min}`; + } + + // ^0.5.0 + return `^${min}`; + } + if (semver.major(min) !== semver.major(max)) { return `^${min}`; } diff --git a/tests/unit/semver-intersect.js b/tests/unit/semver-intersect.js index 1e0c6c5..92a3989 100644 --- a/tests/unit/semver-intersect.js +++ b/tests/unit/semver-intersect.js @@ -31,6 +31,26 @@ describe('createShorthand', () => { const result = createShorthand('>=4.0.0-beta.1 <4.1.0'); expect(result).to.equal('~4.0.0-beta.1'); }); + it('should simplify 0.14.x to ~0.14.0', () => { + const result = createShorthand('>=0.14.0 <0.15.0'); + expect(result).to.equal('^0.14.0'); + }); + it('should simplify ^0.0.5', () => { + const result = createShorthand('>=0.0.5 <0.0.6'); + expect(result).to.equal('^0.0.5'); + }); + it('should simplify ~0.0.5', () => { + const result = createShorthand('>=0.0.5 <0.1.0'); + expect(result).to.equal('~0.0.5'); + }); + it('should simplify to ~0.0.0', () => { + const result = createShorthand('>=0.0.0 <0.1.0'); + expect(result).to.equal('~0.0.0'); + }); + it('should simplify to ^0.0.0', () => { + const result = createShorthand('>=0.0.0 <0.0.1'); + expect(result).to.equal('^0.0.0'); + }); it('should return granular ranges without changes', () => { [ '>4.0.0',