Extras: update semver.js & mod to v4.3.3

This commit is contained in:
Mottie 2015-03-28 15:00:32 -05:00
parent 3ee06dceb6
commit bde0a33f3c
3 changed files with 553 additions and 170 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,18 +1,17 @@
/**
Modified semver.js for node.js by R.Garrison (@Mottie)
Original by @isaacs: https://github.com/isaacs/node-semver
/*! Modified semver.js for node.js (v4.3.3, 3/27/2015) *//*
semver-mod.js by R.Garrison (@Mottie)
semver.js by @isaacs: https://github.com/isaacs/node-semver
( all modifications have been labeled )
*/
// ***** MODIFIED LINE BELOW *****
(function(){
// ***** MODIFIED LINE BELOW *****
var module = { exports : {} };
// export the class if we are in a Node-like system.
// ***** MODIFIED LINE BELOW *****
// if (typeof module === 'object' && module.exports === exports)
// ***** MODIFIED LINE BELOW *****
var exports = module.exports = SemVer;
var exports = module.exports = SemVer;
// The debug function is excluded entirely from the minified version.
/* nomin */ var debug;
@ -32,6 +31,9 @@ var module = { exports : {} };
// Not necessarily the package version of this code.
exports.SEMVER_SPEC_VERSION = '2.0.0';
var MAX_LENGTH = 256;
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
// The actual regexps go on exports.re
var re = exports.re = [];
var src = exports.src = [];
@ -150,18 +152,18 @@ var XRANGEPLAIN = R++;
src[XRANGEPLAIN] = '[v=\\s]*(' + src[XRANGEIDENTIFIER] + ')' +
'(?:\\.(' + src[XRANGEIDENTIFIER] + ')' +
'(?:\\.(' + src[XRANGEIDENTIFIER] + ')' +
'(?:(' + src[PRERELEASE] + ')' +
')?)?)?';
'(?:' + src[PRERELEASE] + ')?' +
src[BUILD] + '?' +
')?)?';
var XRANGEPLAINLOOSE = R++;
src[XRANGEPLAINLOOSE] = '[v=\\s]*(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
'(?:\\.(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
'(?:\\.(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
'(?:(' + src[PRERELEASELOOSE] + ')' +
')?)?)?';
'(?:' + src[PRERELEASELOOSE] + ')?' +
src[BUILD] + '?' +
')?)?';
// >=2.x, for example, means >=2.0.0-0
// <1.x would be the same as "<1.0.0-0", though.
var XRANGE = R++;
src[XRANGE] = '^' + src[GTLT] + '\\s*' + src[XRANGEPLAIN] + '$';
var XRANGELOOSE = R++;
@ -245,8 +247,24 @@ for (var i = 0; i < R; i++) {
exports.parse = parse;
function parse(version, loose) {
if (version instanceof SemVer)
return version;
if (typeof version !== 'string')
return null;
if (version.length > MAX_LENGTH)
return null;
var r = loose ? re[LOOSE] : re[FULL];
return (r.test(version)) ? new SemVer(version, loose) : null;
if (!r.test(version))
return null;
try {
return new SemVer(version, loose);
} catch (er) {
return null;
}
}
exports.valid = valid;
@ -258,7 +276,7 @@ function valid(version, loose) {
exports.clean = clean;
function clean(version, loose) {
var s = parse(version, loose);
var s = parse(version.trim().replace(/^[=v]+/, ''), loose);
return s ? s.version : null;
}
@ -271,8 +289,13 @@ function SemVer(version, loose) {
return version;
else
version = version.version;
} else if (typeof version !== 'string') {
throw new TypeError('Invalid Version: ' + version);
}
if (version.length > MAX_LENGTH)
throw new TypeError('version is longer than ' + MAX_LENGTH + ' characters')
if (!(this instanceof SemVer))
return new SemVer(version, loose);
@ -290,12 +313,26 @@ function SemVer(version, loose) {
this.minor = +m[2];
this.patch = +m[3];
if (this.major > MAX_SAFE_INTEGER || this.major < 0)
throw new TypeError('Invalid major version')
if (this.minor > MAX_SAFE_INTEGER || this.minor < 0)
throw new TypeError('Invalid minor version')
if (this.patch > MAX_SAFE_INTEGER || this.patch < 0)
throw new TypeError('Invalid patch version')
// numberify any prerelease numeric ids
if (!m[4])
this.prerelease = [];
else
this.prerelease = m[4].split('.').map(function(id) {
return (/^[0-9]+$/.test(id)) ? +id : id;
if (/^[0-9]+$/.test(id)) {
var num = +id
if (num >= 0 && num < MAX_SAFE_INTEGER)
return num
}
return id;
});
this.build = m[5] ? m[5].split('.') : [];
@ -343,7 +380,7 @@ SemVer.prototype.comparePre = function(other) {
return -1;
else if (!this.prerelease.length && other.prerelease.length)
return 1;
else if (!this.prerelease.lenth && !other.prerelease.length)
else if (!this.prerelease.length && !other.prerelease.length)
return 0;
var i = 0;
@ -364,19 +401,72 @@ SemVer.prototype.comparePre = function(other) {
} while (++i);
};
SemVer.prototype.inc = function(release) {
// preminor will bump the version up to the next minor release, and immediately
// down to pre-release. premajor and prepatch work the same way.
SemVer.prototype.inc = function(release, identifier) {
switch (release) {
case 'major':
case 'premajor':
this.prerelease.length = 0;
this.patch = 0;
this.minor = 0;
this.major++;
this.minor = -1;
case 'minor':
this.inc('pre', identifier);
break;
case 'preminor':
this.prerelease.length = 0;
this.patch = 0;
this.minor++;
this.patch = -1;
case 'patch':
this.patch++;
this.inc('pre', identifier);
break;
case 'prepatch':
// 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.inc('patch', identifier);
this.inc('pre', identifier);
break;
// If the input is a non-prerelease version, this acts the same as
// prepatch.
case 'prerelease':
if (this.prerelease.length === 0)
this.inc('patch', identifier);
this.inc('pre', identifier);
break;
case 'major':
// If this is a pre-major version, bump up to the same major version.
// Otherwise increment major.
// 1.0.0-5 bumps to 1.0.0
// 1.1.0 bumps to 2.0.0
if (this.minor !== 0 || this.patch !== 0 || this.prerelease.length === 0)
this.major++;
this.minor = 0;
this.patch = 0;
this.prerelease = [];
break;
case 'prerelease':
case 'minor':
// If this is a pre-minor version, bump up to the same minor version.
// Otherwise increment minor.
// 1.2.0-5 bumps to 1.2.0
// 1.2.1 bumps to 1.3.0
if (this.patch !== 0 || this.prerelease.length === 0)
this.minor++;
this.patch = 0;
this.prerelease = [];
break;
case 'patch':
// If this is not a pre-release version, it will increment the patch.
// If it is a pre-release it will bump up to the same patch version.
// 1.2.0-5 patches to 1.2.0
// 1.2.0 patches to 1.2.1
if (this.prerelease.length === 0)
this.patch++;
this.prerelease = [];
break;
// This probably shouldn't be used publicly.
// 1.0.0 "pre" would become 1.0.0-0 which is the wrong direction.
case 'pre':
if (this.prerelease.length === 0)
this.prerelease = [0];
else {
@ -390,6 +480,15 @@ SemVer.prototype.inc = function(release) {
if (i === -1) // didn't increment anything
this.prerelease.push(0);
}
if (identifier) {
// 1.2.0-beta.1 bumps to 1.2.0-beta.2,
// 1.2.0-beta.fooblz or 1.2.0-beta bumps to 1.2.0-beta.0
if (this.prerelease[0] === identifier) {
if (isNaN(this.prerelease[1]))
this.prerelease = [identifier, 0];
} else
this.prerelease = [identifier, 0];
}
break;
default:
@ -400,14 +499,46 @@ SemVer.prototype.inc = function(release) {
};
exports.inc = inc;
function inc(version, release, loose) {
function inc(version, release, loose, identifier) {
if (typeof(loose) === 'string') {
identifier = loose;
loose = undefined;
}
try {
return new SemVer(version, loose).inc(release).version;
return new SemVer(version, loose).inc(release, identifier).version;
} catch (er) {
return null;
}
}
exports.diff = diff;
function diff(version1, version2) {
if (eq(version1, version2)) {
return null;
} else {
var v1 = parse(version1);
var v2 = parse(version2);
if (v1.prerelease.length || v2.prerelease.length) {
for (var key in v1) {
if (key === 'major' || key === 'minor' || key === 'patch') {
if (v1[key] !== v2[key]) {
return 'pre'+key;
}
}
}
return 'prerelease';
}
for (var key in v1) {
if (key === 'major' || key === 'minor' || key === 'patch') {
if (v1[key] !== v2[key]) {
return key;
}
}
}
}
}
exports.compareIdentifiers = compareIdentifiers;
var numeric = /^[0-9]+$/;
@ -432,6 +563,21 @@ function rcompareIdentifiers(a, b) {
return compareIdentifiers(b, a);
}
exports.major = major;
function major(a, loose) {
return new SemVer(a, loose).major;
}
exports.minor = minor;
function minor(a, loose) {
return new SemVer(a, loose).minor;
}
exports.patch = patch;
function patch(a, loose) {
return new SemVer(a, loose).patch;
}
exports.compare = compare;
function compare(a, b, loose) {
return new SemVer(a, loose).compare(b);
@ -495,8 +641,16 @@ exports.cmp = cmp;
function cmp(a, op, b, loose) {
var ret;
switch (op) {
case '===': ret = a === b; break;
case '!==': ret = a !== b; break;
case '===':
if (typeof a === 'object') a = a.version;
if (typeof b === 'object') b = b.version;
ret = a === b;
break;
case '!==':
if (typeof a === 'object') a = a.version;
if (typeof b === 'object') b = b.version;
ret = a !== b;
break;
case '': case '=': case '==': ret = eq(a, b, loose); break;
case '!=': ret = neq(a, b, loose); break;
case '>': ret = gt(a, b, loose); break;
@ -528,6 +682,8 @@ function Comparator(comp, loose) {
this.value = '';
else
this.value = this.operator + this.semver.version;
debug('comp', this);
}
var ANY = {};
@ -539,24 +695,14 @@ Comparator.prototype.parse = function(comp) {
throw new TypeError('Invalid comparator: ' + comp);
this.operator = m[1];
if (this.operator === '=')
this.operator = '';
// if it literally is just '>' or '' then allow anything.
if (!m[2])
this.semver = ANY;
else {
else
this.semver = new SemVer(m[2], this.loose);
// <1.2.3-rc DOES allow 1.2.3-beta (has prerelease)
// >=1.2.3 DOES NOT allow 1.2.3-beta
// <=1.2.3 DOES allow 1.2.3-beta
// However, <1.2.3 does NOT allow 1.2.3-beta,
// even though `1.2.3-beta < 1.2.3`
// The assumption is that the 1.2.3 version has something you
// *don't* want, so we push the prerelease down to the minimum.
if (this.operator === '<' && !this.semver.prerelease.length) {
this.semver.prerelease = ['0'];
this.semver.format();
}
}
};
Comparator.prototype.inspect = function() {
@ -569,8 +715,14 @@ Comparator.prototype.toString = function() {
Comparator.prototype.test = function(version) {
debug('Comparator.test', version, this.loose);
return (this.semver === ANY) ? true :
cmp(version, this.operator, this.semver, this.loose);
if (this.semver === ANY)
return true;
if (typeof version === 'string')
version = new SemVer(version, this.loose);
return cmp(version, this.operator, this.semver, this.loose);
};
@ -707,20 +859,20 @@ function replaceTilde(comp, loose) {
if (isX(M))
ret = '';
else if (isX(m))
ret = '>=' + M + '.0.0-0 <' + (+M + 1) + '.0.0-0';
ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
else if (isX(p))
// ~1.2 == >=1.2.0- <1.3.0-
ret = '>=' + M + '.' + m + '.0-0 <' + M + '.' + (+m + 1) + '.0-0';
ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
else if (pr) {
debug('replaceTilde pr', pr);
if (pr.charAt(0) !== '-')
pr = '-' + pr;
ret = '>=' + M + '.' + m + '.' + p + pr +
' <' + M + '.' + (+m + 1) + '.0-0';
' <' + M + '.' + (+m + 1) + '.0';
} else
// ~1.2.3 == >=1.2.3-0 <1.3.0-0
ret = '>=' + M + '.' + m + '.' + p + '-0' +
' <' + M + '.' + (+m + 1) + '.0-0';
// ~1.2.3 == >=1.2.3 <1.3.0
ret = '>=' + M + '.' + m + '.' + p +
' <' + M + '.' + (+m + 1) + '.0';
debug('tilde return', ret);
return ret;
@ -740,6 +892,7 @@ function replaceCarets(comp, loose) {
}
function replaceCaret(comp, loose) {
debug('caret', comp, loose);
var r = loose ? re[CARETLOOSE] : re[CARET];
return comp.replace(r, function(_, M, m, p, pr) {
debug('caret', comp, _, M, m, p, pr);
@ -748,35 +901,38 @@ function replaceCaret(comp, loose) {
if (isX(M))
ret = '';
else if (isX(m))
ret = '>=' + M + '.0.0-0 <' + (+M + 1) + '.0.0-0';
ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
else if (isX(p)) {
if (M === '0')
ret = '>=' + M + '.' + m + '.0-0 <' + M + '.' + (+m + 1) + '.0-0';
ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
else
ret = '>=' + M + '.' + m + '.0-0 <' + (+M + 1) + '.0.0-0';
ret = '>=' + M + '.' + m + '.0 <' + (+M + 1) + '.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;
ret = '>=' + M + '.' + m + '.' + p + pr +
' <' + M + '.' + m + '.' + (+p + 1);
else
ret = '>=' + M + '.' + m + '.' + p + pr +
' <' + M + '.' + (+m + 1) + '.0-0';
' <' + M + '.' + (+m + 1) + '.0';
} else
ret = '>=' + M + '.' + m + '.' + p + pr +
' <' + (+M + 1) + '.0.0-0';
' <' + (+M + 1) + '.0.0';
} else {
debug('no pr');
if (M === '0') {
if (m === '0')
ret = '=' + M + '.' + m + '.' + p;
ret = '>=' + M + '.' + m + '.' + p +
' <' + M + '.' + m + '.' + (+p + 1);
else
ret = '>=' + M + '.' + m + '.' + p + '-0' +
' <' + M + '.' + (+m + 1) + '.0-0';
ret = '>=' + M + '.' + m + '.' + p +
' <' + M + '.' + (+m + 1) + '.0';
} else
ret = '>=' + M + '.' + m + '.' + p + '-0' +
' <' + (+M + 1) + '.0.0-0';
ret = '>=' + M + '.' + m + '.' + p +
' <' + (+M + 1) + '.0.0';
}
debug('caret return', ret);
@ -804,23 +960,27 @@ function replaceXRange(comp, loose) {
if (gtlt === '=' && anyX)
gtlt = '';
if (gtlt && anyX) {
// replace X with 0, and then append the -0 min-prerelease
if (xM)
M = 0;
if (xM) {
if (gtlt === '>' || gtlt === '<') {
// nothing is allowed
ret = '<0.0.0';
} else {
// nothing is forbidden
ret = '*';
}
} else if (gtlt && anyX) {
// replace X with 0
if (xm)
m = 0;
if (xp)
p = 0;
if (gtlt === '>') {
// >1 => >=2.0.0-0
// >1.2 => >=1.3.0-0
// >1.2.3 => >= 1.2.4-0
// >1 => >=2.0.0
// >1.2 => >=1.3.0
// >1.2.3 => >= 1.2.4
gtlt = '>=';
if (xM) {
// no change
} else if (xm) {
if (xm) {
M = +M + 1;
m = 0;
p = 0;
@ -828,20 +988,21 @@ function replaceXRange(comp, loose) {
m = +m + 1;
p = 0;
}
} else if (gtlt === '<=') {
// <=0.7.x is actually <0.8.0, since any 0.7.x should
// pass. Similarly, <=7.x is actually <8.0.0, etc.
gtlt = '<'
if (xm)
M = +M + 1
else
m = +m + 1
}
ret = gtlt + M + '.' + m + '.' + p + '-0';
} else if (xM) {
// allow any
ret = '*';
ret = gtlt + M + '.' + m + '.' + p;
} else if (xm) {
// append '-0' onto the version, otherwise
// '1.x.x' matches '2.0.0-beta', since the tag
// *lowers* the version value
ret = '>=' + M + '.0.0-0 <' + (+M + 1) + '.0.0-0';
ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
} else if (xp) {
ret = '>=' + M + '.' + m + '.0-0 <' + M + '.' + (+m + 1) + '.0-0';
ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
}
debug('xRange return', ret);
@ -860,9 +1021,9 @@ function replaceStars(comp, loose) {
// This function is passed to string.replace(re[HYPHENRANGE])
// M, m, patch, prerelease, build
// 1.2 - 3.4.5 => >=1.2.0-0 <=3.4.5
// 1.2.3 - 3.4 => >=1.2.0-0 <3.5.0-0 Any 3.4.x will do
// 1.2 - 3.4 => >=1.2.0-0 <3.5.0-0
// 1.2 - 3.4.5 => >=1.2.0 <=3.4.5
// 1.2.3 - 3.4 => >=1.2.0 <3.5.0 Any 3.4.x will do
// 1.2 - 3.4 => >=1.2.0 <3.5.0
function hyphenReplace($0,
from, fM, fm, fp, fpr, fb,
to, tM, tm, tp, tpr, tb) {
@ -870,18 +1031,18 @@ function hyphenReplace($0,
if (isX(fM))
from = '';
else if (isX(fm))
from = '>=' + fM + '.0.0-0';
from = '>=' + fM + '.0.0';
else if (isX(fp))
from = '>=' + fM + '.' + fm + '.0-0';
from = '>=' + fM + '.' + fm + '.0';
else
from = '>=' + from;
if (isX(tM))
to = '';
else if (isX(tm))
to = '<' + (+tM + 1) + '.0.0-0';
to = '<' + (+tM + 1) + '.0.0';
else if (isX(tp))
to = '<' + tM + '.' + (+tm + 1) + '.0-0';
to = '<' + tM + '.' + (+tm + 1) + '.0';
else if (tpr)
to = '<=' + tM + '.' + tm + '.' + tp + '-' + tpr;
else
@ -895,6 +1056,10 @@ function hyphenReplace($0,
Range.prototype.test = function(version) {
if (!version)
return false;
if (typeof version === 'string')
version = new SemVer(version, this.loose);
for (var i = 0; i < this.set.length; i++) {
if (testSet(this.set[i], version))
return true;
@ -907,6 +1072,31 @@ function testSet(set, version) {
if (!set[i].test(version))
return false;
}
if (version.prerelease.length) {
// Find the set of versions that are allowed to have prereleases
// For example, ^1.2.3-pr.1 desugars to >=1.2.3-pr.1 <2.0.0
// That should allow `1.2.3-pr.2` to pass.
// However, `1.2.4-alpha.notready` should NOT be allowed,
// even though it's within the range set by the comparators.
for (var i = 0; i < set.length; i++) {
debug(set[i].semver);
if (set[i].semver === ANY)
return true;
if (set[i].semver.prerelease.length > 0) {
var allowed = set[i].semver;
if (allowed.major === version.major &&
allowed.minor === version.minor &&
allowed.patch === version.patch)
return true;
}
}
// Version has a -pre, but it's not one of the ones we like.
return false;
}
return true;
}
@ -1022,5 +1212,6 @@ function outside(version, range, hilo, loose) {
// Use the define() function if we're in AMD land
if (typeof define === 'function' && define.amd)
define(exports);
// ***** MODIFIED LINE BELOW *****
})();

View File

@ -20,6 +20,9 @@ if (typeof module === 'object' && module.exports === exports)
// Not necessarily the package version of this code.
exports.SEMVER_SPEC_VERSION = '2.0.0';
var MAX_LENGTH = 256;
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
// The actual regexps go on exports.re
var re = exports.re = [];
var src = exports.src = [];
@ -138,18 +141,18 @@ var XRANGEPLAIN = R++;
src[XRANGEPLAIN] = '[v=\\s]*(' + src[XRANGEIDENTIFIER] + ')' +
'(?:\\.(' + src[XRANGEIDENTIFIER] + ')' +
'(?:\\.(' + src[XRANGEIDENTIFIER] + ')' +
'(?:(' + src[PRERELEASE] + ')' +
')?)?)?';
'(?:' + src[PRERELEASE] + ')?' +
src[BUILD] + '?' +
')?)?';
var XRANGEPLAINLOOSE = R++;
src[XRANGEPLAINLOOSE] = '[v=\\s]*(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
'(?:\\.(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
'(?:\\.(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
'(?:(' + src[PRERELEASELOOSE] + ')' +
')?)?)?';
'(?:' + src[PRERELEASELOOSE] + ')?' +
src[BUILD] + '?' +
')?)?';
// >=2.x, for example, means >=2.0.0-0
// <1.x would be the same as "<1.0.0-0", though.
var XRANGE = R++;
src[XRANGE] = '^' + src[GTLT] + '\\s*' + src[XRANGEPLAIN] + '$';
var XRANGELOOSE = R++;
@ -233,8 +236,24 @@ for (var i = 0; i < R; i++) {
exports.parse = parse;
function parse(version, loose) {
if (version instanceof SemVer)
return version;
if (typeof version !== 'string')
return null;
if (version.length > MAX_LENGTH)
return null;
var r = loose ? re[LOOSE] : re[FULL];
return (r.test(version)) ? new SemVer(version, loose) : null;
if (!r.test(version))
return null;
try {
return new SemVer(version, loose);
} catch (er) {
return null;
}
}
exports.valid = valid;
@ -246,7 +265,7 @@ function valid(version, loose) {
exports.clean = clean;
function clean(version, loose) {
var s = parse(version, loose);
var s = parse(version.trim().replace(/^[=v]+/, ''), loose);
return s ? s.version : null;
}
@ -258,8 +277,13 @@ function SemVer(version, loose) {
return version;
else
version = version.version;
} else if (typeof version !== 'string') {
throw new TypeError('Invalid Version: ' + version);
}
if (version.length > MAX_LENGTH)
throw new TypeError('version is longer than ' + MAX_LENGTH + ' characters')
if (!(this instanceof SemVer))
return new SemVer(version, loose);
@ -277,12 +301,26 @@ function SemVer(version, loose) {
this.minor = +m[2];
this.patch = +m[3];
if (this.major > MAX_SAFE_INTEGER || this.major < 0)
throw new TypeError('Invalid major version')
if (this.minor > MAX_SAFE_INTEGER || this.minor < 0)
throw new TypeError('Invalid minor version')
if (this.patch > MAX_SAFE_INTEGER || this.patch < 0)
throw new TypeError('Invalid patch version')
// numberify any prerelease numeric ids
if (!m[4])
this.prerelease = [];
else
this.prerelease = m[4].split('.').map(function(id) {
return (/^[0-9]+$/.test(id)) ? +id : id;
if (/^[0-9]+$/.test(id)) {
var num = +id
if (num >= 0 && num < MAX_SAFE_INTEGER)
return num
}
return id;
});
this.build = m[5] ? m[5].split('.') : [];
@ -330,7 +368,7 @@ SemVer.prototype.comparePre = function(other) {
return -1;
else if (!this.prerelease.length && other.prerelease.length)
return 1;
else if (!this.prerelease.lenth && !other.prerelease.length)
else if (!this.prerelease.length && !other.prerelease.length)
return 0;
var i = 0;
@ -351,19 +389,72 @@ SemVer.prototype.comparePre = function(other) {
} while (++i);
};
SemVer.prototype.inc = function(release) {
// preminor will bump the version up to the next minor release, and immediately
// down to pre-release. premajor and prepatch work the same way.
SemVer.prototype.inc = function(release, identifier) {
switch (release) {
case 'major':
case 'premajor':
this.prerelease.length = 0;
this.patch = 0;
this.minor = 0;
this.major++;
this.minor = -1;
case 'minor':
this.inc('pre', identifier);
break;
case 'preminor':
this.prerelease.length = 0;
this.patch = 0;
this.minor++;
this.patch = -1;
case 'patch':
this.patch++;
this.inc('pre', identifier);
break;
case 'prepatch':
// 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.inc('patch', identifier);
this.inc('pre', identifier);
break;
// If the input is a non-prerelease version, this acts the same as
// prepatch.
case 'prerelease':
if (this.prerelease.length === 0)
this.inc('patch', identifier);
this.inc('pre', identifier);
break;
case 'major':
// If this is a pre-major version, bump up to the same major version.
// Otherwise increment major.
// 1.0.0-5 bumps to 1.0.0
// 1.1.0 bumps to 2.0.0
if (this.minor !== 0 || this.patch !== 0 || this.prerelease.length === 0)
this.major++;
this.minor = 0;
this.patch = 0;
this.prerelease = [];
break;
case 'prerelease':
case 'minor':
// If this is a pre-minor version, bump up to the same minor version.
// Otherwise increment minor.
// 1.2.0-5 bumps to 1.2.0
// 1.2.1 bumps to 1.3.0
if (this.patch !== 0 || this.prerelease.length === 0)
this.minor++;
this.patch = 0;
this.prerelease = [];
break;
case 'patch':
// If this is not a pre-release version, it will increment the patch.
// If it is a pre-release it will bump up to the same patch version.
// 1.2.0-5 patches to 1.2.0
// 1.2.0 patches to 1.2.1
if (this.prerelease.length === 0)
this.patch++;
this.prerelease = [];
break;
// This probably shouldn't be used publicly.
// 1.0.0 "pre" would become 1.0.0-0 which is the wrong direction.
case 'pre':
if (this.prerelease.length === 0)
this.prerelease = [0];
else {
@ -377,6 +468,15 @@ SemVer.prototype.inc = function(release) {
if (i === -1) // didn't increment anything
this.prerelease.push(0);
}
if (identifier) {
// 1.2.0-beta.1 bumps to 1.2.0-beta.2,
// 1.2.0-beta.fooblz or 1.2.0-beta bumps to 1.2.0-beta.0
if (this.prerelease[0] === identifier) {
if (isNaN(this.prerelease[1]))
this.prerelease = [identifier, 0];
} else
this.prerelease = [identifier, 0];
}
break;
default:
@ -387,14 +487,46 @@ SemVer.prototype.inc = function(release) {
};
exports.inc = inc;
function inc(version, release, loose) {
function inc(version, release, loose, identifier) {
if (typeof(loose) === 'string') {
identifier = loose;
loose = undefined;
}
try {
return new SemVer(version, loose).inc(release).version;
return new SemVer(version, loose).inc(release, identifier).version;
} catch (er) {
return null;
}
}
exports.diff = diff;
function diff(version1, version2) {
if (eq(version1, version2)) {
return null;
} else {
var v1 = parse(version1);
var v2 = parse(version2);
if (v1.prerelease.length || v2.prerelease.length) {
for (var key in v1) {
if (key === 'major' || key === 'minor' || key === 'patch') {
if (v1[key] !== v2[key]) {
return 'pre'+key;
}
}
}
return 'prerelease';
}
for (var key in v1) {
if (key === 'major' || key === 'minor' || key === 'patch') {
if (v1[key] !== v2[key]) {
return key;
}
}
}
}
}
exports.compareIdentifiers = compareIdentifiers;
var numeric = /^[0-9]+$/;
@ -419,6 +551,21 @@ function rcompareIdentifiers(a, b) {
return compareIdentifiers(b, a);
}
exports.major = major;
function major(a, loose) {
return new SemVer(a, loose).major;
}
exports.minor = minor;
function minor(a, loose) {
return new SemVer(a, loose).minor;
}
exports.patch = patch;
function patch(a, loose) {
return new SemVer(a, loose).patch;
}
exports.compare = compare;
function compare(a, b, loose) {
return new SemVer(a, loose).compare(b);
@ -482,8 +629,16 @@ exports.cmp = cmp;
function cmp(a, op, b, loose) {
var ret;
switch (op) {
case '===': ret = a === b; break;
case '!==': ret = a !== b; break;
case '===':
if (typeof a === 'object') a = a.version;
if (typeof b === 'object') b = b.version;
ret = a === b;
break;
case '!==':
if (typeof a === 'object') a = a.version;
if (typeof b === 'object') b = b.version;
ret = a !== b;
break;
case '': case '=': case '==': ret = eq(a, b, loose); break;
case '!=': ret = neq(a, b, loose); break;
case '>': ret = gt(a, b, loose); break;
@ -515,6 +670,8 @@ function Comparator(comp, loose) {
this.value = '';
else
this.value = this.operator + this.semver.version;
debug('comp', this);
}
var ANY = {};
@ -526,24 +683,14 @@ Comparator.prototype.parse = function(comp) {
throw new TypeError('Invalid comparator: ' + comp);
this.operator = m[1];
if (this.operator === '=')
this.operator = '';
// if it literally is just '>' or '' then allow anything.
if (!m[2])
this.semver = ANY;
else {
else
this.semver = new SemVer(m[2], this.loose);
// <1.2.3-rc DOES allow 1.2.3-beta (has prerelease)
// >=1.2.3 DOES NOT allow 1.2.3-beta
// <=1.2.3 DOES allow 1.2.3-beta
// However, <1.2.3 does NOT allow 1.2.3-beta,
// even though `1.2.3-beta < 1.2.3`
// The assumption is that the 1.2.3 version has something you
// *don't* want, so we push the prerelease down to the minimum.
if (this.operator === '<' && !this.semver.prerelease.length) {
this.semver.prerelease = ['0'];
this.semver.format();
}
}
};
Comparator.prototype.inspect = function() {
@ -556,8 +703,14 @@ Comparator.prototype.toString = function() {
Comparator.prototype.test = function(version) {
debug('Comparator.test', version, this.loose);
return (this.semver === ANY) ? true :
cmp(version, this.operator, this.semver, this.loose);
if (this.semver === ANY)
return true;
if (typeof version === 'string')
version = new SemVer(version, this.loose);
return cmp(version, this.operator, this.semver, this.loose);
};
@ -694,20 +847,20 @@ function replaceTilde(comp, loose) {
if (isX(M))
ret = '';
else if (isX(m))
ret = '>=' + M + '.0.0-0 <' + (+M + 1) + '.0.0-0';
ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
else if (isX(p))
// ~1.2 == >=1.2.0- <1.3.0-
ret = '>=' + M + '.' + m + '.0-0 <' + M + '.' + (+m + 1) + '.0-0';
ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
else if (pr) {
debug('replaceTilde pr', pr);
if (pr.charAt(0) !== '-')
pr = '-' + pr;
ret = '>=' + M + '.' + m + '.' + p + pr +
' <' + M + '.' + (+m + 1) + '.0-0';
' <' + M + '.' + (+m + 1) + '.0';
} else
// ~1.2.3 == >=1.2.3-0 <1.3.0-0
ret = '>=' + M + '.' + m + '.' + p + '-0' +
' <' + M + '.' + (+m + 1) + '.0-0';
// ~1.2.3 == >=1.2.3 <1.3.0
ret = '>=' + M + '.' + m + '.' + p +
' <' + M + '.' + (+m + 1) + '.0';
debug('tilde return', ret);
return ret;
@ -727,6 +880,7 @@ function replaceCarets(comp, loose) {
}
function replaceCaret(comp, loose) {
debug('caret', comp, loose);
var r = loose ? re[CARETLOOSE] : re[CARET];
return comp.replace(r, function(_, M, m, p, pr) {
debug('caret', comp, _, M, m, p, pr);
@ -735,35 +889,38 @@ function replaceCaret(comp, loose) {
if (isX(M))
ret = '';
else if (isX(m))
ret = '>=' + M + '.0.0-0 <' + (+M + 1) + '.0.0-0';
ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
else if (isX(p)) {
if (M === '0')
ret = '>=' + M + '.' + m + '.0-0 <' + M + '.' + (+m + 1) + '.0-0';
ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
else
ret = '>=' + M + '.' + m + '.0-0 <' + (+M + 1) + '.0.0-0';
ret = '>=' + M + '.' + m + '.0 <' + (+M + 1) + '.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;
ret = '>=' + M + '.' + m + '.' + p + pr +
' <' + M + '.' + m + '.' + (+p + 1);
else
ret = '>=' + M + '.' + m + '.' + p + pr +
' <' + M + '.' + (+m + 1) + '.0-0';
' <' + M + '.' + (+m + 1) + '.0';
} else
ret = '>=' + M + '.' + m + '.' + p + pr +
' <' + (+M + 1) + '.0.0-0';
' <' + (+M + 1) + '.0.0';
} else {
debug('no pr');
if (M === '0') {
if (m === '0')
ret = '=' + M + '.' + m + '.' + p;
ret = '>=' + M + '.' + m + '.' + p +
' <' + M + '.' + m + '.' + (+p + 1);
else
ret = '>=' + M + '.' + m + '.' + p + '-0' +
' <' + M + '.' + (+m + 1) + '.0-0';
ret = '>=' + M + '.' + m + '.' + p +
' <' + M + '.' + (+m + 1) + '.0';
} else
ret = '>=' + M + '.' + m + '.' + p + '-0' +
' <' + (+M + 1) + '.0.0-0';
ret = '>=' + M + '.' + m + '.' + p +
' <' + (+M + 1) + '.0.0';
}
debug('caret return', ret);
@ -791,23 +948,27 @@ function replaceXRange(comp, loose) {
if (gtlt === '=' && anyX)
gtlt = '';
if (gtlt && anyX) {
// replace X with 0, and then append the -0 min-prerelease
if (xM)
M = 0;
if (xM) {
if (gtlt === '>' || gtlt === '<') {
// nothing is allowed
ret = '<0.0.0';
} else {
// nothing is forbidden
ret = '*';
}
} else if (gtlt && anyX) {
// replace X with 0
if (xm)
m = 0;
if (xp)
p = 0;
if (gtlt === '>') {
// >1 => >=2.0.0-0
// >1.2 => >=1.3.0-0
// >1.2.3 => >= 1.2.4-0
// >1 => >=2.0.0
// >1.2 => >=1.3.0
// >1.2.3 => >= 1.2.4
gtlt = '>=';
if (xM) {
// no change
} else if (xm) {
if (xm) {
M = +M + 1;
m = 0;
p = 0;
@ -815,20 +976,21 @@ function replaceXRange(comp, loose) {
m = +m + 1;
p = 0;
}
} else if (gtlt === '<=') {
// <=0.7.x is actually <0.8.0, since any 0.7.x should
// pass. Similarly, <=7.x is actually <8.0.0, etc.
gtlt = '<'
if (xm)
M = +M + 1
else
m = +m + 1
}
ret = gtlt + M + '.' + m + '.' + p + '-0';
} else if (xM) {
// allow any
ret = '*';
ret = gtlt + M + '.' + m + '.' + p;
} else if (xm) {
// append '-0' onto the version, otherwise
// '1.x.x' matches '2.0.0-beta', since the tag
// *lowers* the version value
ret = '>=' + M + '.0.0-0 <' + (+M + 1) + '.0.0-0';
ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
} else if (xp) {
ret = '>=' + M + '.' + m + '.0-0 <' + M + '.' + (+m + 1) + '.0-0';
ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
}
debug('xRange return', ret);
@ -847,9 +1009,9 @@ function replaceStars(comp, loose) {
// This function is passed to string.replace(re[HYPHENRANGE])
// M, m, patch, prerelease, build
// 1.2 - 3.4.5 => >=1.2.0-0 <=3.4.5
// 1.2.3 - 3.4 => >=1.2.0-0 <3.5.0-0 Any 3.4.x will do
// 1.2 - 3.4 => >=1.2.0-0 <3.5.0-0
// 1.2 - 3.4.5 => >=1.2.0 <=3.4.5
// 1.2.3 - 3.4 => >=1.2.0 <3.5.0 Any 3.4.x will do
// 1.2 - 3.4 => >=1.2.0 <3.5.0
function hyphenReplace($0,
from, fM, fm, fp, fpr, fb,
to, tM, tm, tp, tpr, tb) {
@ -857,18 +1019,18 @@ function hyphenReplace($0,
if (isX(fM))
from = '';
else if (isX(fm))
from = '>=' + fM + '.0.0-0';
from = '>=' + fM + '.0.0';
else if (isX(fp))
from = '>=' + fM + '.' + fm + '.0-0';
from = '>=' + fM + '.' + fm + '.0';
else
from = '>=' + from;
if (isX(tM))
to = '';
else if (isX(tm))
to = '<' + (+tM + 1) + '.0.0-0';
to = '<' + (+tM + 1) + '.0.0';
else if (isX(tp))
to = '<' + tM + '.' + (+tm + 1) + '.0-0';
to = '<' + tM + '.' + (+tm + 1) + '.0';
else if (tpr)
to = '<=' + tM + '.' + tm + '.' + tp + '-' + tpr;
else
@ -882,6 +1044,10 @@ function hyphenReplace($0,
Range.prototype.test = function(version) {
if (!version)
return false;
if (typeof version === 'string')
version = new SemVer(version, this.loose);
for (var i = 0; i < this.set.length; i++) {
if (testSet(this.set[i], version))
return true;
@ -894,6 +1060,31 @@ function testSet(set, version) {
if (!set[i].test(version))
return false;
}
if (version.prerelease.length) {
// Find the set of versions that are allowed to have prereleases
// For example, ^1.2.3-pr.1 desugars to >=1.2.3-pr.1 <2.0.0
// That should allow `1.2.3-pr.2` to pass.
// However, `1.2.4-alpha.notready` should NOT be allowed,
// even though it's within the range set by the comparators.
for (var i = 0; i < set.length; i++) {
debug(set[i].semver);
if (set[i].semver === ANY)
return true;
if (set[i].semver.prerelease.length > 0) {
var allowed = set[i].semver;
if (allowed.major === version.major &&
allowed.minor === version.minor &&
allowed.patch === version.patch)
return true;
}
}
// Version has a -pre, but it's not one of the ones we like.
return false;
}
return true;
}