1 exports = module.exports = SemVer;
3 // The debug function is excluded entirely from the minified version.
5 /* nomin */ if (typeof process === 'object' &&
6 /* nomin */ process.env &&
7 /* nomin */ process.env.NODE_DEBUG &&
8 /* nomin */ /\bsemver\b/i.test(process.env.NODE_DEBUG))
9 /* nomin */ debug = function() {
10 /* nomin */ var args = Array.prototype.slice.call(arguments, 0);
11 /* nomin */ args.unshift('SEMVER');
12 /* nomin */ console.log.apply(console, args);
15 /* nomin */ debug = function() {};
17 // Note: this is the semver.org version of the spec that it implements
18 // Not necessarily the package version of this code.
19 exports.SEMVER_SPEC_VERSION = '2.0.0';
22 var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
24 // The actual regexps go on exports.re
25 var re = exports.re = [];
26 var src = exports.src = [];
29 // The following Regular Expressions can be used for tokenizing,
30 // validating, and parsing SemVer version strings.
32 // ## Numeric Identifier
33 // A single `0`, or a non-zero digit followed by zero or more digits.
35 var NUMERICIDENTIFIER = R++;
36 src[NUMERICIDENTIFIER] = '0|[1-9]\\d*';
37 var NUMERICIDENTIFIERLOOSE = R++;
38 src[NUMERICIDENTIFIERLOOSE] = '[0-9]+';
41 // ## Non-numeric Identifier
42 // Zero or more digits, followed by a letter or hyphen, and then zero or
43 // more letters, digits, or hyphens.
45 var NONNUMERICIDENTIFIER = R++;
46 src[NONNUMERICIDENTIFIER] = '\\d*[a-zA-Z-][a-zA-Z0-9-]*';
50 // Three dot-separated numeric identifiers.
52 var MAINVERSION = R++;
53 src[MAINVERSION] = '(' + src[NUMERICIDENTIFIER] + ')\\.' +
54 '(' + src[NUMERICIDENTIFIER] + ')\\.' +
55 '(' + src[NUMERICIDENTIFIER] + ')';
57 var MAINVERSIONLOOSE = R++;
58 src[MAINVERSIONLOOSE] = '(' + src[NUMERICIDENTIFIERLOOSE] + ')\\.' +
59 '(' + src[NUMERICIDENTIFIERLOOSE] + ')\\.' +
60 '(' + src[NUMERICIDENTIFIERLOOSE] + ')';
62 // ## Pre-release Version Identifier
63 // A numeric identifier, or a non-numeric identifier.
65 var PRERELEASEIDENTIFIER = R++;
66 src[PRERELEASEIDENTIFIER] = '(?:' + src[NUMERICIDENTIFIER] +
67 '|' + src[NONNUMERICIDENTIFIER] + ')';
69 var PRERELEASEIDENTIFIERLOOSE = R++;
70 src[PRERELEASEIDENTIFIERLOOSE] = '(?:' + src[NUMERICIDENTIFIERLOOSE] +
71 '|' + src[NONNUMERICIDENTIFIER] + ')';
74 // ## Pre-release Version
75 // Hyphen, followed by one or more dot-separated pre-release version
79 src[PRERELEASE] = '(?:-(' + src[PRERELEASEIDENTIFIER] +
80 '(?:\\.' + src[PRERELEASEIDENTIFIER] + ')*))';
82 var PRERELEASELOOSE = R++;
83 src[PRERELEASELOOSE] = '(?:-?(' + src[PRERELEASEIDENTIFIERLOOSE] +
84 '(?:\\.' + src[PRERELEASEIDENTIFIERLOOSE] + ')*))';
86 // ## Build Metadata Identifier
87 // Any combination of digits, letters, or hyphens.
89 var BUILDIDENTIFIER = R++;
90 src[BUILDIDENTIFIER] = '[0-9A-Za-z-]+';
93 // Plus sign, followed by one or more period-separated build metadata
97 src[BUILD] = '(?:\\+(' + src[BUILDIDENTIFIER] +
98 '(?:\\.' + src[BUILDIDENTIFIER] + ')*))';
101 // ## Full Version String
102 // A main version, followed optionally by a pre-release version and
105 // Note that the only major, minor, patch, and pre-release sections of
106 // the version string are capturing groups. The build metadata is not a
107 // capturing group, because it should not ever be used in version
111 var FULLPLAIN = 'v?' + src[MAINVERSION] +
112 src[PRERELEASE] + '?' +
115 src[FULL] = '^' + FULLPLAIN + '$';
117 // like full, but allows v1.2.3 and =1.2.3, which people do sometimes.
118 // also, 1.0.0alpha1 (prerelease without the hyphen) which is pretty
119 // common in the npm registry.
120 var LOOSEPLAIN = '[v=\\s]*' + src[MAINVERSIONLOOSE] +
121 src[PRERELEASELOOSE] + '?' +
125 src[LOOSE] = '^' + LOOSEPLAIN + '$';
128 src[GTLT] = '((?:<|>)?=?)';
130 // Something like "2.*" or "1.2.x".
131 // Note that "x.x" is a valid xRange identifer, meaning "any version"
132 // Only the first item is strictly required.
133 var XRANGEIDENTIFIERLOOSE = R++;
134 src[XRANGEIDENTIFIERLOOSE] = src[NUMERICIDENTIFIERLOOSE] + '|x|X|\\*';
135 var XRANGEIDENTIFIER = R++;
136 src[XRANGEIDENTIFIER] = src[NUMERICIDENTIFIER] + '|x|X|\\*';
138 var XRANGEPLAIN = R++;
139 src[XRANGEPLAIN] = '[v=\\s]*(' + src[XRANGEIDENTIFIER] + ')' +
140 '(?:\\.(' + src[XRANGEIDENTIFIER] + ')' +
141 '(?:\\.(' + src[XRANGEIDENTIFIER] + ')' +
142 '(?:' + src[PRERELEASE] + ')?' +
146 var XRANGEPLAINLOOSE = R++;
147 src[XRANGEPLAINLOOSE] = '[v=\\s]*(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
148 '(?:\\.(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
149 '(?:\\.(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
150 '(?:' + src[PRERELEASELOOSE] + ')?' +
155 src[XRANGE] = '^' + src[GTLT] + '\\s*' + src[XRANGEPLAIN] + '$';
156 var XRANGELOOSE = R++;
157 src[XRANGELOOSE] = '^' + src[GTLT] + '\\s*' + src[XRANGEPLAINLOOSE] + '$';
160 // Meaning is "reasonably at or greater than"
162 src[LONETILDE] = '(?:~>?)';
165 src[TILDETRIM] = '(\\s*)' + src[LONETILDE] + '\\s+';
166 re[TILDETRIM] = new RegExp(src[TILDETRIM], 'g');
167 var tildeTrimReplace = '$1~';
170 src[TILDE] = '^' + src[LONETILDE] + src[XRANGEPLAIN] + '$';
171 var TILDELOOSE = R++;
172 src[TILDELOOSE] = '^' + src[LONETILDE] + src[XRANGEPLAINLOOSE] + '$';
175 // Meaning is "at least and backwards compatible with"
177 src[LONECARET] = '(?:\\^)';
180 src[CARETTRIM] = '(\\s*)' + src[LONECARET] + '\\s+';
181 re[CARETTRIM] = new RegExp(src[CARETTRIM], 'g');
182 var caretTrimReplace = '$1^';
185 src[CARET] = '^' + src[LONECARET] + src[XRANGEPLAIN] + '$';
186 var CARETLOOSE = R++;
187 src[CARETLOOSE] = '^' + src[LONECARET] + src[XRANGEPLAINLOOSE] + '$';
189 // A simple gt/lt/eq thing, or just "" to indicate "any version"
190 var COMPARATORLOOSE = R++;
191 src[COMPARATORLOOSE] = '^' + src[GTLT] + '\\s*(' + LOOSEPLAIN + ')$|^$';
192 var COMPARATOR = R++;
193 src[COMPARATOR] = '^' + src[GTLT] + '\\s*(' + FULLPLAIN + ')$|^$';
196 // An expression to strip any whitespace between the gtlt and the thing
197 // it modifies, so that `> 1.2.3` ==> `>1.2.3`
198 var COMPARATORTRIM = R++;
199 src[COMPARATORTRIM] = '(\\s*)' + src[GTLT] +
200 '\\s*(' + LOOSEPLAIN + '|' + src[XRANGEPLAIN] + ')';
202 // this one has to use the /g flag
203 re[COMPARATORTRIM] = new RegExp(src[COMPARATORTRIM], 'g');
204 var comparatorTrimReplace = '$1$2$3';
207 // Something like `1.2.3 - 1.2.4`
208 // Note that these all use the loose form, because they'll be
209 // checked against either the strict or loose comparator form
211 var HYPHENRANGE = R++;
212 src[HYPHENRANGE] = '^\\s*(' + src[XRANGEPLAIN] + ')' +
214 '(' + src[XRANGEPLAIN] + ')' +
217 var HYPHENRANGELOOSE = R++;
218 src[HYPHENRANGELOOSE] = '^\\s*(' + src[XRANGEPLAINLOOSE] + ')' +
220 '(' + src[XRANGEPLAINLOOSE] + ')' +
223 // Star ranges basically just allow anything at all.
225 src[STAR] = '(<|>)?=?\\s*\\*';
227 // Compile to actual regexp objects.
228 // All are flag-free, unless they were created above with a flag.
229 for (var i = 0; i < R; i++) {
232 re[i] = new RegExp(src[i]);
235 exports.parse = parse;
236 function parse(version, loose) {
237 if (version instanceof SemVer)
240 if (typeof version !== 'string')
243 if (version.length > MAX_LENGTH)
246 var r = loose ? re[LOOSE] : re[FULL];
247 if (!r.test(version))
251 return new SemVer(version, loose);
257 exports.valid = valid;
258 function valid(version, loose) {
259 var v = parse(version, loose);
260 return v ? v.version : null;
264 exports.clean = clean;
265 function clean(version, loose) {
266 var s = parse(version.trim().replace(/^[=v]+/, ''), loose);
267 return s ? s.version : null;
270 exports.SemVer = SemVer;
272 function SemVer(version, loose) {
273 if (version instanceof SemVer) {
274 if (version.loose === loose)
277 version = version.version;
278 } else if (typeof version !== 'string') {
279 throw new TypeError('Invalid Version: ' + version);
282 if (version.length > MAX_LENGTH)
283 throw new TypeError('version is longer than ' + MAX_LENGTH + ' characters')
285 if (!(this instanceof SemVer))
286 return new SemVer(version, loose);
288 debug('SemVer', version, loose);
290 var m = version.trim().match(loose ? re[LOOSE] : re[FULL]);
293 throw new TypeError('Invalid Version: ' + version);
297 // these are actually numbers
302 if (this.major > MAX_SAFE_INTEGER || this.major < 0)
303 throw new TypeError('Invalid major version')
305 if (this.minor > MAX_SAFE_INTEGER || this.minor < 0)
306 throw new TypeError('Invalid minor version')
308 if (this.patch > MAX_SAFE_INTEGER || this.patch < 0)
309 throw new TypeError('Invalid patch version')
311 // numberify any prerelease numeric ids
313 this.prerelease = [];
315 this.prerelease = m[4].split('.').map(function(id) {
316 if (/^[0-9]+$/.test(id)) {
318 if (num >= 0 && num < MAX_SAFE_INTEGER)
324 this.build = m[5] ? m[5].split('.') : [];
328 SemVer.prototype.format = function() {
329 this.version = this.major + '.' + this.minor + '.' + this.patch;
330 if (this.prerelease.length)
331 this.version += '-' + this.prerelease.join('.');
335 SemVer.prototype.toString = function() {
339 SemVer.prototype.compare = function(other) {
340 debug('SemVer.compare', this.version, this.loose, other);
341 if (!(other instanceof SemVer))
342 other = new SemVer(other, this.loose);
344 return this.compareMain(other) || this.comparePre(other);
347 SemVer.prototype.compareMain = function(other) {
348 if (!(other instanceof SemVer))
349 other = new SemVer(other, this.loose);
351 return compareIdentifiers(this.major, other.major) ||
352 compareIdentifiers(this.minor, other.minor) ||
353 compareIdentifiers(this.patch, other.patch);
356 SemVer.prototype.comparePre = function(other) {
357 if (!(other instanceof SemVer))
358 other = new SemVer(other, this.loose);
360 // NOT having a prerelease is > having one
361 if (this.prerelease.length && !other.prerelease.length)
363 else if (!this.prerelease.length && other.prerelease.length)
365 else if (!this.prerelease.length && !other.prerelease.length)
370 var a = this.prerelease[i];
371 var b = other.prerelease[i];
372 debug('prerelease compare', i, a, b);
373 if (a === undefined && b === undefined)
375 else if (b === undefined)
377 else if (a === undefined)
382 return compareIdentifiers(a, b);
386 // preminor will bump the version up to the next minor release, and immediately
387 // down to pre-release. premajor and prepatch work the same way.
388 SemVer.prototype.inc = function(release, identifier) {
391 this.prerelease.length = 0;
395 this.inc('pre', identifier);
398 this.prerelease.length = 0;
401 this.inc('pre', identifier);
404 // If this is already a prerelease, it will bump to the next version
405 // drop any prereleases that might already exist, since they are not
406 // relevant at this point.
407 this.prerelease.length = 0;
408 this.inc('patch', identifier);
409 this.inc('pre', identifier);
411 // If the input is a non-prerelease version, this acts the same as
414 if (this.prerelease.length === 0)
415 this.inc('patch', identifier);
416 this.inc('pre', identifier);
420 // If this is a pre-major version, bump up to the same major version.
421 // Otherwise increment major.
422 // 1.0.0-5 bumps to 1.0.0
423 // 1.1.0 bumps to 2.0.0
424 if (this.minor !== 0 || this.patch !== 0 || this.prerelease.length === 0)
428 this.prerelease = [];
431 // If this is a pre-minor version, bump up to the same minor version.
432 // Otherwise increment minor.
433 // 1.2.0-5 bumps to 1.2.0
434 // 1.2.1 bumps to 1.3.0
435 if (this.patch !== 0 || this.prerelease.length === 0)
438 this.prerelease = [];
441 // If this is not a pre-release version, it will increment the patch.
442 // If it is a pre-release it will bump up to the same patch version.
443 // 1.2.0-5 patches to 1.2.0
444 // 1.2.0 patches to 1.2.1
445 if (this.prerelease.length === 0)
447 this.prerelease = [];
449 // This probably shouldn't be used publicly.
450 // 1.0.0 "pre" would become 1.0.0-0 which is the wrong direction.
452 if (this.prerelease.length === 0)
453 this.prerelease = [0];
455 var i = this.prerelease.length;
457 if (typeof this.prerelease[i] === 'number') {
458 this.prerelease[i]++;
462 if (i === -1) // didn't increment anything
463 this.prerelease.push(0);
466 // 1.2.0-beta.1 bumps to 1.2.0-beta.2,
467 // 1.2.0-beta.fooblz or 1.2.0-beta bumps to 1.2.0-beta.0
468 if (this.prerelease[0] === identifier) {
469 if (isNaN(this.prerelease[1]))
470 this.prerelease = [identifier, 0];
472 this.prerelease = [identifier, 0];
477 throw new Error('invalid increment argument: ' + release);
480 this.raw = this.version;
485 function inc(version, release, loose, identifier) {
486 if (typeof(loose) === 'string') {
492 return new SemVer(version, loose).inc(release, identifier).version;
499 function diff(version1, version2) {
500 if (eq(version1, version2)) {
503 var v1 = parse(version1);
504 var v2 = parse(version2);
505 if (v1.prerelease.length || v2.prerelease.length) {
506 for (var key in v1) {
507 if (key === 'major' || key === 'minor' || key === 'patch') {
508 if (v1[key] !== v2[key]) {
515 for (var key in v1) {
516 if (key === 'major' || key === 'minor' || key === 'patch') {
517 if (v1[key] !== v2[key]) {
525 exports.compareIdentifiers = compareIdentifiers;
527 var numeric = /^[0-9]+$/;
528 function compareIdentifiers(a, b) {
529 var anum = numeric.test(a);
530 var bnum = numeric.test(b);
537 return (anum && !bnum) ? -1 :
538 (bnum && !anum) ? 1 :
544 exports.rcompareIdentifiers = rcompareIdentifiers;
545 function rcompareIdentifiers(a, b) {
546 return compareIdentifiers(b, a);
549 exports.major = major;
550 function major(a, loose) {
551 return new SemVer(a, loose).major;
554 exports.minor = minor;
555 function minor(a, loose) {
556 return new SemVer(a, loose).minor;
559 exports.patch = patch;
560 function patch(a, loose) {
561 return new SemVer(a, loose).patch;
564 exports.compare = compare;
565 function compare(a, b, loose) {
566 return new SemVer(a, loose).compare(b);
569 exports.compareLoose = compareLoose;
570 function compareLoose(a, b) {
571 return compare(a, b, true);
574 exports.rcompare = rcompare;
575 function rcompare(a, b, loose) {
576 return compare(b, a, loose);
580 function sort(list, loose) {
581 return list.sort(function(a, b) {
582 return exports.compare(a, b, loose);
586 exports.rsort = rsort;
587 function rsort(list, loose) {
588 return list.sort(function(a, b) {
589 return exports.rcompare(a, b, loose);
594 function gt(a, b, loose) {
595 return compare(a, b, loose) > 0;
599 function lt(a, b, loose) {
600 return compare(a, b, loose) < 0;
604 function eq(a, b, loose) {
605 return compare(a, b, loose) === 0;
609 function neq(a, b, loose) {
610 return compare(a, b, loose) !== 0;
614 function gte(a, b, loose) {
615 return compare(a, b, loose) >= 0;
619 function lte(a, b, loose) {
620 return compare(a, b, loose) <= 0;
624 function cmp(a, op, b, loose) {
628 if (typeof a === 'object') a = a.version;
629 if (typeof b === 'object') b = b.version;
633 if (typeof a === 'object') a = a.version;
634 if (typeof b === 'object') b = b.version;
637 case '': case '=': case '==': ret = eq(a, b, loose); break;
638 case '!=': ret = neq(a, b, loose); break;
639 case '>': ret = gt(a, b, loose); break;
640 case '>=': ret = gte(a, b, loose); break;
641 case '<': ret = lt(a, b, loose); break;
642 case '<=': ret = lte(a, b, loose); break;
643 default: throw new TypeError('Invalid operator: ' + op);
648 exports.Comparator = Comparator;
649 function Comparator(comp, loose) {
650 if (comp instanceof Comparator) {
651 if (comp.loose === loose)
657 if (!(this instanceof Comparator))
658 return new Comparator(comp, loose);
660 debug('comparator', comp, loose);
664 if (this.semver === ANY)
667 this.value = this.operator + this.semver.version;
673 Comparator.prototype.parse = function(comp) {
674 var r = this.loose ? re[COMPARATORLOOSE] : re[COMPARATOR];
675 var m = comp.match(r);
678 throw new TypeError('Invalid comparator: ' + comp);
680 this.operator = m[1];
681 if (this.operator === '=')
684 // if it literally is just '>' or '' then allow anything.
688 this.semver = new SemVer(m[2], this.loose);
691 Comparator.prototype.toString = function() {
695 Comparator.prototype.test = function(version) {
696 debug('Comparator.test', version, this.loose);
698 if (this.semver === ANY)
701 if (typeof version === 'string')
702 version = new SemVer(version, this.loose);
704 return cmp(version, this.operator, this.semver, this.loose);
708 exports.Range = Range;
709 function Range(range, loose) {
710 if ((range instanceof Range) && range.loose === loose)
713 if (!(this instanceof Range))
714 return new Range(range, loose);
718 // First, split based on boolean or ||
720 this.set = range.split(/\s*\|\|\s*/).map(function(range) {
721 return this.parseRange(range.trim());
722 }, this).filter(function(c) {
723 // throw out any that are not relevant for whatever reason
727 if (!this.set.length) {
728 throw new TypeError('Invalid SemVer Range: ' + range);
734 Range.prototype.format = function() {
735 this.range = this.set.map(function(comps) {
736 return comps.join(' ').trim();
737 }).join('||').trim();
741 Range.prototype.toString = function() {
745 Range.prototype.parseRange = function(range) {
746 var loose = this.loose;
747 range = range.trim();
748 debug('range', range, loose);
749 // `1.2.3 - 1.2.4` => `>=1.2.3 <=1.2.4`
750 var hr = loose ? re[HYPHENRANGELOOSE] : re[HYPHENRANGE];
751 range = range.replace(hr, hyphenReplace);
752 debug('hyphen replace', range);
753 // `> 1.2.3 < 1.2.5` => `>1.2.3 <1.2.5`
754 range = range.replace(re[COMPARATORTRIM], comparatorTrimReplace);
755 debug('comparator trim', range, re[COMPARATORTRIM]);
757 // `~ 1.2.3` => `~1.2.3`
758 range = range.replace(re[TILDETRIM], tildeTrimReplace);
760 // `^ 1.2.3` => `^1.2.3`
761 range = range.replace(re[CARETTRIM], caretTrimReplace);
764 range = range.split(/\s+/).join(' ');
766 // At this point, the range is completely trimmed and
767 // ready to be split into comparators.
769 var compRe = loose ? re[COMPARATORLOOSE] : re[COMPARATOR];
770 var set = range.split(' ').map(function(comp) {
771 return parseComparator(comp, loose);
772 }).join(' ').split(/\s+/);
774 // in loose mode, throw out any that are not valid comparators
775 set = set.filter(function(comp) {
776 return !!comp.match(compRe);
779 set = set.map(function(comp) {
780 return new Comparator(comp, loose);
786 // Mostly just for testing and legacy API reasons
787 exports.toComparators = toComparators;
788 function toComparators(range, loose) {
789 return new Range(range, loose).set.map(function(comp) {
790 return comp.map(function(c) {
792 }).join(' ').trim().split(' ');
796 // comprised of xranges, tildes, stars, and gtlt's at this point.
797 // already replaced the hyphen ranges
798 // turn into a set of JUST comparators.
799 function parseComparator(comp, loose) {
801 comp = replaceCarets(comp, loose);
802 debug('caret', comp);
803 comp = replaceTildes(comp, loose);
804 debug('tildes', comp);
805 comp = replaceXRanges(comp, loose);
806 debug('xrange', comp);
807 comp = replaceStars(comp, loose);
808 debug('stars', comp);
813 return !id || id.toLowerCase() === 'x' || id === '*';
816 // ~, ~> --> * (any, kinda silly)
817 // ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0 <3.0.0
818 // ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0 <2.1.0
819 // ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0 <1.3.0
820 // ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0
821 // ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0
822 function replaceTildes(comp, loose) {
823 return comp.trim().split(/\s+/).map(function(comp) {
824 return replaceTilde(comp, loose);
828 function replaceTilde(comp, loose) {
829 var r = loose ? re[TILDELOOSE] : re[TILDE];
830 return comp.replace(r, function(_, M, m, p, pr) {
831 debug('tilde', comp, _, M, m, p, pr);
837 ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
839 // ~1.2 == >=1.2.0- <1.3.0-
840 ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
842 debug('replaceTilde pr', pr);
843 if (pr.charAt(0) !== '-')
845 ret = '>=' + M + '.' + m + '.' + p + pr +
846 ' <' + M + '.' + (+m + 1) + '.0';
848 // ~1.2.3 == >=1.2.3 <1.3.0
849 ret = '>=' + M + '.' + m + '.' + p +
850 ' <' + M + '.' + (+m + 1) + '.0';
852 debug('tilde return', ret);
857 // ^ --> * (any, kinda silly)
858 // ^2, ^2.x, ^2.x.x --> >=2.0.0 <3.0.0
859 // ^2.0, ^2.0.x --> >=2.0.0 <3.0.0
860 // ^1.2, ^1.2.x --> >=1.2.0 <2.0.0
861 // ^1.2.3 --> >=1.2.3 <2.0.0
862 // ^1.2.0 --> >=1.2.0 <2.0.0
863 function replaceCarets(comp, loose) {
864 return comp.trim().split(/\s+/).map(function(comp) {
865 return replaceCaret(comp, loose);
869 function replaceCaret(comp, loose) {
870 debug('caret', comp, loose);
871 var r = loose ? re[CARETLOOSE] : re[CARET];
872 return comp.replace(r, function(_, M, m, p, pr) {
873 debug('caret', comp, _, M, m, p, pr);
879 ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
882 ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
884 ret = '>=' + M + '.' + m + '.0 <' + (+M + 1) + '.0.0';
886 debug('replaceCaret pr', pr);
887 if (pr.charAt(0) !== '-')
891 ret = '>=' + M + '.' + m + '.' + p + pr +
892 ' <' + M + '.' + m + '.' + (+p + 1);
894 ret = '>=' + M + '.' + m + '.' + p + pr +
895 ' <' + M + '.' + (+m + 1) + '.0';
897 ret = '>=' + M + '.' + m + '.' + p + pr +
898 ' <' + (+M + 1) + '.0.0';
903 ret = '>=' + M + '.' + m + '.' + p +
904 ' <' + M + '.' + m + '.' + (+p + 1);
906 ret = '>=' + M + '.' + m + '.' + p +
907 ' <' + M + '.' + (+m + 1) + '.0';
909 ret = '>=' + M + '.' + m + '.' + p +
910 ' <' + (+M + 1) + '.0.0';
913 debug('caret return', ret);
918 function replaceXRanges(comp, loose) {
919 debug('replaceXRanges', comp, loose);
920 return comp.split(/\s+/).map(function(comp) {
921 return replaceXRange(comp, loose);
925 function replaceXRange(comp, loose) {
927 var r = loose ? re[XRANGELOOSE] : re[XRANGE];
928 return comp.replace(r, function(ret, gtlt, M, m, p, pr) {
929 debug('xRange', comp, ret, gtlt, M, m, p, pr);
931 var xm = xM || isX(m);
932 var xp = xm || isX(p);
935 if (gtlt === '=' && anyX)
939 if (gtlt === '>' || gtlt === '<') {
940 // nothing is allowed
943 // nothing is forbidden
946 } else if (gtlt && anyX) {
956 // >1.2.3 => >= 1.2.4
966 } else if (gtlt === '<=') {
967 // <=0.7.x is actually <0.8.0, since any 0.7.x should
968 // pass. Similarly, <=7.x is actually <8.0.0, etc.
976 ret = gtlt + M + '.' + m + '.' + p;
978 ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
980 ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
983 debug('xRange return', ret);
989 // Because * is AND-ed with everything else in the comparator,
990 // and '' means "any version", just remove the *s entirely.
991 function replaceStars(comp, loose) {
992 debug('replaceStars', comp, loose);
993 // Looseness is ignored here. star is always as loose as it gets!
994 return comp.trim().replace(re[STAR], '');
997 // This function is passed to string.replace(re[HYPHENRANGE])
998 // M, m, patch, prerelease, build
999 // 1.2 - 3.4.5 => >=1.2.0 <=3.4.5
1000 // 1.2.3 - 3.4 => >=1.2.0 <3.5.0 Any 3.4.x will do
1001 // 1.2 - 3.4 => >=1.2.0 <3.5.0
1002 function hyphenReplace($0,
1003 from, fM, fm, fp, fpr, fb,
1004 to, tM, tm, tp, tpr, tb) {
1009 from = '>=' + fM + '.0.0';
1011 from = '>=' + fM + '.' + fm + '.0';
1018 to = '<' + (+tM + 1) + '.0.0';
1020 to = '<' + tM + '.' + (+tm + 1) + '.0';
1022 to = '<=' + tM + '.' + tm + '.' + tp + '-' + tpr;
1026 return (from + ' ' + to).trim();
1030 // if ANY of the sets match ALL of its comparators, then pass
1031 Range.prototype.test = function(version) {
1035 if (typeof version === 'string')
1036 version = new SemVer(version, this.loose);
1038 for (var i = 0; i < this.set.length; i++) {
1039 if (testSet(this.set[i], version))
1045 function testSet(set, version) {
1046 for (var i = 0; i < set.length; i++) {
1047 if (!set[i].test(version))
1051 if (version.prerelease.length) {
1052 // Find the set of versions that are allowed to have prereleases
1053 // For example, ^1.2.3-pr.1 desugars to >=1.2.3-pr.1 <2.0.0
1054 // That should allow `1.2.3-pr.2` to pass.
1055 // However, `1.2.4-alpha.notready` should NOT be allowed,
1056 // even though it's within the range set by the comparators.
1057 for (var i = 0; i < set.length; i++) {
1058 debug(set[i].semver);
1059 if (set[i].semver === ANY)
1062 if (set[i].semver.prerelease.length > 0) {
1063 var allowed = set[i].semver;
1064 if (allowed.major === version.major &&
1065 allowed.minor === version.minor &&
1066 allowed.patch === version.patch)
1071 // Version has a -pre, but it's not one of the ones we like.
1078 exports.satisfies = satisfies;
1079 function satisfies(version, range, loose) {
1081 range = new Range(range, loose);
1085 return range.test(version);
1088 exports.maxSatisfying = maxSatisfying;
1089 function maxSatisfying(versions, range, loose) {
1090 return versions.filter(function(version) {
1091 return satisfies(version, range, loose);
1092 }).sort(function(a, b) {
1093 return rcompare(a, b, loose);
1097 exports.validRange = validRange;
1098 function validRange(range, loose) {
1100 // Return '*' instead of '' so that truthiness works.
1101 // This will throw if it's invalid anyway
1102 return new Range(range, loose).range || '*';
1108 // Determine if version is less than all the versions possible in the range
1110 function ltr(version, range, loose) {
1111 return outside(version, range, '<', loose);
1114 // Determine if version is greater than all the versions possible in the range.
1116 function gtr(version, range, loose) {
1117 return outside(version, range, '>', loose);
1120 exports.outside = outside;
1121 function outside(version, range, hilo, loose) {
1122 version = new SemVer(version, loose);
1123 range = new Range(range, loose);
1125 var gtfn, ltefn, ltfn, comp, ecomp;
1142 throw new TypeError('Must provide a hilo val of "<" or ">"');
1145 // If it satisifes the range it is not outside
1146 if (satisfies(version, range, loose)) {
1150 // From now on, variable terms are as if we're in "gtr" mode.
1151 // but note that everything is flipped for the "ltr" function.
1153 for (var i = 0; i < range.set.length; ++i) {
1154 var comparators = range.set[i];
1159 comparators.forEach(function(comparator) {
1160 if (comparator.semver === ANY) {
1161 comparator = new Comparator('>=0.0.0')
1163 high = high || comparator;
1164 low = low || comparator;
1165 if (gtfn(comparator.semver, high.semver, loose)) {
1167 } else if (ltfn(comparator.semver, low.semver, loose)) {
1172 // If the edge version comparator has a operator then our version
1174 if (high.operator === comp || high.operator === ecomp) {
1178 // If the lowest version comparator has an operator and our version
1179 // is less than it then it isn't higher than the range
1180 if ((!low.operator || low.operator === comp) &&
1181 ltefn(version, low.semver)) {
1183 } else if (low.operator === ecomp && ltfn(version, low.semver)) {