From 3f676f510b311f63bc1dac2f92174f589bc5a14b Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 22 Jul 2014 12:32:51 -0700 Subject: [PATCH] ^0.x.y always equivalent to =0.x.y More in line with specification at http://semver.org --- README.md | 10 +++++----- semver.js | 39 ++++++++++++++------------------------- test/index.js | 2 +- 3 files changed, 20 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 4e95b846..2ca8d80c 100644 --- a/README.md +++ b/README.md @@ -66,11 +66,11 @@ The following range styles are supported: prerelease) will be supported up to, but not including, the next major version (or its prereleases). `1.5.1` will satisfy `^1.2.3`, while `1.2.2` and `2.0.0-beta` will not. -* `^0.1.3` := `>=0.1.3-0 <0.2.0-0` "Compatible with `0.1.3`". `0.x.x` versions are - special: the first non-zero component indicates potentially breaking changes, - meaning the caret operator matches any version with the same first non-zero - component starting at the specified version. -* `^0.0.2` := `=0.0.2` "Only the version `0.0.2` is considered compatible" +* `^0.1.3` := `0.1.3` "Compatible with `0.1.3`". `0.x.x` versions are + special: since the semver spec specifies that `0.x.x` versions make + no stability guarantees, only the version specified is considered + valid. +* `^0.0.2` := `0.0.2` "Only the version `0.0.2` is considered compatible" * `~1.2` := `>=1.2.0-0 <1.3.0-0` "Any version starting with `1.2`" * `^1.2` := `>=1.2.0-0 <2.0.0-0` "Any version compatible with `1.2`" * `1.2.x` := `>=1.2.0-0 <1.3.0-0` "Any version starting with `1.2`" diff --git a/semver.js b/semver.js index 75f60f2a..2939331c 100644 --- a/semver.js +++ b/semver.js @@ -369,7 +369,7 @@ SemVer.prototype.inc = function(release) { // If this is already a prerelease, it will bump to the next version // drop any prereleases that might already exist, since they are not // relevant at this point. - this.prerelease.length = 0 + this.prerelease.length = 0; this.inc('patch'); this.inc('pre'); break; @@ -770,6 +770,11 @@ function replaceCaret(comp, loose) { return comp.replace(r, function(_, M, m, p, pr) { debug('caret', comp, _, M, m, p, pr); var ret; + if (pr) { + if (pr.charAt(0) !== '-') + pr = '-' + pr; + } else + pr = ''; if (isX(M)) ret = ''; @@ -780,30 +785,14 @@ function replaceCaret(comp, loose) { ret = '>=' + M + '.' + m + '.0-0 <' + M + '.' + (+m + 1) + '.0-0'; else ret = '>=' + M + '.' + m + '.0-0 <' + (+M + 1) + '.0.0-0'; - } else if (pr) { - debug('replaceCaret pr', pr); - if (pr.charAt(0) !== '-') - pr = '-' + pr; - if (M === '0') { - if (m === '0') - ret = '=' + M + '.' + m + '.' + p + pr; - else - ret = '>=' + M + '.' + m + '.' + p + pr + - ' <' + M + '.' + (+m + 1) + '.0-0'; - } else - ret = '>=' + M + '.' + m + '.' + p + pr + - ' <' + (+M + 1) + '.0.0-0'; - } else { - if (M === '0') { - if (m === '0') - ret = '=' + M + '.' + m + '.' + p; - else - ret = '>=' + M + '.' + m + '.' + p + '-0' + - ' <' + M + '.' + (+m + 1) + '.0-0'; - } else - ret = '>=' + M + '.' + m + '.' + p + '-0' + - ' <' + (+M + 1) + '.0.0-0'; - } + } else if (M === '0') + ret = '=' + M + '.' + m + '.' + p + pr; + else if (pr) + ret = '>=' + M + '.' + m + '.' + p + pr + + ' <' + (+M + 1) + '.0.0-0'; + else + ret = '>=' + M + '.' + m + '.' + p + '-0' + + ' <' + (+M + 1) + '.0.0-0'; debug('caret return', ret); return ret; diff --git a/test/index.js b/test/index.js index 2d15ed3f..abd9713d 100644 --- a/test/index.js +++ b/test/index.js @@ -410,7 +410,7 @@ test('\nvalid range test', function(t) { ['^1.2', '>=1.2.0-0 <2.0.0-0'], ['^0.0.1', '0.0.1'], ['^0.0.1-beta', '0.0.1-beta'], - ['^0.1.2', '>=0.1.2-0 <0.2.0-0'], + ['^0.1.2', '0.1.2'], ['^1.2.3', '>=1.2.3-0 <2.0.0-0'], ['^1.2.3-beta.4', '>=1.2.3-beta.4 <2.0.0-0'], ['<1', '<1.0.0-0'],