3 // export the class if we are in a Node-like system.
4 if (typeof module === 'object' && module.exports === exports)
5 exports = module.exports = SemVer;
7 // The debug function is excluded entirely from the minified version.
9 // Note: this is the semver.org version of the spec that it implements
10 // Not necessarily the package version of this code.
11 exports.SEMVER_SPEC_VERSION = '2.0.0';
14 var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
16 // The actual regexps go on exports.re
17 var re = exports.re = [];
18 var src = exports.src = [];
21 // The following Regular Expressions can be used for tokenizing,
22 // validating, and parsing SemVer version strings.
24 // ## Numeric Identifier
25 // A single `0`, or a non-zero digit followed by zero or more digits.
27 var NUMERICIDENTIFIER = R++;
28 src[NUMERICIDENTIFIER] = '0|[1-9]\\d*';
29 var NUMERICIDENTIFIERLOOSE = R++;
30 src[NUMERICIDENTIFIERLOOSE] = '[0-9]+';
33 // ## Non-numeric Identifier
34 // Zero or more digits, followed by a letter or hyphen, and then zero or
35 // more letters, digits, or hyphens.
37 var NONNUMERICIDENTIFIER = R++;
38 src[NONNUMERICIDENTIFIER] = '\\d*[a-zA-Z-][a-zA-Z0-9-]*';
42 // Three dot-separated numeric identifiers.
44 var MAINVERSION = R++;
45 src[MAINVERSION] = '(' + src[NUMERICIDENTIFIER] + ')\\.' +
46 '(' + src[NUMERICIDENTIFIER] + ')\\.' +
47 '(' + src[NUMERICIDENTIFIER] + ')';
49 var MAINVERSIONLOOSE = R++;
50 src[MAINVERSIONLOOSE] = '(' + src[NUMERICIDENTIFIERLOOSE] + ')\\.' +
51 '(' + src[NUMERICIDENTIFIERLOOSE] + ')\\.' +
52 '(' + src[NUMERICIDENTIFIERLOOSE] + ')';
54 // ## Pre-release Version Identifier
55 // A numeric identifier, or a non-numeric identifier.
57 var PRERELEASEIDENTIFIER = R++;
58 src[PRERELEASEIDENTIFIER] = '(?:' + src[NUMERICIDENTIFIER] +
59 '|' + src[NONNUMERICIDENTIFIER] + ')';
61 var PRERELEASEIDENTIFIERLOOSE = R++;
62 src[PRERELEASEIDENTIFIERLOOSE] = '(?:' + src[NUMERICIDENTIFIERLOOSE] +
63 '|' + src[NONNUMERICIDENTIFIER] + ')';
66 // ## Pre-release Version
67 // Hyphen, followed by one or more dot-separated pre-release version
71 src[PRERELEASE] = '(?:-(' + src[PRERELEASEIDENTIFIER] +
72 '(?:\\.' + src[PRERELEASEIDENTIFIER] + ')*))';
74 var PRERELEASELOOSE = R++;
75 src[PRERELEASELOOSE] = '(?:-?(' + src[PRERELEASEIDENTIFIERLOOSE] +
76 '(?:\\.' + src[PRERELEASEIDENTIFIERLOOSE] + ')*))';
78 // ## Build Metadata Identifier
79 // Any combination of digits, letters, or hyphens.
81 var BUILDIDENTIFIER = R++;
82 src[BUILDIDENTIFIER] = '[0-9A-Za-z-]+';
85 // Plus sign, followed by one or more period-separated build metadata
89 src[BUILD] = '(?:\\+(' + src[BUILDIDENTIFIER] +
90 '(?:\\.' + src[BUILDIDENTIFIER] + ')*))';
93 // ## Full Version String
94 // A main version, followed optionally by a pre-release version and
97 // Note that the only major, minor, patch, and pre-release sections of
98 // the version string are capturing groups. The build metadata is not a
99 // capturing group, because it should not ever be used in version
103 var FULLPLAIN = 'v?' + src[MAINVERSION] +
104 src[PRERELEASE] + '?' +
107 src[FULL] = '^' + FULLPLAIN + '$';
109 // like full, but allows v1.2.3 and =1.2.3, which people do sometimes.
110 // also, 1.0.0alpha1 (prerelease without the hyphen) which is pretty
111 // common in the npm registry.
112 var LOOSEPLAIN = '[v=\\s]*' + src[MAINVERSIONLOOSE] +
113 src[PRERELEASELOOSE] + '?' +
117 src[LOOSE] = '^' + LOOSEPLAIN + '$';
120 src[GTLT] = '((?:<|>)?=?)';
122 // Something like "2.*" or "1.2.x".
123 // Note that "x.x" is a valid xRange identifer, meaning "any version"
124 // Only the first item is strictly required.
125 var XRANGEIDENTIFIERLOOSE = R++;
126 src[XRANGEIDENTIFIERLOOSE] = src[NUMERICIDENTIFIERLOOSE] + '|x|X|\\*';
127 var XRANGEIDENTIFIER = R++;
128 src[XRANGEIDENTIFIER] = src[NUMERICIDENTIFIER] + '|x|X|\\*';
130 var XRANGEPLAIN = R++;
131 src[XRANGEPLAIN] = '[v=\\s]*(' + src[XRANGEIDENTIFIER] + ')' +
132 '(?:\\.(' + src[XRANGEIDENTIFIER] + ')' +
133 '(?:\\.(' + src[XRANGEIDENTIFIER] + ')' +
134 '(?:' + src[PRERELEASE] + ')?' +
138 var XRANGEPLAINLOOSE = R++;
139 src[XRANGEPLAINLOOSE] = '[v=\\s]*(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
140 '(?:\\.(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
141 '(?:\\.(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
142 '(?:' + src[PRERELEASELOOSE] + ')?' +
147 src[XRANGE] = '^' + src[GTLT] + '\\s*' + src[XRANGEPLAIN] + '$';
148 var XRANGELOOSE = R++;
149 src[XRANGELOOSE] = '^' + src[GTLT] + '\\s*' + src[XRANGEPLAINLOOSE] + '$';
152 // Meaning is "reasonably at or greater than"
154 src[LONETILDE] = '(?:~>?)';
157 src[TILDETRIM] = '(\\s*)' + src[LONETILDE] + '\\s+';
158 re[TILDETRIM] = new RegExp(src[TILDETRIM], 'g');
159 var tildeTrimReplace = '$1~';
162 src[TILDE] = '^' + src[LONETILDE] + src[XRANGEPLAIN] + '$';
163 var TILDELOOSE = R++;
164 src[TILDELOOSE] = '^' + src[LONETILDE] + src[XRANGEPLAINLOOSE] + '$';
167 // Meaning is "at least and backwards compatible with"
169 src[LONECARET] = '(?:\\^)';
172 src[CARETTRIM] = '(\\s*)' + src[LONECARET] + '\\s+';
173 re[CARETTRIM] = new RegExp(src[CARETTRIM], 'g');
174 var caretTrimReplace = '$1^';
177 src[CARET] = '^' + src[LONECARET] + src[XRANGEPLAIN] + '$';
178 var CARETLOOSE = R++;
179 src[CARETLOOSE] = '^' + src[LONECARET] + src[XRANGEPLAINLOOSE] + '$';
181 // A simple gt/lt/eq thing, or just "" to indicate "any version"
182 var COMPARATORLOOSE = R++;
183 src[COMPARATORLOOSE] = '^' + src[GTLT] + '\\s*(' + LOOSEPLAIN + ')$|^$';
184 var COMPARATOR = R++;
185 src[COMPARATOR] = '^' + src[GTLT] + '\\s*(' + FULLPLAIN + ')$|^$';
188 // An expression to strip any whitespace between the gtlt and the thing
189 // it modifies, so that `> 1.2.3` ==> `>1.2.3`
190 var COMPARATORTRIM = R++;
191 src[COMPARATORTRIM] = '(\\s*)' + src[GTLT] +
192 '\\s*(' + LOOSEPLAIN + '|' + src[XRANGEPLAIN] + ')';
194 // this one has to use the /g flag
195 re[COMPARATORTRIM] = new RegExp(src[COMPARATORTRIM], 'g');
196 var comparatorTrimReplace = '$1$2$3';
199 // Something like `1.2.3 - 1.2.4`
200 // Note that these all use the loose form, because they'll be
201 // checked against either the strict or loose comparator form
203 var HYPHENRANGE = R++;
204 src[HYPHENRANGE] = '^\\s*(' + src[XRANGEPLAIN] + ')' +
206 '(' + src[XRANGEPLAIN] + ')' +
209 var HYPHENRANGELOOSE = R++;
210 src[HYPHENRANGELOOSE] = '^\\s*(' + src[XRANGEPLAINLOOSE] + ')' +
212 '(' + src[XRANGEPLAINLOOSE] + ')' +
215 // Star ranges basically just allow anything at all.
217 src[STAR] = '(<|>)?=?\\s*\\*';
219 // Compile to actual regexp objects.
220 // All are flag-free, unless they were created above with a flag.
221 for (var i = 0; i < R; i++) {
224 re[i] = new RegExp(src[i]);
227 exports.parse = parse;
228 function parse(version, loose) {
229 if (version instanceof SemVer)
232 if (typeof version !== 'string')
235 if (version.length > MAX_LENGTH)
238 var r = loose ? re[LOOSE] : re[FULL];
239 if (!r.test(version))
243 return new SemVer(version, loose);
249 exports.valid = valid;
250 function valid(version, loose) {
251 var v = parse(version, loose);
252 return v ? v.version : null;
256 exports.clean = clean;
257 function clean(version, loose) {
258 var s = parse(version.trim().replace(/^[=v]+/, ''), loose);
259 return s ? s.version : null;
262 exports.SemVer = SemVer;
264 function SemVer(version, loose) {
265 if (version instanceof SemVer) {
266 if (version.loose === loose)
269 version = version.version;
270 } else if (typeof version !== 'string') {
271 throw new TypeError('Invalid Version: ' + version);
274 if (version.length > MAX_LENGTH)
275 throw new TypeError('version is longer than ' + MAX_LENGTH + ' characters')
277 if (!(this instanceof SemVer))
278 return new SemVer(version, loose);
282 var m = version.trim().match(loose ? re[LOOSE] : re[FULL]);
285 throw new TypeError('Invalid Version: ' + version);
289 // these are actually numbers
294 if (this.major > MAX_SAFE_INTEGER || this.major < 0)
295 throw new TypeError('Invalid major version')
297 if (this.minor > MAX_SAFE_INTEGER || this.minor < 0)
298 throw new TypeError('Invalid minor version')
300 if (this.patch > MAX_SAFE_INTEGER || this.patch < 0)
301 throw new TypeError('Invalid patch version')
303 // numberify any prerelease numeric ids
305 this.prerelease = [];
307 this.prerelease = m[4].split('.').map(function(id) {
308 if (/^[0-9]+$/.test(id)) {
310 if (num >= 0 && num < MAX_SAFE_INTEGER)
316 this.build = m[5] ? m[5].split('.') : [];
320 SemVer.prototype.format = function() {
321 this.version = this.major + '.' + this.minor + '.' + this.patch;
322 if (this.prerelease.length)
323 this.version += '-' + this.prerelease.join('.');
327 SemVer.prototype.inspect = function() {
328 return '<SemVer "' + this + '">';
331 SemVer.prototype.toString = function() {
335 SemVer.prototype.compare = function(other) {
337 if (!(other instanceof SemVer))
338 other = new SemVer(other, this.loose);
340 return this.compareMain(other) || this.comparePre(other);
343 SemVer.prototype.compareMain = function(other) {
344 if (!(other instanceof SemVer))
345 other = new SemVer(other, this.loose);
347 return compareIdentifiers(this.major, other.major) ||
348 compareIdentifiers(this.minor, other.minor) ||
349 compareIdentifiers(this.patch, other.patch);
352 SemVer.prototype.comparePre = function(other) {
353 if (!(other instanceof SemVer))
354 other = new SemVer(other, this.loose);
356 // NOT having a prerelease is > having one
357 if (this.prerelease.length && !other.prerelease.length)
359 else if (!this.prerelease.length && other.prerelease.length)
361 else if (!this.prerelease.length && !other.prerelease.length)
366 var a = this.prerelease[i];
367 var b = other.prerelease[i];
369 if (a === undefined && b === undefined)
371 else if (b === undefined)
373 else if (a === undefined)
378 return compareIdentifiers(a, b);
382 // preminor will bump the version up to the next minor release, and immediately
383 // down to pre-release. premajor and prepatch work the same way.
384 SemVer.prototype.inc = function(release, identifier) {
387 this.prerelease.length = 0;
391 this.inc('pre', identifier);
394 this.prerelease.length = 0;
397 this.inc('pre', identifier);
400 // If this is already a prerelease, it will bump to the next version
401 // drop any prereleases that might already exist, since they are not
402 // relevant at this point.
403 this.prerelease.length = 0;
404 this.inc('patch', identifier);
405 this.inc('pre', identifier);
407 // If the input is a non-prerelease version, this acts the same as
410 if (this.prerelease.length === 0)
411 this.inc('patch', identifier);
412 this.inc('pre', identifier);
416 // If this is a pre-major version, bump up to the same major version.
417 // Otherwise increment major.
418 // 1.0.0-5 bumps to 1.0.0
419 // 1.1.0 bumps to 2.0.0
420 if (this.minor !== 0 || this.patch !== 0 || this.prerelease.length === 0)
424 this.prerelease = [];
427 // If this is a pre-minor version, bump up to the same minor version.
428 // Otherwise increment minor.
429 // 1.2.0-5 bumps to 1.2.0
430 // 1.2.1 bumps to 1.3.0
431 if (this.patch !== 0 || this.prerelease.length === 0)
434 this.prerelease = [];
437 // If this is not a pre-release version, it will increment the patch.
438 // If it is a pre-release it will bump up to the same patch version.
439 // 1.2.0-5 patches to 1.2.0
440 // 1.2.0 patches to 1.2.1
441 if (this.prerelease.length === 0)
443 this.prerelease = [];
445 // This probably shouldn't be used publicly.
446 // 1.0.0 "pre" would become 1.0.0-0 which is the wrong direction.
448 if (this.prerelease.length === 0)
449 this.prerelease = [0];
451 var i = this.prerelease.length;
453 if (typeof this.prerelease[i] === 'number') {
454 this.prerelease[i]++;
458 if (i === -1) // didn't increment anything
459 this.prerelease.push(0);
462 // 1.2.0-beta.1 bumps to 1.2.0-beta.2,
463 // 1.2.0-beta.fooblz or 1.2.0-beta bumps to 1.2.0-beta.0
464 if (this.prerelease[0] === identifier) {
465 if (isNaN(this.prerelease[1]))
466 this.prerelease = [identifier, 0];
468 this.prerelease = [identifier, 0];
473 throw new Error('invalid increment argument: ' + release);
480 function inc(version, release, loose, identifier) {
481 if (typeof(loose) === 'string') {
487 return new SemVer(version, loose).inc(release, identifier).version;
494 function diff(version1, version2) {
495 if (eq(version1, version2)) {
498 var v1 = parse(version1);
499 var v2 = parse(version2);
500 if (v1.prerelease.length || v2.prerelease.length) {
501 for (var key in v1) {
502 if (key === 'major' || key === 'minor' || key === 'patch') {
503 if (v1[key] !== v2[key]) {
510 for (var key in v1) {
511 if (key === 'major' || key === 'minor' || key === 'patch') {
512 if (v1[key] !== v2[key]) {
520 exports.compareIdentifiers = compareIdentifiers;
522 var numeric = /^[0-9]+$/;
523 function compareIdentifiers(a, b) {
524 var anum = numeric.test(a);
525 var bnum = numeric.test(b);
532 return (anum && !bnum) ? -1 :
533 (bnum && !anum) ? 1 :
539 exports.rcompareIdentifiers = rcompareIdentifiers;
540 function rcompareIdentifiers(a, b) {
541 return compareIdentifiers(b, a);
544 exports.major = major;
545 function major(a, loose) {
546 return new SemVer(a, loose).major;
549 exports.minor = minor;
550 function minor(a, loose) {
551 return new SemVer(a, loose).minor;
554 exports.patch = patch;
555 function patch(a, loose) {
556 return new SemVer(a, loose).patch;
559 exports.compare = compare;
560 function compare(a, b, loose) {
561 return new SemVer(a, loose).compare(b);
564 exports.compareLoose = compareLoose;
565 function compareLoose(a, b) {
566 return compare(a, b, true);
569 exports.rcompare = rcompare;
570 function rcompare(a, b, loose) {
571 return compare(b, a, loose);
575 function sort(list, loose) {
576 return list.sort(function(a, b) {
577 return exports.compare(a, b, loose);
581 exports.rsort = rsort;
582 function rsort(list, loose) {
583 return list.sort(function(a, b) {
584 return exports.rcompare(a, b, loose);
589 function gt(a, b, loose) {
590 return compare(a, b, loose) > 0;
594 function lt(a, b, loose) {
595 return compare(a, b, loose) < 0;
599 function eq(a, b, loose) {
600 return compare(a, b, loose) === 0;
604 function neq(a, b, loose) {
605 return compare(a, b, loose) !== 0;
609 function gte(a, b, loose) {
610 return compare(a, b, loose) >= 0;
614 function lte(a, b, loose) {
615 return compare(a, b, loose) <= 0;
619 function cmp(a, op, b, loose) {
623 if (typeof a === 'object') a = a.version;
624 if (typeof b === 'object') b = b.version;
628 if (typeof a === 'object') a = a.version;
629 if (typeof b === 'object') b = b.version;
632 case '': case '=': case '==': ret = eq(a, b, loose); break;
633 case '!=': ret = neq(a, b, loose); break;
634 case '>': ret = gt(a, b, loose); break;
635 case '>=': ret = gte(a, b, loose); break;
636 case '<': ret = lt(a, b, loose); break;
637 case '<=': ret = lte(a, b, loose); break;
638 default: throw new TypeError('Invalid operator: ' + op);
643 exports.Comparator = Comparator;
644 function Comparator(comp, loose) {
645 if (comp instanceof Comparator) {
646 if (comp.loose === loose)
652 if (!(this instanceof Comparator))
653 return new Comparator(comp, loose);
659 if (this.semver === ANY)
662 this.value = this.operator + this.semver.version;
668 Comparator.prototype.parse = function(comp) {
669 var r = this.loose ? re[COMPARATORLOOSE] : re[COMPARATOR];
670 var m = comp.match(r);
673 throw new TypeError('Invalid comparator: ' + comp);
675 this.operator = m[1];
676 if (this.operator === '=')
679 // if it literally is just '>' or '' then allow anything.
683 this.semver = new SemVer(m[2], this.loose);
686 Comparator.prototype.inspect = function() {
687 return '<SemVer Comparator "' + this + '">';
690 Comparator.prototype.toString = function() {
694 Comparator.prototype.test = function(version) {
697 if (this.semver === ANY)
700 if (typeof version === 'string')
701 version = new SemVer(version, this.loose);
703 return cmp(version, this.operator, this.semver, this.loose);
707 exports.Range = Range;
708 function Range(range, loose) {
709 if ((range instanceof Range) && range.loose === loose)
712 if (!(this instanceof Range))
713 return new Range(range, loose);
717 // First, split based on boolean or ||
719 this.set = range.split(/\s*\|\|\s*/).map(function(range) {
720 return this.parseRange(range.trim());
721 }, this).filter(function(c) {
722 // throw out any that are not relevant for whatever reason
726 if (!this.set.length) {
727 throw new TypeError('Invalid SemVer Range: ' + range);
733 Range.prototype.inspect = function() {
734 return '<SemVer Range "' + this.range + '">';
737 Range.prototype.format = function() {
738 this.range = this.set.map(function(comps) {
739 return comps.join(' ').trim();
740 }).join('||').trim();
744 Range.prototype.toString = function() {
748 Range.prototype.parseRange = function(range) {
749 var loose = this.loose;
750 range = range.trim();
752 // `1.2.3 - 1.2.4` => `>=1.2.3 <=1.2.4`
753 var hr = loose ? re[HYPHENRANGELOOSE] : re[HYPHENRANGE];
754 range = range.replace(hr, hyphenReplace);
756 // `> 1.2.3 < 1.2.5` => `>1.2.3 <1.2.5`
757 range = range.replace(re[COMPARATORTRIM], comparatorTrimReplace);
760 // `~ 1.2.3` => `~1.2.3`
761 range = range.replace(re[TILDETRIM], tildeTrimReplace);
763 // `^ 1.2.3` => `^1.2.3`
764 range = range.replace(re[CARETTRIM], caretTrimReplace);
767 range = range.split(/\s+/).join(' ');
769 // At this point, the range is completely trimmed and
770 // ready to be split into comparators.
772 var compRe = loose ? re[COMPARATORLOOSE] : re[COMPARATOR];
773 var set = range.split(' ').map(function(comp) {
774 return parseComparator(comp, loose);
775 }).join(' ').split(/\s+/);
777 // in loose mode, throw out any that are not valid comparators
778 set = set.filter(function(comp) {
779 return !!comp.match(compRe);
782 set = set.map(function(comp) {
783 return new Comparator(comp, loose);
789 // Mostly just for testing and legacy API reasons
790 exports.toComparators = toComparators;
791 function toComparators(range, loose) {
792 return new Range(range, loose).set.map(function(comp) {
793 return comp.map(function(c) {
795 }).join(' ').trim().split(' ');
799 // comprised of xranges, tildes, stars, and gtlt's at this point.
800 // already replaced the hyphen ranges
801 // turn into a set of JUST comparators.
802 function parseComparator(comp, loose) {
804 comp = replaceCarets(comp, loose);
806 comp = replaceTildes(comp, loose);
808 comp = replaceXRanges(comp, loose);
810 comp = replaceStars(comp, loose);
816 return !id || id.toLowerCase() === 'x' || id === '*';
819 // ~, ~> --> * (any, kinda silly)
820 // ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0 <3.0.0
821 // ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0 <2.1.0
822 // ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0 <1.3.0
823 // ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0
824 // ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0
825 function replaceTildes(comp, loose) {
826 return comp.trim().split(/\s+/).map(function(comp) {
827 return replaceTilde(comp, loose);
831 function replaceTilde(comp, loose) {
832 var r = loose ? re[TILDELOOSE] : re[TILDE];
833 return comp.replace(r, function(_, M, m, p, pr) {
840 ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
842 // ~1.2 == >=1.2.0- <1.3.0-
843 ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
846 if (pr.charAt(0) !== '-')
848 ret = '>=' + M + '.' + m + '.' + p + pr +
849 ' <' + M + '.' + (+m + 1) + '.0';
851 // ~1.2.3 == >=1.2.3 <1.3.0
852 ret = '>=' + M + '.' + m + '.' + p +
853 ' <' + M + '.' + (+m + 1) + '.0';
860 // ^ --> * (any, kinda silly)
861 // ^2, ^2.x, ^2.x.x --> >=2.0.0 <3.0.0
862 // ^2.0, ^2.0.x --> >=2.0.0 <3.0.0
863 // ^1.2, ^1.2.x --> >=1.2.0 <2.0.0
864 // ^1.2.3 --> >=1.2.3 <2.0.0
865 // ^1.2.0 --> >=1.2.0 <2.0.0
866 function replaceCarets(comp, loose) {
867 return comp.trim().split(/\s+/).map(function(comp) {
868 return replaceCaret(comp, loose);
872 function replaceCaret(comp, loose) {
874 var r = loose ? re[CARETLOOSE] : re[CARET];
875 return comp.replace(r, function(_, M, m, p, pr) {
882 ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
885 ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
887 ret = '>=' + M + '.' + m + '.0 <' + (+M + 1) + '.0.0';
890 if (pr.charAt(0) !== '-')
894 ret = '>=' + M + '.' + m + '.' + p + pr +
895 ' <' + M + '.' + m + '.' + (+p + 1);
897 ret = '>=' + M + '.' + m + '.' + p + pr +
898 ' <' + M + '.' + (+m + 1) + '.0';
900 ret = '>=' + M + '.' + m + '.' + p + pr +
901 ' <' + (+M + 1) + '.0.0';
906 ret = '>=' + M + '.' + m + '.' + p +
907 ' <' + M + '.' + m + '.' + (+p + 1);
909 ret = '>=' + M + '.' + m + '.' + p +
910 ' <' + M + '.' + (+m + 1) + '.0';
912 ret = '>=' + M + '.' + m + '.' + p +
913 ' <' + (+M + 1) + '.0.0';
921 function replaceXRanges(comp, loose) {
923 return comp.split(/\s+/).map(function(comp) {
924 return replaceXRange(comp, loose);
928 function replaceXRange(comp, loose) {
930 var r = loose ? re[XRANGELOOSE] : re[XRANGE];
931 return comp.replace(r, function(ret, gtlt, M, m, p, pr) {
934 var xm = xM || isX(m);
935 var xp = xm || isX(p);
938 if (gtlt === '=' && anyX)
942 if (gtlt === '>' || gtlt === '<') {
943 // nothing is allowed
946 // nothing is forbidden
949 } else if (gtlt && anyX) {
959 // >1.2.3 => >= 1.2.4
969 } else if (gtlt === '<=') {
970 // <=0.7.x is actually <0.8.0, since any 0.7.x should
971 // pass. Similarly, <=7.x is actually <8.0.0, etc.
979 ret = gtlt + M + '.' + m + '.' + p;
981 ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
983 ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
992 // Because * is AND-ed with everything else in the comparator,
993 // and '' means "any version", just remove the *s entirely.
994 function replaceStars(comp, loose) {
996 // Looseness is ignored here. star is always as loose as it gets!
997 return comp.trim().replace(re[STAR], '');
1000 // This function is passed to string.replace(re[HYPHENRANGE])
1001 // M, m, patch, prerelease, build
1002 // 1.2 - 3.4.5 => >=1.2.0 <=3.4.5
1003 // 1.2.3 - 3.4 => >=1.2.0 <3.5.0 Any 3.4.x will do
1004 // 1.2 - 3.4 => >=1.2.0 <3.5.0
1005 function hyphenReplace($0,
1006 from, fM, fm, fp, fpr, fb,
1007 to, tM, tm, tp, tpr, tb) {
1012 from = '>=' + fM + '.0.0';
1014 from = '>=' + fM + '.' + fm + '.0';
1021 to = '<' + (+tM + 1) + '.0.0';
1023 to = '<' + tM + '.' + (+tm + 1) + '.0';
1025 to = '<=' + tM + '.' + tm + '.' + tp + '-' + tpr;
1029 return (from + ' ' + to).trim();
1033 // if ANY of the sets match ALL of its comparators, then pass
1034 Range.prototype.test = function(version) {
1038 if (typeof version === 'string')
1039 version = new SemVer(version, this.loose);
1041 for (var i = 0; i < this.set.length; i++) {
1042 if (testSet(this.set[i], version))
1048 function testSet(set, version) {
1049 for (var i = 0; i < set.length; i++) {
1050 if (!set[i].test(version))
1054 if (version.prerelease.length) {
1055 // Find the set of versions that are allowed to have prereleases
1056 // For example, ^1.2.3-pr.1 desugars to >=1.2.3-pr.1 <2.0.0
1057 // That should allow `1.2.3-pr.2` to pass.
1058 // However, `1.2.4-alpha.notready` should NOT be allowed,
1059 // even though it's within the range set by the comparators.
1060 for (var i = 0; i < set.length; i++) {
1062 if (set[i].semver === ANY)
1065 if (set[i].semver.prerelease.length > 0) {
1066 var allowed = set[i].semver;
1067 if (allowed.major === version.major &&
1068 allowed.minor === version.minor &&
1069 allowed.patch === version.patch)
1074 // Version has a -pre, but it's not one of the ones we like.
1081 exports.satisfies = satisfies;
1082 function satisfies(version, range, loose) {
1084 range = new Range(range, loose);
1088 return range.test(version);
1091 exports.maxSatisfying = maxSatisfying;
1092 function maxSatisfying(versions, range, loose) {
1093 return versions.filter(function(version) {
1094 return satisfies(version, range, loose);
1095 }).sort(function(a, b) {
1096 return rcompare(a, b, loose);
1100 exports.validRange = validRange;
1101 function validRange(range, loose) {
1103 // Return '*' instead of '' so that truthiness works.
1104 // This will throw if it's invalid anyway
1105 return new Range(range, loose).range || '*';
1111 // Determine if version is less than all the versions possible in the range
1113 function ltr(version, range, loose) {
1114 return outside(version, range, '<', loose);
1117 // Determine if version is greater than all the versions possible in the range.
1119 function gtr(version, range, loose) {
1120 return outside(version, range, '>', loose);
1123 exports.outside = outside;
1124 function outside(version, range, hilo, loose) {
1125 version = new SemVer(version, loose);
1126 range = new Range(range, loose);
1128 var gtfn, ltefn, ltfn, comp, ecomp;
1145 throw new TypeError('Must provide a hilo val of "<" or ">"');
1148 // If it satisifes the range it is not outside
1149 if (satisfies(version, range, loose)) {
1153 // From now on, variable terms are as if we're in "gtr" mode.
1154 // but note that everything is flipped for the "ltr" function.
1156 for (var i = 0; i < range.set.length; ++i) {
1157 var comparators = range.set[i];
1162 comparators.forEach(function(comparator) {
1163 if (comparator.semver === ANY) {
1164 comparator = new Comparator('>=0.0.0')
1166 high = high || comparator;
1167 low = low || comparator;
1168 if (gtfn(comparator.semver, high.semver, loose)) {
1170 } else if (ltfn(comparator.semver, low.semver, loose)) {
1175 // If the edge version comparator has a operator then our version
1177 if (high.operator === comp || high.operator === ecomp) {
1181 // If the lowest version comparator has an operator and our version
1182 // is less than it then it isn't higher than the range
1183 if ((!low.operator || low.operator === comp) &&
1184 ltefn(version, low.semver)) {
1186 } else if (low.operator === ecomp && ltfn(version, low.semver)) {
1193 // Use the define() function if we're in AMD land
1194 if (typeof define === 'function' && define.amd)
1198 typeof exports === 'object' ? exports :
1199 typeof define === 'function' && define.amd ? {} :