npm: upgrade to v1.4.14
[platform/upstream/nodejs.git] / deps / npm / node_modules / semver / semver.browser.js
1 ;(function(exports) {
2
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;
6
7 // The debug function is excluded entirely from the minified version.
8
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';
12
13 // The actual regexps go on exports.re
14 var re = exports.re = [];
15 var src = exports.src = [];
16 var R = 0;
17
18 // The following Regular Expressions can be used for tokenizing,
19 // validating, and parsing SemVer version strings.
20
21 // ## Numeric Identifier
22 // A single `0`, or a non-zero digit followed by zero or more digits.
23
24 var NUMERICIDENTIFIER = R++;
25 src[NUMERICIDENTIFIER] = '0|[1-9]\\d*';
26 var NUMERICIDENTIFIERLOOSE = R++;
27 src[NUMERICIDENTIFIERLOOSE] = '[0-9]+';
28
29
30 // ## Non-numeric Identifier
31 // Zero or more digits, followed by a letter or hyphen, and then zero or
32 // more letters, digits, or hyphens.
33
34 var NONNUMERICIDENTIFIER = R++;
35 src[NONNUMERICIDENTIFIER] = '\\d*[a-zA-Z-][a-zA-Z0-9-]*';
36
37
38 // ## Main Version
39 // Three dot-separated numeric identifiers.
40
41 var MAINVERSION = R++;
42 src[MAINVERSION] = '(' + src[NUMERICIDENTIFIER] + ')\\.' +
43                    '(' + src[NUMERICIDENTIFIER] + ')\\.' +
44                    '(' + src[NUMERICIDENTIFIER] + ')';
45
46 var MAINVERSIONLOOSE = R++;
47 src[MAINVERSIONLOOSE] = '(' + src[NUMERICIDENTIFIERLOOSE] + ')\\.' +
48                         '(' + src[NUMERICIDENTIFIERLOOSE] + ')\\.' +
49                         '(' + src[NUMERICIDENTIFIERLOOSE] + ')';
50
51 // ## Pre-release Version Identifier
52 // A numeric identifier, or a non-numeric identifier.
53
54 var PRERELEASEIDENTIFIER = R++;
55 src[PRERELEASEIDENTIFIER] = '(?:' + src[NUMERICIDENTIFIER] +
56                             '|' + src[NONNUMERICIDENTIFIER] + ')';
57
58 var PRERELEASEIDENTIFIERLOOSE = R++;
59 src[PRERELEASEIDENTIFIERLOOSE] = '(?:' + src[NUMERICIDENTIFIERLOOSE] +
60                                  '|' + src[NONNUMERICIDENTIFIER] + ')';
61
62
63 // ## Pre-release Version
64 // Hyphen, followed by one or more dot-separated pre-release version
65 // identifiers.
66
67 var PRERELEASE = R++;
68 src[PRERELEASE] = '(?:-(' + src[PRERELEASEIDENTIFIER] +
69                   '(?:\\.' + src[PRERELEASEIDENTIFIER] + ')*))';
70
71 var PRERELEASELOOSE = R++;
72 src[PRERELEASELOOSE] = '(?:-?(' + src[PRERELEASEIDENTIFIERLOOSE] +
73                        '(?:\\.' + src[PRERELEASEIDENTIFIERLOOSE] + ')*))';
74
75 // ## Build Metadata Identifier
76 // Any combination of digits, letters, or hyphens.
77
78 var BUILDIDENTIFIER = R++;
79 src[BUILDIDENTIFIER] = '[0-9A-Za-z-]+';
80
81 // ## Build Metadata
82 // Plus sign, followed by one or more period-separated build metadata
83 // identifiers.
84
85 var BUILD = R++;
86 src[BUILD] = '(?:\\+(' + src[BUILDIDENTIFIER] +
87              '(?:\\.' + src[BUILDIDENTIFIER] + ')*))';
88
89
90 // ## Full Version String
91 // A main version, followed optionally by a pre-release version and
92 // build metadata.
93
94 // Note that the only major, minor, patch, and pre-release sections of
95 // the version string are capturing groups.  The build metadata is not a
96 // capturing group, because it should not ever be used in version
97 // comparison.
98
99 var FULL = R++;
100 var FULLPLAIN = 'v?' + src[MAINVERSION] +
101                 src[PRERELEASE] + '?' +
102                 src[BUILD] + '?';
103
104 src[FULL] = '^' + FULLPLAIN + '$';
105
106 // like full, but allows v1.2.3 and =1.2.3, which people do sometimes.
107 // also, 1.0.0alpha1 (prerelease without the hyphen) which is pretty
108 // common in the npm registry.
109 var LOOSEPLAIN = '[v=\\s]*' + src[MAINVERSIONLOOSE] +
110                  src[PRERELEASELOOSE] + '?' +
111                  src[BUILD] + '?';
112
113 var LOOSE = R++;
114 src[LOOSE] = '^' + LOOSEPLAIN + '$';
115
116 var GTLT = R++;
117 src[GTLT] = '((?:<|>)?=?)';
118
119 // Something like "2.*" or "1.2.x".
120 // Note that "x.x" is a valid xRange identifer, meaning "any version"
121 // Only the first item is strictly required.
122 var XRANGEIDENTIFIERLOOSE = R++;
123 src[XRANGEIDENTIFIERLOOSE] = src[NUMERICIDENTIFIERLOOSE] + '|x|X|\\*';
124 var XRANGEIDENTIFIER = R++;
125 src[XRANGEIDENTIFIER] = src[NUMERICIDENTIFIER] + '|x|X|\\*';
126
127 var XRANGEPLAIN = R++;
128 src[XRANGEPLAIN] = '[v=\\s]*(' + src[XRANGEIDENTIFIER] + ')' +
129                    '(?:\\.(' + src[XRANGEIDENTIFIER] + ')' +
130                    '(?:\\.(' + src[XRANGEIDENTIFIER] + ')' +
131                    '(?:(' + src[PRERELEASE] + ')' +
132                    ')?)?)?';
133
134 var XRANGEPLAINLOOSE = R++;
135 src[XRANGEPLAINLOOSE] = '[v=\\s]*(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
136                         '(?:\\.(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
137                         '(?:\\.(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
138                         '(?:(' + src[PRERELEASELOOSE] + ')' +
139                         ')?)?)?';
140
141 // >=2.x, for example, means >=2.0.0-0
142 // <1.x would be the same as "<1.0.0-0", though.
143 var XRANGE = R++;
144 src[XRANGE] = '^' + src[GTLT] + '\\s*' + src[XRANGEPLAIN] + '$';
145 var XRANGELOOSE = R++;
146 src[XRANGELOOSE] = '^' + src[GTLT] + '\\s*' + src[XRANGEPLAINLOOSE] + '$';
147
148 // Tilde ranges.
149 // Meaning is "reasonably at or greater than"
150 var LONETILDE = R++;
151 src[LONETILDE] = '(?:~>?)';
152
153 var TILDETRIM = R++;
154 src[TILDETRIM] = '(\\s*)' + src[LONETILDE] + '\\s+';
155 re[TILDETRIM] = new RegExp(src[TILDETRIM], 'g');
156 var tildeTrimReplace = '$1~';
157
158 var TILDE = R++;
159 src[TILDE] = '^' + src[LONETILDE] + src[XRANGEPLAIN] + '$';
160 var TILDELOOSE = R++;
161 src[TILDELOOSE] = '^' + src[LONETILDE] + src[XRANGEPLAINLOOSE] + '$';
162
163 // Caret ranges.
164 // Meaning is "at least and backwards compatible with"
165 var LONECARET = R++;
166 src[LONECARET] = '(?:\\^)';
167
168 var CARETTRIM = R++;
169 src[CARETTRIM] = '(\\s*)' + src[LONECARET] + '\\s+';
170 re[CARETTRIM] = new RegExp(src[CARETTRIM], 'g');
171 var caretTrimReplace = '$1^';
172
173 var CARET = R++;
174 src[CARET] = '^' + src[LONECARET] + src[XRANGEPLAIN] + '$';
175 var CARETLOOSE = R++;
176 src[CARETLOOSE] = '^' + src[LONECARET] + src[XRANGEPLAINLOOSE] + '$';
177
178 // A simple gt/lt/eq thing, or just "" to indicate "any version"
179 var COMPARATORLOOSE = R++;
180 src[COMPARATORLOOSE] = '^' + src[GTLT] + '\\s*(' + LOOSEPLAIN + ')$|^$';
181 var COMPARATOR = R++;
182 src[COMPARATOR] = '^' + src[GTLT] + '\\s*(' + FULLPLAIN + ')$|^$';
183
184
185 // An expression to strip any whitespace between the gtlt and the thing
186 // it modifies, so that `> 1.2.3` ==> `>1.2.3`
187 var COMPARATORTRIM = R++;
188 src[COMPARATORTRIM] = '(\\s*)' + src[GTLT] +
189                       '\\s*(' + LOOSEPLAIN + '|' + src[XRANGEPLAIN] + ')';
190
191 // this one has to use the /g flag
192 re[COMPARATORTRIM] = new RegExp(src[COMPARATORTRIM], 'g');
193 var comparatorTrimReplace = '$1$2$3';
194
195
196 // Something like `1.2.3 - 1.2.4`
197 // Note that these all use the loose form, because they'll be
198 // checked against either the strict or loose comparator form
199 // later.
200 var HYPHENRANGE = R++;
201 src[HYPHENRANGE] = '^\\s*(' + src[XRANGEPLAIN] + ')' +
202                    '\\s+-\\s+' +
203                    '(' + src[XRANGEPLAIN] + ')' +
204                    '\\s*$';
205
206 var HYPHENRANGELOOSE = R++;
207 src[HYPHENRANGELOOSE] = '^\\s*(' + src[XRANGEPLAINLOOSE] + ')' +
208                         '\\s+-\\s+' +
209                         '(' + src[XRANGEPLAINLOOSE] + ')' +
210                         '\\s*$';
211
212 // Star ranges basically just allow anything at all.
213 var STAR = R++;
214 src[STAR] = '(<|>)?=?\\s*\\*';
215
216 // Compile to actual regexp objects.
217 // All are flag-free, unless they were created above with a flag.
218 for (var i = 0; i < R; i++) {
219   ;
220   if (!re[i])
221     re[i] = new RegExp(src[i]);
222 }
223
224 exports.parse = parse;
225 function parse(version, loose) {
226   var r = loose ? re[LOOSE] : re[FULL];
227   return (r.test(version)) ? new SemVer(version, loose) : null;
228 }
229
230 exports.valid = valid;
231 function valid(version, loose) {
232   var v = parse(version, loose);
233   return v ? v.version : null;
234 }
235
236
237 exports.clean = clean;
238 function clean(version, loose) {
239   var s = parse(version, loose);
240   return s ? s.version : null;
241 }
242
243 exports.SemVer = SemVer;
244
245 function SemVer(version, loose) {
246   if (version instanceof SemVer) {
247     if (version.loose === loose)
248       return version;
249     else
250       version = version.version;
251   } else if (typeof version !== 'string') {
252     throw new TypeError('Invalid Version: ' + version);
253   }
254
255   if (!(this instanceof SemVer))
256     return new SemVer(version, loose);
257
258   ;
259   this.loose = loose;
260   var m = version.trim().match(loose ? re[LOOSE] : re[FULL]);
261
262   if (!m)
263     throw new TypeError('Invalid Version: ' + version);
264
265   this.raw = version;
266
267   // these are actually numbers
268   this.major = +m[1];
269   this.minor = +m[2];
270   this.patch = +m[3];
271
272   // numberify any prerelease numeric ids
273   if (!m[4])
274     this.prerelease = [];
275   else
276     this.prerelease = m[4].split('.').map(function(id) {
277       return (/^[0-9]+$/.test(id)) ? +id : id;
278     });
279
280   this.build = m[5] ? m[5].split('.') : [];
281   this.format();
282 }
283
284 SemVer.prototype.format = function() {
285   this.version = this.major + '.' + this.minor + '.' + this.patch;
286   if (this.prerelease.length)
287     this.version += '-' + this.prerelease.join('.');
288   return this.version;
289 };
290
291 SemVer.prototype.inspect = function() {
292   return '<SemVer "' + this + '">';
293 };
294
295 SemVer.prototype.toString = function() {
296   return this.version;
297 };
298
299 SemVer.prototype.compare = function(other) {
300   ;
301   if (!(other instanceof SemVer))
302     other = new SemVer(other, this.loose);
303
304   return this.compareMain(other) || this.comparePre(other);
305 };
306
307 SemVer.prototype.compareMain = function(other) {
308   if (!(other instanceof SemVer))
309     other = new SemVer(other, this.loose);
310
311   return compareIdentifiers(this.major, other.major) ||
312          compareIdentifiers(this.minor, other.minor) ||
313          compareIdentifiers(this.patch, other.patch);
314 };
315
316 SemVer.prototype.comparePre = function(other) {
317   if (!(other instanceof SemVer))
318     other = new SemVer(other, this.loose);
319
320   // NOT having a prerelease is > having one
321   if (this.prerelease.length && !other.prerelease.length)
322     return -1;
323   else if (!this.prerelease.length && other.prerelease.length)
324     return 1;
325   else if (!this.prerelease.length && !other.prerelease.length)
326     return 0;
327
328   var i = 0;
329   do {
330     var a = this.prerelease[i];
331     var b = other.prerelease[i];
332     ;
333     if (a === undefined && b === undefined)
334       return 0;
335     else if (b === undefined)
336       return 1;
337     else if (a === undefined)
338       return -1;
339     else if (a === b)
340       continue;
341     else
342       return compareIdentifiers(a, b);
343   } while (++i);
344 };
345
346 // preminor will bump the version up to the next minor release, and immediately
347 // down to pre-release. premajor and prepatch work the same way.
348 SemVer.prototype.inc = function(release) {
349   switch (release) {
350     case 'premajor':
351       this.inc('major');
352       this.inc('pre');
353       break;
354     case 'preminor':
355       this.inc('minor');
356       this.inc('pre');
357       break;
358     case 'prepatch':
359       this.inc('patch');
360       this.inc('pre');
361       break;
362     // If the input is a non-prerelease version, this acts the same as
363     // prepatch.
364     case 'prerelease':
365       if (this.prerelease.length === 0)
366         this.inc('patch');
367       this.inc('pre');
368       break;
369     case 'major':
370       this.major++;
371       this.minor = -1;
372     case 'minor':
373       this.minor++;
374       this.patch = 0;
375       this.prerelease = [];
376       break;
377     case 'patch':
378       // If this is not a pre-release version, it will increment the patch.
379       // If it is a pre-release it will bump up to the same patch version.
380       // 1.2.0-5 patches to 1.2.0
381       // 1.2.0 patches to 1.2.1
382       if (this.prerelease.length === 0)
383         this.patch++;
384       this.prerelease = [];
385       break;
386     // This probably shouldn't be used publically.
387     // 1.0.0 "pre" would become 1.0.0-0 which is the wrong direction.
388     case 'pre':
389       if (this.prerelease.length === 0)
390         this.prerelease = [0];
391       else {
392         var i = this.prerelease.length;
393         while (--i >= 0) {
394           if (typeof this.prerelease[i] === 'number') {
395             this.prerelease[i]++;
396             i = -2;
397           }
398         }
399         if (i === -1) // didn't increment anything
400           this.prerelease.push(0);
401       }
402       break;
403
404     default:
405       throw new Error('invalid increment argument: ' + release);
406   }
407   this.format();
408   return this;
409 };
410
411 exports.inc = inc;
412 function inc(version, release, loose) {
413   try {
414     return new SemVer(version, loose).inc(release).version;
415   } catch (er) {
416     return null;
417   }
418 }
419
420 exports.compareIdentifiers = compareIdentifiers;
421
422 var numeric = /^[0-9]+$/;
423 function compareIdentifiers(a, b) {
424   var anum = numeric.test(a);
425   var bnum = numeric.test(b);
426
427   if (anum && bnum) {
428     a = +a;
429     b = +b;
430   }
431
432   return (anum && !bnum) ? -1 :
433          (bnum && !anum) ? 1 :
434          a < b ? -1 :
435          a > b ? 1 :
436          0;
437 }
438
439 exports.rcompareIdentifiers = rcompareIdentifiers;
440 function rcompareIdentifiers(a, b) {
441   return compareIdentifiers(b, a);
442 }
443
444 exports.compare = compare;
445 function compare(a, b, loose) {
446   return new SemVer(a, loose).compare(b);
447 }
448
449 exports.compareLoose = compareLoose;
450 function compareLoose(a, b) {
451   return compare(a, b, true);
452 }
453
454 exports.rcompare = rcompare;
455 function rcompare(a, b, loose) {
456   return compare(b, a, loose);
457 }
458
459 exports.sort = sort;
460 function sort(list, loose) {
461   return list.sort(function(a, b) {
462     return exports.compare(a, b, loose);
463   });
464 }
465
466 exports.rsort = rsort;
467 function rsort(list, loose) {
468   return list.sort(function(a, b) {
469     return exports.rcompare(a, b, loose);
470   });
471 }
472
473 exports.gt = gt;
474 function gt(a, b, loose) {
475   return compare(a, b, loose) > 0;
476 }
477
478 exports.lt = lt;
479 function lt(a, b, loose) {
480   return compare(a, b, loose) < 0;
481 }
482
483 exports.eq = eq;
484 function eq(a, b, loose) {
485   return compare(a, b, loose) === 0;
486 }
487
488 exports.neq = neq;
489 function neq(a, b, loose) {
490   return compare(a, b, loose) !== 0;
491 }
492
493 exports.gte = gte;
494 function gte(a, b, loose) {
495   return compare(a, b, loose) >= 0;
496 }
497
498 exports.lte = lte;
499 function lte(a, b, loose) {
500   return compare(a, b, loose) <= 0;
501 }
502
503 exports.cmp = cmp;
504 function cmp(a, op, b, loose) {
505   var ret;
506   switch (op) {
507     case '===': ret = a === b; break;
508     case '!==': ret = a !== b; break;
509     case '': case '=': case '==': ret = eq(a, b, loose); break;
510     case '!=': ret = neq(a, b, loose); break;
511     case '>': ret = gt(a, b, loose); break;
512     case '>=': ret = gte(a, b, loose); break;
513     case '<': ret = lt(a, b, loose); break;
514     case '<=': ret = lte(a, b, loose); break;
515     default: throw new TypeError('Invalid operator: ' + op);
516   }
517   return ret;
518 }
519
520 exports.Comparator = Comparator;
521 function Comparator(comp, loose) {
522   if (comp instanceof Comparator) {
523     if (comp.loose === loose)
524       return comp;
525     else
526       comp = comp.value;
527   }
528
529   if (!(this instanceof Comparator))
530     return new Comparator(comp, loose);
531
532   ;
533   this.loose = loose;
534   this.parse(comp);
535
536   if (this.semver === ANY)
537     this.value = '';
538   else
539     this.value = this.operator + this.semver.version;
540 }
541
542 var ANY = {};
543 Comparator.prototype.parse = function(comp) {
544   var r = this.loose ? re[COMPARATORLOOSE] : re[COMPARATOR];
545   var m = comp.match(r);
546
547   if (!m)
548     throw new TypeError('Invalid comparator: ' + comp);
549
550   this.operator = m[1];
551   // if it literally is just '>' or '' then allow anything.
552   if (!m[2])
553     this.semver = ANY;
554   else {
555     this.semver = new SemVer(m[2], this.loose);
556
557     // <1.2.3-rc DOES allow 1.2.3-beta (has prerelease)
558     // >=1.2.3 DOES NOT allow 1.2.3-beta
559     // <=1.2.3 DOES allow 1.2.3-beta
560     // However, <1.2.3 does NOT allow 1.2.3-beta,
561     // even though `1.2.3-beta < 1.2.3`
562     // The assumption is that the 1.2.3 version has something you
563     // *don't* want, so we push the prerelease down to the minimum.
564     if (this.operator === '<' && !this.semver.prerelease.length) {
565       this.semver.prerelease = ['0'];
566       this.semver.format();
567     }
568   }
569 };
570
571 Comparator.prototype.inspect = function() {
572   return '<SemVer Comparator "' + this + '">';
573 };
574
575 Comparator.prototype.toString = function() {
576   return this.value;
577 };
578
579 Comparator.prototype.test = function(version) {
580   ;
581   return (this.semver === ANY) ? true :
582          cmp(version, this.operator, this.semver, this.loose);
583 };
584
585
586 exports.Range = Range;
587 function Range(range, loose) {
588   if ((range instanceof Range) && range.loose === loose)
589     return range;
590
591   if (!(this instanceof Range))
592     return new Range(range, loose);
593
594   this.loose = loose;
595
596   // First, split based on boolean or ||
597   this.raw = range;
598   this.set = range.split(/\s*\|\|\s*/).map(function(range) {
599     return this.parseRange(range.trim());
600   }, this).filter(function(c) {
601     // throw out any that are not relevant for whatever reason
602     return c.length;
603   });
604
605   if (!this.set.length) {
606     throw new TypeError('Invalid SemVer Range: ' + range);
607   }
608
609   this.format();
610 }
611
612 Range.prototype.inspect = function() {
613   return '<SemVer Range "' + this.range + '">';
614 };
615
616 Range.prototype.format = function() {
617   this.range = this.set.map(function(comps) {
618     return comps.join(' ').trim();
619   }).join('||').trim();
620   return this.range;
621 };
622
623 Range.prototype.toString = function() {
624   return this.range;
625 };
626
627 Range.prototype.parseRange = function(range) {
628   var loose = this.loose;
629   range = range.trim();
630   ;
631   // `1.2.3 - 1.2.4` => `>=1.2.3 <=1.2.4`
632   var hr = loose ? re[HYPHENRANGELOOSE] : re[HYPHENRANGE];
633   range = range.replace(hr, hyphenReplace);
634   ;
635   // `> 1.2.3 < 1.2.5` => `>1.2.3 <1.2.5`
636   range = range.replace(re[COMPARATORTRIM], comparatorTrimReplace);
637   ;
638
639   // `~ 1.2.3` => `~1.2.3`
640   range = range.replace(re[TILDETRIM], tildeTrimReplace);
641
642   // `^ 1.2.3` => `^1.2.3`
643   range = range.replace(re[CARETTRIM], caretTrimReplace);
644
645   // normalize spaces
646   range = range.split(/\s+/).join(' ');
647
648   // At this point, the range is completely trimmed and
649   // ready to be split into comparators.
650
651   var compRe = loose ? re[COMPARATORLOOSE] : re[COMPARATOR];
652   var set = range.split(' ').map(function(comp) {
653     return parseComparator(comp, loose);
654   }).join(' ').split(/\s+/);
655   if (this.loose) {
656     // in loose mode, throw out any that are not valid comparators
657     set = set.filter(function(comp) {
658       return !!comp.match(compRe);
659     });
660   }
661   set = set.map(function(comp) {
662     return new Comparator(comp, loose);
663   });
664
665   return set;
666 };
667
668 // Mostly just for testing and legacy API reasons
669 exports.toComparators = toComparators;
670 function toComparators(range, loose) {
671   return new Range(range, loose).set.map(function(comp) {
672     return comp.map(function(c) {
673       return c.value;
674     }).join(' ').trim().split(' ');
675   });
676 }
677
678 // comprised of xranges, tildes, stars, and gtlt's at this point.
679 // already replaced the hyphen ranges
680 // turn into a set of JUST comparators.
681 function parseComparator(comp, loose) {
682   ;
683   comp = replaceCarets(comp, loose);
684   ;
685   comp = replaceTildes(comp, loose);
686   ;
687   comp = replaceXRanges(comp, loose);
688   ;
689   comp = replaceStars(comp, loose);
690   ;
691   return comp;
692 }
693
694 function isX(id) {
695   return !id || id.toLowerCase() === 'x' || id === '*';
696 }
697
698 // ~, ~> --> * (any, kinda silly)
699 // ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0 <3.0.0
700 // ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0 <2.1.0
701 // ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0 <1.3.0
702 // ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0
703 // ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0
704 function replaceTildes(comp, loose) {
705   return comp.trim().split(/\s+/).map(function(comp) {
706     return replaceTilde(comp, loose);
707   }).join(' ');
708 }
709
710 function replaceTilde(comp, loose) {
711   var r = loose ? re[TILDELOOSE] : re[TILDE];
712   return comp.replace(r, function(_, M, m, p, pr) {
713     ;
714     var ret;
715
716     if (isX(M))
717       ret = '';
718     else if (isX(m))
719       ret = '>=' + M + '.0.0-0 <' + (+M + 1) + '.0.0-0';
720     else if (isX(p))
721       // ~1.2 == >=1.2.0- <1.3.0-
722       ret = '>=' + M + '.' + m + '.0-0 <' + M + '.' + (+m + 1) + '.0-0';
723     else if (pr) {
724       ;
725       if (pr.charAt(0) !== '-')
726         pr = '-' + pr;
727       ret = '>=' + M + '.' + m + '.' + p + pr +
728             ' <' + M + '.' + (+m + 1) + '.0-0';
729     } else
730       // ~1.2.3 == >=1.2.3-0 <1.3.0-0
731       ret = '>=' + M + '.' + m + '.' + p + '-0' +
732             ' <' + M + '.' + (+m + 1) + '.0-0';
733
734     ;
735     return ret;
736   });
737 }
738
739 // ^ --> * (any, kinda silly)
740 // ^2, ^2.x, ^2.x.x --> >=2.0.0 <3.0.0
741 // ^2.0, ^2.0.x --> >=2.0.0 <3.0.0
742 // ^1.2, ^1.2.x --> >=1.2.0 <2.0.0
743 // ^1.2.3 --> >=1.2.3 <2.0.0
744 // ^1.2.0 --> >=1.2.0 <2.0.0
745 function replaceCarets(comp, loose) {
746   return comp.trim().split(/\s+/).map(function(comp) {
747     return replaceCaret(comp, loose);
748   }).join(' ');
749 }
750
751 function replaceCaret(comp, loose) {
752   var r = loose ? re[CARETLOOSE] : re[CARET];
753   return comp.replace(r, function(_, M, m, p, pr) {
754     ;
755     var ret;
756
757     if (isX(M))
758       ret = '';
759     else if (isX(m))
760       ret = '>=' + M + '.0.0-0 <' + (+M + 1) + '.0.0-0';
761     else if (isX(p)) {
762       if (M === '0')
763         ret = '>=' + M + '.' + m + '.0-0 <' + M + '.' + (+m + 1) + '.0-0';
764       else
765         ret = '>=' + M + '.' + m + '.0-0 <' + (+M + 1) + '.0.0-0';
766     } else if (pr) {
767       ;
768       if (pr.charAt(0) !== '-')
769         pr = '-' + pr;
770       if (M === '0') {
771         if (m === '0')
772           ret = '=' + M + '.' + m + '.' + p + pr;
773         else
774           ret = '>=' + M + '.' + m + '.' + p + pr +
775                 ' <' + M + '.' + (+m + 1) + '.0-0';
776       } else
777         ret = '>=' + M + '.' + m + '.' + p + pr +
778               ' <' + (+M + 1) + '.0.0-0';
779     } else {
780       if (M === '0') {
781         if (m === '0')
782           ret = '=' + M + '.' + m + '.' + p;
783         else
784           ret = '>=' + M + '.' + m + '.' + p + '-0' +
785                 ' <' + M + '.' + (+m + 1) + '.0-0';
786       } else
787         ret = '>=' + M + '.' + m + '.' + p + '-0' +
788               ' <' + (+M + 1) + '.0.0-0';
789     }
790
791     ;
792     return ret;
793   });
794 }
795
796 function replaceXRanges(comp, loose) {
797   ;
798   return comp.split(/\s+/).map(function(comp) {
799     return replaceXRange(comp, loose);
800   }).join(' ');
801 }
802
803 function replaceXRange(comp, loose) {
804   comp = comp.trim();
805   var r = loose ? re[XRANGELOOSE] : re[XRANGE];
806   return comp.replace(r, function(ret, gtlt, M, m, p, pr) {
807     ;
808     var xM = isX(M);
809     var xm = xM || isX(m);
810     var xp = xm || isX(p);
811     var anyX = xp;
812
813     if (gtlt === '=' && anyX)
814       gtlt = '';
815
816     if (gtlt && anyX) {
817       // replace X with 0, and then append the -0 min-prerelease
818       if (xM)
819         M = 0;
820       if (xm)
821         m = 0;
822       if (xp)
823         p = 0;
824
825       if (gtlt === '>') {
826         // >1 => >=2.0.0-0
827         // >1.2 => >=1.3.0-0
828         // >1.2.3 => >= 1.2.4-0
829         gtlt = '>=';
830         if (xM) {
831           // no change
832         } else if (xm) {
833           M = +M + 1;
834           m = 0;
835           p = 0;
836         } else if (xp) {
837           m = +m + 1;
838           p = 0;
839         }
840       }
841
842
843       ret = gtlt + M + '.' + m + '.' + p + '-0';
844     } else if (xM) {
845       // allow any
846       ret = '*';
847     } else if (xm) {
848       // append '-0' onto the version, otherwise
849       // '1.x.x' matches '2.0.0-beta', since the tag
850       // *lowers* the version value
851       ret = '>=' + M + '.0.0-0 <' + (+M + 1) + '.0.0-0';
852     } else if (xp) {
853       ret = '>=' + M + '.' + m + '.0-0 <' + M + '.' + (+m + 1) + '.0-0';
854     }
855
856     ;
857
858     return ret;
859   });
860 }
861
862 // Because * is AND-ed with everything else in the comparator,
863 // and '' means "any version", just remove the *s entirely.
864 function replaceStars(comp, loose) {
865   ;
866   // Looseness is ignored here.  star is always as loose as it gets!
867   return comp.trim().replace(re[STAR], '');
868 }
869
870 // This function is passed to string.replace(re[HYPHENRANGE])
871 // M, m, patch, prerelease, build
872 // 1.2 - 3.4.5 => >=1.2.0-0 <=3.4.5
873 // 1.2.3 - 3.4 => >=1.2.0-0 <3.5.0-0 Any 3.4.x will do
874 // 1.2 - 3.4 => >=1.2.0-0 <3.5.0-0
875 function hyphenReplace($0,
876                        from, fM, fm, fp, fpr, fb,
877                        to, tM, tm, tp, tpr, tb) {
878
879   if (isX(fM))
880     from = '';
881   else if (isX(fm))
882     from = '>=' + fM + '.0.0-0';
883   else if (isX(fp))
884     from = '>=' + fM + '.' + fm + '.0-0';
885   else
886     from = '>=' + from;
887
888   if (isX(tM))
889     to = '';
890   else if (isX(tm))
891     to = '<' + (+tM + 1) + '.0.0-0';
892   else if (isX(tp))
893     to = '<' + tM + '.' + (+tm + 1) + '.0-0';
894   else if (tpr)
895     to = '<=' + tM + '.' + tm + '.' + tp + '-' + tpr;
896   else
897     to = '<=' + to;
898
899   return (from + ' ' + to).trim();
900 }
901
902
903 // if ANY of the sets match ALL of its comparators, then pass
904 Range.prototype.test = function(version) {
905   if (!version)
906     return false;
907   for (var i = 0; i < this.set.length; i++) {
908     if (testSet(this.set[i], version))
909       return true;
910   }
911   return false;
912 };
913
914 function testSet(set, version) {
915   for (var i = 0; i < set.length; i++) {
916     if (!set[i].test(version))
917       return false;
918   }
919   return true;
920 }
921
922 exports.satisfies = satisfies;
923 function satisfies(version, range, loose) {
924   try {
925     range = new Range(range, loose);
926   } catch (er) {
927     return false;
928   }
929   return range.test(version);
930 }
931
932 exports.maxSatisfying = maxSatisfying;
933 function maxSatisfying(versions, range, loose) {
934   return versions.filter(function(version) {
935     return satisfies(version, range, loose);
936   }).sort(function(a, b) {
937     return rcompare(a, b, loose);
938   })[0] || null;
939 }
940
941 exports.validRange = validRange;
942 function validRange(range, loose) {
943   try {
944     // Return '*' instead of '' so that truthiness works.
945     // This will throw if it's invalid anyway
946     return new Range(range, loose).range || '*';
947   } catch (er) {
948     return null;
949   }
950 }
951
952 // Determine if version is less than all the versions possible in the range
953 exports.ltr = ltr;
954 function ltr(version, range, loose) {
955   return outside(version, range, '<', loose);
956 }
957
958 // Determine if version is greater than all the versions possible in the range.
959 exports.gtr = gtr;
960 function gtr(version, range, loose) {
961   return outside(version, range, '>', loose);
962 }
963
964 exports.outside = outside;
965 function outside(version, range, hilo, loose) {
966   version = new SemVer(version, loose);
967   range = new Range(range, loose);
968
969   var gtfn, ltefn, ltfn, comp, ecomp;
970   switch (hilo) {
971     case '>':
972       gtfn = gt;
973       ltefn = lte;
974       ltfn = lt;
975       comp = '>';
976       ecomp = '>=';
977       break;
978     case '<':
979       gtfn = lt;
980       ltefn = gte;
981       ltfn = gt;
982       comp = '<';
983       ecomp = '<=';
984       break;
985     default:
986       throw new TypeError('Must provide a hilo val of "<" or ">"');
987   }
988
989   // If it satisifes the range it is not outside
990   if (satisfies(version, range, loose)) {
991     return false;
992   }
993
994   // From now on, variable terms are as if we're in "gtr" mode.
995   // but note that everything is flipped for the "ltr" function.
996
997   for (var i = 0; i < range.set.length; ++i) {
998     var comparators = range.set[i];
999
1000     var high = null;
1001     var low = null;
1002
1003     comparators.forEach(function(comparator) {
1004       high = high || comparator;
1005       low = low || comparator;
1006       if (gtfn(comparator.semver, high.semver, loose)) {
1007         high = comparator;
1008       } else if (ltfn(comparator.semver, low.semver, loose)) {
1009         low = comparator;
1010       }
1011     });
1012
1013     // If the edge version comparator has a operator then our version
1014     // isn't outside it
1015     if (high.operator === comp || high.operator === ecomp) {
1016       return false;
1017     }
1018
1019     // If the lowest version comparator has an operator and our version
1020     // is less than it then it isn't higher than the range
1021     if ((!low.operator || low.operator === comp) &&
1022         ltefn(version, low.semver)) {
1023       return false;
1024     } else if (low.operator === ecomp && ltfn(version, low.semver)) {
1025       return false;
1026     }
1027   }
1028   return true;
1029 }
1030
1031 // Use the define() function if we're in AMD land
1032 if (typeof define === 'function' && define.amd)
1033   define(exports);
1034
1035 })(
1036   typeof exports === 'object' ? exports :
1037   typeof define === 'function' && define.amd ? {} :
1038   semver = {}
1039 );