1 // export the class if we are in a Node-like system.
2 if (typeof module === 'object' && module.exports === exports)
3 exports = module.exports = SemVer;
5 // The debug function is excluded entirely from the minified version.
7 /* nomin */ if (typeof process === 'object' &&
8 /* nomin */ process.env &&
9 /* nomin */ process.env.NODE_DEBUG &&
10 /* nomin */ /\bsemver\b/i.test(process.env.NODE_DEBUG))
11 /* nomin */ debug = function() {
12 /* nomin */ var args = Array.prototype.slice.call(arguments, 0);
13 /* nomin */ args.unshift('SEMVER');
14 /* nomin */ console.log.apply(console, args);
17 /* nomin */ debug = function() {};
19 // Note: this is the semver.org version of the spec that it implements
20 // Not necessarily the package version of this code.
21 exports.SEMVER_SPEC_VERSION = '2.0.0';
24 var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
26 // The actual regexps go on exports.re
27 var re = exports.re = [];
28 var src = exports.src = [];
31 // The following Regular Expressions can be used for tokenizing,
32 // validating, and parsing SemVer version strings.
34 // ## Numeric Identifier
35 // A single `0`, or a non-zero digit followed by zero or more digits.
37 var NUMERICIDENTIFIER = R++;
38 src[NUMERICIDENTIFIER] = '0|[1-9]\\d*';
39 var NUMERICIDENTIFIERLOOSE = R++;
40 src[NUMERICIDENTIFIERLOOSE] = '[0-9]+';
43 // ## Non-numeric Identifier
44 // Zero or more digits, followed by a letter or hyphen, and then zero or
45 // more letters, digits, or hyphens.
47 var NONNUMERICIDENTIFIER = R++;
48 src[NONNUMERICIDENTIFIER] = '\\d*[a-zA-Z-][a-zA-Z0-9-]*';
52 // Three dot-separated numeric identifiers.
54 var MAINVERSION = R++;
55 src[MAINVERSION] = '(' + src[NUMERICIDENTIFIER] + ')\\.' +
56 '(' + src[NUMERICIDENTIFIER] + ')\\.' +
57 '(' + src[NUMERICIDENTIFIER] + ')';
59 var MAINVERSIONLOOSE = R++;
60 src[MAINVERSIONLOOSE] = '(' + src[NUMERICIDENTIFIERLOOSE] + ')\\.' +
61 '(' + src[NUMERICIDENTIFIERLOOSE] + ')\\.' +
62 '(' + src[NUMERICIDENTIFIERLOOSE] + ')';
64 // ## Pre-release Version Identifier
65 // A numeric identifier, or a non-numeric identifier.
67 var PRERELEASEIDENTIFIER = R++;
68 src[PRERELEASEIDENTIFIER] = '(?:' + src[NUMERICIDENTIFIER] +
69 '|' + src[NONNUMERICIDENTIFIER] + ')';
71 var PRERELEASEIDENTIFIERLOOSE = R++;
72 src[PRERELEASEIDENTIFIERLOOSE] = '(?:' + src[NUMERICIDENTIFIERLOOSE] +
73 '|' + src[NONNUMERICIDENTIFIER] + ')';
76 // ## Pre-release Version
77 // Hyphen, followed by one or more dot-separated pre-release version
81 src[PRERELEASE] = '(?:-(' + src[PRERELEASEIDENTIFIER] +
82 '(?:\\.' + src[PRERELEASEIDENTIFIER] + ')*))';
84 var PRERELEASELOOSE = R++;
85 src[PRERELEASELOOSE] = '(?:-?(' + src[PRERELEASEIDENTIFIERLOOSE] +
86 '(?:\\.' + src[PRERELEASEIDENTIFIERLOOSE] + ')*))';
88 // ## Build Metadata Identifier
89 // Any combination of digits, letters, or hyphens.
91 var BUILDIDENTIFIER = R++;
92 src[BUILDIDENTIFIER] = '[0-9A-Za-z-]+';
95 // Plus sign, followed by one or more period-separated build metadata
99 src[BUILD] = '(?:\\+(' + src[BUILDIDENTIFIER] +
100 '(?:\\.' + src[BUILDIDENTIFIER] + ')*))';
103 // ## Full Version String
104 // A main version, followed optionally by a pre-release version and
107 // Note that the only major, minor, patch, and pre-release sections of
108 // the version string are capturing groups. The build metadata is not a
109 // capturing group, because it should not ever be used in version
113 var FULLPLAIN = 'v?' + src[MAINVERSION] +
114 src[PRERELEASE] + '?' +
117 src[FULL] = '^' + FULLPLAIN + '$';
119 // like full, but allows v1.2.3 and =1.2.3, which people do sometimes.
120 // also, 1.0.0alpha1 (prerelease without the hyphen) which is pretty
121 // common in the npm registry.
122 var LOOSEPLAIN = '[v=\\s]*' + src[MAINVERSIONLOOSE] +
123 src[PRERELEASELOOSE] + '?' +
127 src[LOOSE] = '^' + LOOSEPLAIN + '$';
130 src[GTLT] = '((?:<|>)?=?)';
132 // Something like "2.*" or "1.2.x".
133 // Note that "x.x" is a valid xRange identifer, meaning "any version"
134 // Only the first item is strictly required.
135 var XRANGEIDENTIFIERLOOSE = R++;
136 src[XRANGEIDENTIFIERLOOSE] = src[NUMERICIDENTIFIERLOOSE] + '|x|X|\\*';
137 var XRANGEIDENTIFIER = R++;
138 src[XRANGEIDENTIFIER] = src[NUMERICIDENTIFIER] + '|x|X|\\*';
140 var XRANGEPLAIN = R++;
141 src[XRANGEPLAIN] = '[v=\\s]*(' + src[XRANGEIDENTIFIER] + ')' +
142 '(?:\\.(' + src[XRANGEIDENTIFIER] + ')' +
143 '(?:\\.(' + src[XRANGEIDENTIFIER] + ')' +
144 '(?:' + src[PRERELEASE] + ')?' +
148 var XRANGEPLAINLOOSE = R++;
149 src[XRANGEPLAINLOOSE] = '[v=\\s]*(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
150 '(?:\\.(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
151 '(?:\\.(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
152 '(?:' + src[PRERELEASELOOSE] + ')?' +
157 src[XRANGE] = '^' + src[GTLT] + '\\s*' + src[XRANGEPLAIN] + '$';
158 var XRANGELOOSE = R++;
159 src[XRANGELOOSE] = '^' + src[GTLT] + '\\s*' + src[XRANGEPLAINLOOSE] + '$';
162 // Meaning is "reasonably at or greater than"
164 src[LONETILDE] = '(?:~>?)';
167 src[TILDETRIM] = '(\\s*)' + src[LONETILDE] + '\\s+';
168 re[TILDETRIM] = new RegExp(src[TILDETRIM], 'g');
169 var tildeTrimReplace = '$1~';
172 src[TILDE] = '^' + src[LONETILDE] + src[XRANGEPLAIN] + '$';
173 var TILDELOOSE = R++;
174 src[TILDELOOSE] = '^' + src[LONETILDE] + src[XRANGEPLAINLOOSE] + '$';
177 // Meaning is "at least and backwards compatible with"
179 src[LONECARET] = '(?:\\^)';
182 src[CARETTRIM] = '(\\s*)' + src[LONECARET] + '\\s+';
183 re[CARETTRIM] = new RegExp(src[CARETTRIM], 'g');
184 var caretTrimReplace = '$1^';
187 src[CARET] = '^' + src[LONECARET] + src[XRANGEPLAIN] + '$';
188 var CARETLOOSE = R++;
189 src[CARETLOOSE] = '^' + src[LONECARET] + src[XRANGEPLAINLOOSE] + '$';
191 // A simple gt/lt/eq thing, or just "" to indicate "any version"
192 var COMPARATORLOOSE = R++;
193 src[COMPARATORLOOSE] = '^' + src[GTLT] + '\\s*(' + LOOSEPLAIN + ')$|^$';
194 var COMPARATOR = R++;
195 src[COMPARATOR] = '^' + src[GTLT] + '\\s*(' + FULLPLAIN + ')$|^$';
198 // An expression to strip any whitespace between the gtlt and the thing
199 // it modifies, so that `> 1.2.3` ==> `>1.2.3`
200 var COMPARATORTRIM = R++;
201 src[COMPARATORTRIM] = '(\\s*)' + src[GTLT] +
202 '\\s*(' + LOOSEPLAIN + '|' + src[XRANGEPLAIN] + ')';
204 // this one has to use the /g flag
205 re[COMPARATORTRIM] = new RegExp(src[COMPARATORTRIM], 'g');
206 var comparatorTrimReplace = '$1$2$3';
209 // Something like `1.2.3 - 1.2.4`
210 // Note that these all use the loose form, because they'll be
211 // checked against either the strict or loose comparator form
213 var HYPHENRANGE = R++;
214 src[HYPHENRANGE] = '^\\s*(' + src[XRANGEPLAIN] + ')' +
216 '(' + src[XRANGEPLAIN] + ')' +
219 var HYPHENRANGELOOSE = R++;
220 src[HYPHENRANGELOOSE] = '^\\s*(' + src[XRANGEPLAINLOOSE] + ')' +
222 '(' + src[XRANGEPLAINLOOSE] + ')' +
225 // Star ranges basically just allow anything at all.
227 src[STAR] = '(<|>)?=?\\s*\\*';
229 // Compile to actual regexp objects.
230 // All are flag-free, unless they were created above with a flag.
231 for (var i = 0; i < R; i++) {
234 re[i] = new RegExp(src[i]);
237 exports.parse = parse;
238 function parse(version, loose) {
239 if (version instanceof SemVer)
242 if (typeof version !== 'string')
245 if (version.length > MAX_LENGTH)
248 var r = loose ? re[LOOSE] : re[FULL];
249 if (!r.test(version))
253 return new SemVer(version, loose);
259 exports.valid = valid;
260 function valid(version, loose) {
261 var v = parse(version, loose);
262 return v ? v.version : null;
266 exports.clean = clean;
267 function clean(version, loose) {
268 var s = parse(version.trim().replace(/^[=v]+/, ''), loose);
269 return s ? s.version : null;
272 exports.SemVer = SemVer;
274 function SemVer(version, loose) {
275 if (version instanceof SemVer) {
276 if (version.loose === loose)
279 version = version.version;
280 } else if (typeof version !== 'string') {
281 throw new TypeError('Invalid Version: ' + version);
284 if (version.length > MAX_LENGTH)
285 throw new TypeError('version is longer than ' + MAX_LENGTH + ' characters')
287 if (!(this instanceof SemVer))
288 return new SemVer(version, loose);
290 debug('SemVer', version, loose);
292 var m = version.trim().match(loose ? re[LOOSE] : re[FULL]);
295 throw new TypeError('Invalid Version: ' + version);
299 // these are actually numbers
304 if (this.major > MAX_SAFE_INTEGER || this.major < 0)
305 throw new TypeError('Invalid major version')
307 if (this.minor > MAX_SAFE_INTEGER || this.minor < 0)
308 throw new TypeError('Invalid minor version')
310 if (this.patch > MAX_SAFE_INTEGER || this.patch < 0)
311 throw new TypeError('Invalid patch version')
313 // numberify any prerelease numeric ids
315 this.prerelease = [];
317 this.prerelease = m[4].split('.').map(function(id) {
318 if (/^[0-9]+$/.test(id)) {
320 if (num >= 0 && num < MAX_SAFE_INTEGER)
326 this.build = m[5] ? m[5].split('.') : [];
330 SemVer.prototype.format = function() {
331 this.version = this.major + '.' + this.minor + '.' + this.patch;
332 if (this.prerelease.length)
333 this.version += '-' + this.prerelease.join('.');
337 SemVer.prototype.inspect = function() {
338 return '<SemVer "' + this + '">';
341 SemVer.prototype.toString = function() {
345 SemVer.prototype.compare = function(other) {
346 debug('SemVer.compare', this.version, this.loose, other);
347 if (!(other instanceof SemVer))
348 other = new SemVer(other, this.loose);
350 return this.compareMain(other) || this.comparePre(other);
353 SemVer.prototype.compareMain = function(other) {
354 if (!(other instanceof SemVer))
355 other = new SemVer(other, this.loose);
357 return compareIdentifiers(this.major, other.major) ||
358 compareIdentifiers(this.minor, other.minor) ||
359 compareIdentifiers(this.patch, other.patch);
362 SemVer.prototype.comparePre = function(other) {
363 if (!(other instanceof SemVer))
364 other = new SemVer(other, this.loose);
366 // NOT having a prerelease is > having one
367 if (this.prerelease.length && !other.prerelease.length)
369 else if (!this.prerelease.length && other.prerelease.length)
371 else if (!this.prerelease.length && !other.prerelease.length)
376 var a = this.prerelease[i];
377 var b = other.prerelease[i];
378 debug('prerelease compare', i, a, b);
379 if (a === undefined && b === undefined)
381 else if (b === undefined)
383 else if (a === undefined)
388 return compareIdentifiers(a, b);
392 // preminor will bump the version up to the next minor release, and immediately
393 // down to pre-release. premajor and prepatch work the same way.
394 SemVer.prototype.inc = function(release, identifier) {
397 this.prerelease.length = 0;
401 this.inc('pre', identifier);
404 this.prerelease.length = 0;
407 this.inc('pre', identifier);
410 // If this is already a prerelease, it will bump to the next version
411 // drop any prereleases that might already exist, since they are not
412 // relevant at this point.
413 this.prerelease.length = 0;
414 this.inc('patch', identifier);
415 this.inc('pre', identifier);
417 // If the input is a non-prerelease version, this acts the same as
420 if (this.prerelease.length === 0)
421 this.inc('patch', identifier);
422 this.inc('pre', identifier);
426 // If this is a pre-major version, bump up to the same major version.
427 // Otherwise increment major.
428 // 1.0.0-5 bumps to 1.0.0
429 // 1.1.0 bumps to 2.0.0
430 if (this.minor !== 0 || this.patch !== 0 || this.prerelease.length === 0)
434 this.prerelease = [];
437 // If this is a pre-minor version, bump up to the same minor version.
438 // Otherwise increment minor.
439 // 1.2.0-5 bumps to 1.2.0
440 // 1.2.1 bumps to 1.3.0
441 if (this.patch !== 0 || this.prerelease.length === 0)
444 this.prerelease = [];
447 // If this is not a pre-release version, it will increment the patch.
448 // If it is a pre-release it will bump up to the same patch version.
449 // 1.2.0-5 patches to 1.2.0
450 // 1.2.0 patches to 1.2.1
451 if (this.prerelease.length === 0)
453 this.prerelease = [];
455 // This probably shouldn't be used publicly.
456 // 1.0.0 "pre" would become 1.0.0-0 which is the wrong direction.
458 if (this.prerelease.length === 0)
459 this.prerelease = [0];
461 var i = this.prerelease.length;
463 if (typeof this.prerelease[i] === 'number') {
464 this.prerelease[i]++;
468 if (i === -1) // didn't increment anything
469 this.prerelease.push(0);
472 // 1.2.0-beta.1 bumps to 1.2.0-beta.2,
473 // 1.2.0-beta.fooblz or 1.2.0-beta bumps to 1.2.0-beta.0
474 if (this.prerelease[0] === identifier) {
475 if (isNaN(this.prerelease[1]))
476 this.prerelease = [identifier, 0];
478 this.prerelease = [identifier, 0];
483 throw new Error('invalid increment argument: ' + release);
490 function inc(version, release, loose, identifier) {
491 if (typeof(loose) === 'string') {
497 return new SemVer(version, loose).inc(release, identifier).version;
504 function diff(version1, version2) {
505 if (eq(version1, version2)) {
508 var v1 = parse(version1);
509 var v2 = parse(version2);
510 if (v1.prerelease.length || v2.prerelease.length) {
511 for (var key in v1) {
512 if (key === 'major' || key === 'minor' || key === 'patch') {
513 if (v1[key] !== v2[key]) {
520 for (var key in v1) {
521 if (key === 'major' || key === 'minor' || key === 'patch') {
522 if (v1[key] !== v2[key]) {
530 exports.compareIdentifiers = compareIdentifiers;
532 var numeric = /^[0-9]+$/;
533 function compareIdentifiers(a, b) {
534 var anum = numeric.test(a);
535 var bnum = numeric.test(b);
542 return (anum && !bnum) ? -1 :
543 (bnum && !anum) ? 1 :
549 exports.rcompareIdentifiers = rcompareIdentifiers;
550 function rcompareIdentifiers(a, b) {
551 return compareIdentifiers(b, a);
554 exports.major = major;
555 function major(a, loose) {
556 return new SemVer(a, loose).major;
559 exports.minor = minor;
560 function minor(a, loose) {
561 return new SemVer(a, loose).minor;
564 exports.patch = patch;
565 function patch(a, loose) {
566 return new SemVer(a, loose).patch;
569 exports.compare = compare;
570 function compare(a, b, loose) {
571 return new SemVer(a, loose).compare(b);
574 exports.compareLoose = compareLoose;
575 function compareLoose(a, b) {
576 return compare(a, b, true);
579 exports.rcompare = rcompare;
580 function rcompare(a, b, loose) {
581 return compare(b, a, loose);
585 function sort(list, loose) {
586 return list.sort(function(a, b) {
587 return exports.compare(a, b, loose);
591 exports.rsort = rsort;
592 function rsort(list, loose) {
593 return list.sort(function(a, b) {
594 return exports.rcompare(a, b, loose);
599 function gt(a, b, loose) {
600 return compare(a, b, loose) > 0;
604 function lt(a, b, loose) {
605 return compare(a, b, loose) < 0;
609 function eq(a, b, loose) {
610 return compare(a, b, loose) === 0;
614 function neq(a, b, loose) {
615 return compare(a, b, loose) !== 0;
619 function gte(a, b, loose) {
620 return compare(a, b, loose) >= 0;
624 function lte(a, b, loose) {
625 return compare(a, b, loose) <= 0;
629 function cmp(a, op, b, loose) {
633 if (typeof a === 'object') a = a.version;
634 if (typeof b === 'object') b = b.version;
638 if (typeof a === 'object') a = a.version;
639 if (typeof b === 'object') b = b.version;
642 case '': case '=': case '==': ret = eq(a, b, loose); break;
643 case '!=': ret = neq(a, b, loose); break;
644 case '>': ret = gt(a, b, loose); break;
645 case '>=': ret = gte(a, b, loose); break;
646 case '<': ret = lt(a, b, loose); break;
647 case '<=': ret = lte(a, b, loose); break;
648 default: throw new TypeError('Invalid operator: ' + op);
653 exports.Comparator = Comparator;
654 function Comparator(comp, loose) {
655 if (comp instanceof Comparator) {
656 if (comp.loose === loose)
662 if (!(this instanceof Comparator))
663 return new Comparator(comp, loose);
665 debug('comparator', comp, loose);
669 if (this.semver === ANY)
672 this.value = this.operator + this.semver.version;
678 Comparator.prototype.parse = function(comp) {
679 var r = this.loose ? re[COMPARATORLOOSE] : re[COMPARATOR];
680 var m = comp.match(r);
683 throw new TypeError('Invalid comparator: ' + comp);
685 this.operator = m[1];
686 if (this.operator === '=')
689 // if it literally is just '>' or '' then allow anything.
693 this.semver = new SemVer(m[2], this.loose);
696 Comparator.prototype.inspect = function() {
697 return '<SemVer Comparator "' + this + '">';
700 Comparator.prototype.toString = function() {
704 Comparator.prototype.test = function(version) {
705 debug('Comparator.test', version, this.loose);
707 if (this.semver === ANY)
710 if (typeof version === 'string')
711 version = new SemVer(version, this.loose);
713 return cmp(version, this.operator, this.semver, this.loose);
717 exports.Range = Range;
718 function Range(range, loose) {
719 if ((range instanceof Range) && range.loose === loose)
722 if (!(this instanceof Range))
723 return new Range(range, loose);
727 // First, split based on boolean or ||
729 this.set = range.split(/\s*\|\|\s*/).map(function(range) {
730 return this.parseRange(range.trim());
731 }, this).filter(function(c) {
732 // throw out any that are not relevant for whatever reason
736 if (!this.set.length) {
737 throw new TypeError('Invalid SemVer Range: ' + range);
743 Range.prototype.inspect = function() {
744 return '<SemVer Range "' + this.range + '">';
747 Range.prototype.format = function() {
748 this.range = this.set.map(function(comps) {
749 return comps.join(' ').trim();
750 }).join('||').trim();
754 Range.prototype.toString = function() {
758 Range.prototype.parseRange = function(range) {
759 var loose = this.loose;
760 range = range.trim();
761 debug('range', range, loose);
762 // `1.2.3 - 1.2.4` => `>=1.2.3 <=1.2.4`
763 var hr = loose ? re[HYPHENRANGELOOSE] : re[HYPHENRANGE];
764 range = range.replace(hr, hyphenReplace);
765 debug('hyphen replace', range);
766 // `> 1.2.3 < 1.2.5` => `>1.2.3 <1.2.5`
767 range = range.replace(re[COMPARATORTRIM], comparatorTrimReplace);
768 debug('comparator trim', range, re[COMPARATORTRIM]);
770 // `~ 1.2.3` => `~1.2.3`
771 range = range.replace(re[TILDETRIM], tildeTrimReplace);
773 // `^ 1.2.3` => `^1.2.3`
774 range = range.replace(re[CARETTRIM], caretTrimReplace);
777 range = range.split(/\s+/).join(' ');
779 // At this point, the range is completely trimmed and
780 // ready to be split into comparators.
782 var compRe = loose ? re[COMPARATORLOOSE] : re[COMPARATOR];
783 var set = range.split(' ').map(function(comp) {
784 return parseComparator(comp, loose);
785 }).join(' ').split(/\s+/);
787 // in loose mode, throw out any that are not valid comparators
788 set = set.filter(function(comp) {
789 return !!comp.match(compRe);
792 set = set.map(function(comp) {
793 return new Comparator(comp, loose);
799 // Mostly just for testing and legacy API reasons
800 exports.toComparators = toComparators;
801 function toComparators(range, loose) {
802 return new Range(range, loose).set.map(function(comp) {
803 return comp.map(function(c) {
805 }).join(' ').trim().split(' ');
809 // comprised of xranges, tildes, stars, and gtlt's at this point.
810 // already replaced the hyphen ranges
811 // turn into a set of JUST comparators.
812 function parseComparator(comp, loose) {
814 comp = replaceCarets(comp, loose);
815 debug('caret', comp);
816 comp = replaceTildes(comp, loose);
817 debug('tildes', comp);
818 comp = replaceXRanges(comp, loose);
819 debug('xrange', comp);
820 comp = replaceStars(comp, loose);
821 debug('stars', comp);
826 return !id || id.toLowerCase() === 'x' || id === '*';
829 // ~, ~> --> * (any, kinda silly)
830 // ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0 <3.0.0
831 // ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0 <2.1.0
832 // ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0 <1.3.0
833 // ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0
834 // ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0
835 function replaceTildes(comp, loose) {
836 return comp.trim().split(/\s+/).map(function(comp) {
837 return replaceTilde(comp, loose);
841 function replaceTilde(comp, loose) {
842 var r = loose ? re[TILDELOOSE] : re[TILDE];
843 return comp.replace(r, function(_, M, m, p, pr) {
844 debug('tilde', comp, _, M, m, p, pr);
850 ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
852 // ~1.2 == >=1.2.0- <1.3.0-
853 ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
855 debug('replaceTilde pr', pr);
856 if (pr.charAt(0) !== '-')
858 ret = '>=' + M + '.' + m + '.' + p + pr +
859 ' <' + M + '.' + (+m + 1) + '.0';
861 // ~1.2.3 == >=1.2.3 <1.3.0
862 ret = '>=' + M + '.' + m + '.' + p +
863 ' <' + M + '.' + (+m + 1) + '.0';
865 debug('tilde return', ret);
870 // ^ --> * (any, kinda silly)
871 // ^2, ^2.x, ^2.x.x --> >=2.0.0 <3.0.0
872 // ^2.0, ^2.0.x --> >=2.0.0 <3.0.0
873 // ^1.2, ^1.2.x --> >=1.2.0 <2.0.0
874 // ^1.2.3 --> >=1.2.3 <2.0.0
875 // ^1.2.0 --> >=1.2.0 <2.0.0
876 function replaceCarets(comp, loose) {
877 return comp.trim().split(/\s+/).map(function(comp) {
878 return replaceCaret(comp, loose);
882 function replaceCaret(comp, loose) {
883 debug('caret', comp, loose);
884 var r = loose ? re[CARETLOOSE] : re[CARET];
885 return comp.replace(r, function(_, M, m, p, pr) {
886 debug('caret', comp, _, M, m, p, pr);
892 ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
895 ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
897 ret = '>=' + M + '.' + m + '.0 <' + (+M + 1) + '.0.0';
899 debug('replaceCaret pr', pr);
900 if (pr.charAt(0) !== '-')
904 ret = '>=' + M + '.' + m + '.' + p + pr +
905 ' <' + M + '.' + m + '.' + (+p + 1);
907 ret = '>=' + M + '.' + m + '.' + p + pr +
908 ' <' + M + '.' + (+m + 1) + '.0';
910 ret = '>=' + M + '.' + m + '.' + p + pr +
911 ' <' + (+M + 1) + '.0.0';
916 ret = '>=' + M + '.' + m + '.' + p +
917 ' <' + M + '.' + m + '.' + (+p + 1);
919 ret = '>=' + M + '.' + m + '.' + p +
920 ' <' + M + '.' + (+m + 1) + '.0';
922 ret = '>=' + M + '.' + m + '.' + p +
923 ' <' + (+M + 1) + '.0.0';
926 debug('caret return', ret);
931 function replaceXRanges(comp, loose) {
932 debug('replaceXRanges', comp, loose);
933 return comp.split(/\s+/).map(function(comp) {
934 return replaceXRange(comp, loose);
938 function replaceXRange(comp, loose) {
940 var r = loose ? re[XRANGELOOSE] : re[XRANGE];
941 return comp.replace(r, function(ret, gtlt, M, m, p, pr) {
942 debug('xRange', comp, ret, gtlt, M, m, p, pr);
944 var xm = xM || isX(m);
945 var xp = xm || isX(p);
948 if (gtlt === '=' && anyX)
952 if (gtlt === '>' || gtlt === '<') {
953 // nothing is allowed
956 // nothing is forbidden
959 } else if (gtlt && anyX) {
969 // >1.2.3 => >= 1.2.4
979 } else if (gtlt === '<=') {
980 // <=0.7.x is actually <0.8.0, since any 0.7.x should
981 // pass. Similarly, <=7.x is actually <8.0.0, etc.
989 ret = gtlt + M + '.' + m + '.' + p;
991 ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
993 ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
996 debug('xRange return', ret);
1002 // Because * is AND-ed with everything else in the comparator,
1003 // and '' means "any version", just remove the *s entirely.
1004 function replaceStars(comp, loose) {
1005 debug('replaceStars', comp, loose);
1006 // Looseness is ignored here. star is always as loose as it gets!
1007 return comp.trim().replace(re[STAR], '');
1010 // This function is passed to string.replace(re[HYPHENRANGE])
1011 // M, m, patch, prerelease, build
1012 // 1.2 - 3.4.5 => >=1.2.0 <=3.4.5
1013 // 1.2.3 - 3.4 => >=1.2.0 <3.5.0 Any 3.4.x will do
1014 // 1.2 - 3.4 => >=1.2.0 <3.5.0
1015 function hyphenReplace($0,
1016 from, fM, fm, fp, fpr, fb,
1017 to, tM, tm, tp, tpr, tb) {
1022 from = '>=' + fM + '.0.0';
1024 from = '>=' + fM + '.' + fm + '.0';
1031 to = '<' + (+tM + 1) + '.0.0';
1033 to = '<' + tM + '.' + (+tm + 1) + '.0';
1035 to = '<=' + tM + '.' + tm + '.' + tp + '-' + tpr;
1039 return (from + ' ' + to).trim();
1043 // if ANY of the sets match ALL of its comparators, then pass
1044 Range.prototype.test = function(version) {
1048 if (typeof version === 'string')
1049 version = new SemVer(version, this.loose);
1051 for (var i = 0; i < this.set.length; i++) {
1052 if (testSet(this.set[i], version))
1058 function testSet(set, version) {
1059 for (var i = 0; i < set.length; i++) {
1060 if (!set[i].test(version))
1064 if (version.prerelease.length) {
1065 // Find the set of versions that are allowed to have prereleases
1066 // For example, ^1.2.3-pr.1 desugars to >=1.2.3-pr.1 <2.0.0
1067 // That should allow `1.2.3-pr.2` to pass.
1068 // However, `1.2.4-alpha.notready` should NOT be allowed,
1069 // even though it's within the range set by the comparators.
1070 for (var i = 0; i < set.length; i++) {
1071 debug(set[i].semver);
1072 if (set[i].semver === ANY)
1075 if (set[i].semver.prerelease.length > 0) {
1076 var allowed = set[i].semver;
1077 if (allowed.major === version.major &&
1078 allowed.minor === version.minor &&
1079 allowed.patch === version.patch)
1084 // Version has a -pre, but it's not one of the ones we like.
1091 exports.satisfies = satisfies;
1092 function satisfies(version, range, loose) {
1094 range = new Range(range, loose);
1098 return range.test(version);
1101 exports.maxSatisfying = maxSatisfying;
1102 function maxSatisfying(versions, range, loose) {
1103 return versions.filter(function(version) {
1104 return satisfies(version, range, loose);
1105 }).sort(function(a, b) {
1106 return rcompare(a, b, loose);
1110 exports.validRange = validRange;
1111 function validRange(range, loose) {
1113 // Return '*' instead of '' so that truthiness works.
1114 // This will throw if it's invalid anyway
1115 return new Range(range, loose).range || '*';
1121 // Determine if version is less than all the versions possible in the range
1123 function ltr(version, range, loose) {
1124 return outside(version, range, '<', loose);
1127 // Determine if version is greater than all the versions possible in the range.
1129 function gtr(version, range, loose) {
1130 return outside(version, range, '>', loose);
1133 exports.outside = outside;
1134 function outside(version, range, hilo, loose) {
1135 version = new SemVer(version, loose);
1136 range = new Range(range, loose);
1138 var gtfn, ltefn, ltfn, comp, ecomp;
1155 throw new TypeError('Must provide a hilo val of "<" or ">"');
1158 // If it satisifes the range it is not outside
1159 if (satisfies(version, range, loose)) {
1163 // From now on, variable terms are as if we're in "gtr" mode.
1164 // but note that everything is flipped for the "ltr" function.
1166 for (var i = 0; i < range.set.length; ++i) {
1167 var comparators = range.set[i];
1172 comparators.forEach(function(comparator) {
1173 if (comparator.semver === ANY) {
1174 comparator = new Comparator('>=0.0.0')
1176 high = high || comparator;
1177 low = low || comparator;
1178 if (gtfn(comparator.semver, high.semver, loose)) {
1180 } else if (ltfn(comparator.semver, low.semver, loose)) {
1185 // If the edge version comparator has a operator then our version
1187 if (high.operator === comp || high.operator === ecomp) {
1191 // If the lowest version comparator has an operator and our version
1192 // is less than it then it isn't higher than the range
1193 if ((!low.operator || low.operator === comp) &&
1194 ltefn(version, low.semver)) {
1196 } else if (low.operator === ecomp && ltfn(version, low.semver)) {
1203 // Use the define() function if we're in AMD land
1204 if (typeof define === 'function' && define.amd)