Initial commit
[yaffs-website] / 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 var MAX_LENGTH = 256;
14 var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
15
16 // The actual regexps go on exports.re
17 var re = exports.re = [];
18 var src = exports.src = [];
19 var R = 0;
20
21 // The following Regular Expressions can be used for tokenizing,
22 // validating, and parsing SemVer version strings.
23
24 // ## Numeric Identifier
25 // A single `0`, or a non-zero digit followed by zero or more digits.
26
27 var NUMERICIDENTIFIER = R++;
28 src[NUMERICIDENTIFIER] = '0|[1-9]\\d*';
29 var NUMERICIDENTIFIERLOOSE = R++;
30 src[NUMERICIDENTIFIERLOOSE] = '[0-9]+';
31
32
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.
36
37 var NONNUMERICIDENTIFIER = R++;
38 src[NONNUMERICIDENTIFIER] = '\\d*[a-zA-Z-][a-zA-Z0-9-]*';
39
40
41 // ## Main Version
42 // Three dot-separated numeric identifiers.
43
44 var MAINVERSION = R++;
45 src[MAINVERSION] = '(' + src[NUMERICIDENTIFIER] + ')\\.' +
46                    '(' + src[NUMERICIDENTIFIER] + ')\\.' +
47                    '(' + src[NUMERICIDENTIFIER] + ')';
48
49 var MAINVERSIONLOOSE = R++;
50 src[MAINVERSIONLOOSE] = '(' + src[NUMERICIDENTIFIERLOOSE] + ')\\.' +
51                         '(' + src[NUMERICIDENTIFIERLOOSE] + ')\\.' +
52                         '(' + src[NUMERICIDENTIFIERLOOSE] + ')';
53
54 // ## Pre-release Version Identifier
55 // A numeric identifier, or a non-numeric identifier.
56
57 var PRERELEASEIDENTIFIER = R++;
58 src[PRERELEASEIDENTIFIER] = '(?:' + src[NUMERICIDENTIFIER] +
59                             '|' + src[NONNUMERICIDENTIFIER] + ')';
60
61 var PRERELEASEIDENTIFIERLOOSE = R++;
62 src[PRERELEASEIDENTIFIERLOOSE] = '(?:' + src[NUMERICIDENTIFIERLOOSE] +
63                                  '|' + src[NONNUMERICIDENTIFIER] + ')';
64
65
66 // ## Pre-release Version
67 // Hyphen, followed by one or more dot-separated pre-release version
68 // identifiers.
69
70 var PRERELEASE = R++;
71 src[PRERELEASE] = '(?:-(' + src[PRERELEASEIDENTIFIER] +
72                   '(?:\\.' + src[PRERELEASEIDENTIFIER] + ')*))';
73
74 var PRERELEASELOOSE = R++;
75 src[PRERELEASELOOSE] = '(?:-?(' + src[PRERELEASEIDENTIFIERLOOSE] +
76                        '(?:\\.' + src[PRERELEASEIDENTIFIERLOOSE] + ')*))';
77
78 // ## Build Metadata Identifier
79 // Any combination of digits, letters, or hyphens.
80
81 var BUILDIDENTIFIER = R++;
82 src[BUILDIDENTIFIER] = '[0-9A-Za-z-]+';
83
84 // ## Build Metadata
85 // Plus sign, followed by one or more period-separated build metadata
86 // identifiers.
87
88 var BUILD = R++;
89 src[BUILD] = '(?:\\+(' + src[BUILDIDENTIFIER] +
90              '(?:\\.' + src[BUILDIDENTIFIER] + ')*))';
91
92
93 // ## Full Version String
94 // A main version, followed optionally by a pre-release version and
95 // build metadata.
96
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
100 // comparison.
101
102 var FULL = R++;
103 var FULLPLAIN = 'v?' + src[MAINVERSION] +
104                 src[PRERELEASE] + '?' +
105                 src[BUILD] + '?';
106
107 src[FULL] = '^' + FULLPLAIN + '$';
108
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] + '?' +
114                  src[BUILD] + '?';
115
116 var LOOSE = R++;
117 src[LOOSE] = '^' + LOOSEPLAIN + '$';
118
119 var GTLT = R++;
120 src[GTLT] = '((?:<|>)?=?)';
121
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|\\*';
129
130 var XRANGEPLAIN = R++;
131 src[XRANGEPLAIN] = '[v=\\s]*(' + src[XRANGEIDENTIFIER] + ')' +
132                    '(?:\\.(' + src[XRANGEIDENTIFIER] + ')' +
133                    '(?:\\.(' + src[XRANGEIDENTIFIER] + ')' +
134                    '(?:' + src[PRERELEASE] + ')?' +
135                    src[BUILD] + '?' +
136                    ')?)?';
137
138 var XRANGEPLAINLOOSE = R++;
139 src[XRANGEPLAINLOOSE] = '[v=\\s]*(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
140                         '(?:\\.(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
141                         '(?:\\.(' + src[XRANGEIDENTIFIERLOOSE] + ')' +
142                         '(?:' + src[PRERELEASELOOSE] + ')?' +
143                         src[BUILD] + '?' +
144                         ')?)?';
145
146 var XRANGE = R++;
147 src[XRANGE] = '^' + src[GTLT] + '\\s*' + src[XRANGEPLAIN] + '$';
148 var XRANGELOOSE = R++;
149 src[XRANGELOOSE] = '^' + src[GTLT] + '\\s*' + src[XRANGEPLAINLOOSE] + '$';
150
151 // Tilde ranges.
152 // Meaning is "reasonably at or greater than"
153 var LONETILDE = R++;
154 src[LONETILDE] = '(?:~>?)';
155
156 var TILDETRIM = R++;
157 src[TILDETRIM] = '(\\s*)' + src[LONETILDE] + '\\s+';
158 re[TILDETRIM] = new RegExp(src[TILDETRIM], 'g');
159 var tildeTrimReplace = '$1~';
160
161 var TILDE = R++;
162 src[TILDE] = '^' + src[LONETILDE] + src[XRANGEPLAIN] + '$';
163 var TILDELOOSE = R++;
164 src[TILDELOOSE] = '^' + src[LONETILDE] + src[XRANGEPLAINLOOSE] + '$';
165
166 // Caret ranges.
167 // Meaning is "at least and backwards compatible with"
168 var LONECARET = R++;
169 src[LONECARET] = '(?:\\^)';
170
171 var CARETTRIM = R++;
172 src[CARETTRIM] = '(\\s*)' + src[LONECARET] + '\\s+';
173 re[CARETTRIM] = new RegExp(src[CARETTRIM], 'g');
174 var caretTrimReplace = '$1^';
175
176 var CARET = R++;
177 src[CARET] = '^' + src[LONECARET] + src[XRANGEPLAIN] + '$';
178 var CARETLOOSE = R++;
179 src[CARETLOOSE] = '^' + src[LONECARET] + src[XRANGEPLAINLOOSE] + '$';
180
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 + ')$|^$';
186
187
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] + ')';
193
194 // this one has to use the /g flag
195 re[COMPARATORTRIM] = new RegExp(src[COMPARATORTRIM], 'g');
196 var comparatorTrimReplace = '$1$2$3';
197
198
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
202 // later.
203 var HYPHENRANGE = R++;
204 src[HYPHENRANGE] = '^\\s*(' + src[XRANGEPLAIN] + ')' +
205                    '\\s+-\\s+' +
206                    '(' + src[XRANGEPLAIN] + ')' +
207                    '\\s*$';
208
209 var HYPHENRANGELOOSE = R++;
210 src[HYPHENRANGELOOSE] = '^\\s*(' + src[XRANGEPLAINLOOSE] + ')' +
211                         '\\s+-\\s+' +
212                         '(' + src[XRANGEPLAINLOOSE] + ')' +
213                         '\\s*$';
214
215 // Star ranges basically just allow anything at all.
216 var STAR = R++;
217 src[STAR] = '(<|>)?=?\\s*\\*';
218
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++) {
222   ;
223   if (!re[i])
224     re[i] = new RegExp(src[i]);
225 }
226
227 exports.parse = parse;
228 function parse(version, loose) {
229   if (version instanceof SemVer)
230     return version;
231
232   if (typeof version !== 'string')
233     return null;
234
235   if (version.length > MAX_LENGTH)
236     return null;
237
238   var r = loose ? re[LOOSE] : re[FULL];
239   if (!r.test(version))
240     return null;
241
242   try {
243     return new SemVer(version, loose);
244   } catch (er) {
245     return null;
246   }
247 }
248
249 exports.valid = valid;
250 function valid(version, loose) {
251   var v = parse(version, loose);
252   return v ? v.version : null;
253 }
254
255
256 exports.clean = clean;
257 function clean(version, loose) {
258   var s = parse(version.trim().replace(/^[=v]+/, ''), loose);
259   return s ? s.version : null;
260 }
261
262 exports.SemVer = SemVer;
263
264 function SemVer(version, loose) {
265   if (version instanceof SemVer) {
266     if (version.loose === loose)
267       return version;
268     else
269       version = version.version;
270   } else if (typeof version !== 'string') {
271     throw new TypeError('Invalid Version: ' + version);
272   }
273
274   if (version.length > MAX_LENGTH)
275     throw new TypeError('version is longer than ' + MAX_LENGTH + ' characters')
276
277   if (!(this instanceof SemVer))
278     return new SemVer(version, loose);
279
280   ;
281   this.loose = loose;
282   var m = version.trim().match(loose ? re[LOOSE] : re[FULL]);
283
284   if (!m)
285     throw new TypeError('Invalid Version: ' + version);
286
287   this.raw = version;
288
289   // these are actually numbers
290   this.major = +m[1];
291   this.minor = +m[2];
292   this.patch = +m[3];
293
294   if (this.major > MAX_SAFE_INTEGER || this.major < 0)
295     throw new TypeError('Invalid major version')
296
297   if (this.minor > MAX_SAFE_INTEGER || this.minor < 0)
298     throw new TypeError('Invalid minor version')
299
300   if (this.patch > MAX_SAFE_INTEGER || this.patch < 0)
301     throw new TypeError('Invalid patch version')
302
303   // numberify any prerelease numeric ids
304   if (!m[4])
305     this.prerelease = [];
306   else
307     this.prerelease = m[4].split('.').map(function(id) {
308       if (/^[0-9]+$/.test(id)) {
309         var num = +id
310         if (num >= 0 && num < MAX_SAFE_INTEGER)
311           return num
312       }
313       return id;
314     });
315
316   this.build = m[5] ? m[5].split('.') : [];
317   this.format();
318 }
319
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('.');
324   return this.version;
325 };
326
327 SemVer.prototype.inspect = function() {
328   return '<SemVer "' + this + '">';
329 };
330
331 SemVer.prototype.toString = function() {
332   return this.version;
333 };
334
335 SemVer.prototype.compare = function(other) {
336   ;
337   if (!(other instanceof SemVer))
338     other = new SemVer(other, this.loose);
339
340   return this.compareMain(other) || this.comparePre(other);
341 };
342
343 SemVer.prototype.compareMain = function(other) {
344   if (!(other instanceof SemVer))
345     other = new SemVer(other, this.loose);
346
347   return compareIdentifiers(this.major, other.major) ||
348          compareIdentifiers(this.minor, other.minor) ||
349          compareIdentifiers(this.patch, other.patch);
350 };
351
352 SemVer.prototype.comparePre = function(other) {
353   if (!(other instanceof SemVer))
354     other = new SemVer(other, this.loose);
355
356   // NOT having a prerelease is > having one
357   if (this.prerelease.length && !other.prerelease.length)
358     return -1;
359   else if (!this.prerelease.length && other.prerelease.length)
360     return 1;
361   else if (!this.prerelease.length && !other.prerelease.length)
362     return 0;
363
364   var i = 0;
365   do {
366     var a = this.prerelease[i];
367     var b = other.prerelease[i];
368     ;
369     if (a === undefined && b === undefined)
370       return 0;
371     else if (b === undefined)
372       return 1;
373     else if (a === undefined)
374       return -1;
375     else if (a === b)
376       continue;
377     else
378       return compareIdentifiers(a, b);
379   } while (++i);
380 };
381
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) {
385   switch (release) {
386     case 'premajor':
387       this.prerelease.length = 0;
388       this.patch = 0;
389       this.minor = 0;
390       this.major++;
391       this.inc('pre', identifier);
392       break;
393     case 'preminor':
394       this.prerelease.length = 0;
395       this.patch = 0;
396       this.minor++;
397       this.inc('pre', identifier);
398       break;
399     case 'prepatch':
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);
406       break;
407     // If the input is a non-prerelease version, this acts the same as
408     // prepatch.
409     case 'prerelease':
410       if (this.prerelease.length === 0)
411         this.inc('patch', identifier);
412       this.inc('pre', identifier);
413       break;
414
415     case 'major':
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)
421         this.major++;
422       this.minor = 0;
423       this.patch = 0;
424       this.prerelease = [];
425       break;
426     case 'minor':
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)
432         this.minor++;
433       this.patch = 0;
434       this.prerelease = [];
435       break;
436     case 'patch':
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)
442         this.patch++;
443       this.prerelease = [];
444       break;
445     // This probably shouldn't be used publicly.
446     // 1.0.0 "pre" would become 1.0.0-0 which is the wrong direction.
447     case 'pre':
448       if (this.prerelease.length === 0)
449         this.prerelease = [0];
450       else {
451         var i = this.prerelease.length;
452         while (--i >= 0) {
453           if (typeof this.prerelease[i] === 'number') {
454             this.prerelease[i]++;
455             i = -2;
456           }
457         }
458         if (i === -1) // didn't increment anything
459           this.prerelease.push(0);
460       }
461       if (identifier) {
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];
467         } else
468           this.prerelease = [identifier, 0];
469       }
470       break;
471
472     default:
473       throw new Error('invalid increment argument: ' + release);
474   }
475   this.format();
476   return this;
477 };
478
479 exports.inc = inc;
480 function inc(version, release, loose, identifier) {
481   if (typeof(loose) === 'string') {
482     identifier = loose;
483     loose = undefined;
484   }
485
486   try {
487     return new SemVer(version, loose).inc(release, identifier).version;
488   } catch (er) {
489     return null;
490   }
491 }
492
493 exports.diff = diff;
494 function diff(version1, version2) {
495   if (eq(version1, version2)) {
496     return null;
497   } else {
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]) {
504             return 'pre'+key;
505           }
506         }
507       }
508       return 'prerelease';
509     }
510     for (var key in v1) {
511       if (key === 'major' || key === 'minor' || key === 'patch') {
512         if (v1[key] !== v2[key]) {
513           return key;
514         }
515       }
516     }
517   }
518 }
519
520 exports.compareIdentifiers = compareIdentifiers;
521
522 var numeric = /^[0-9]+$/;
523 function compareIdentifiers(a, b) {
524   var anum = numeric.test(a);
525   var bnum = numeric.test(b);
526
527   if (anum && bnum) {
528     a = +a;
529     b = +b;
530   }
531
532   return (anum && !bnum) ? -1 :
533          (bnum && !anum) ? 1 :
534          a < b ? -1 :
535          a > b ? 1 :
536          0;
537 }
538
539 exports.rcompareIdentifiers = rcompareIdentifiers;
540 function rcompareIdentifiers(a, b) {
541   return compareIdentifiers(b, a);
542 }
543
544 exports.major = major;
545 function major(a, loose) {
546   return new SemVer(a, loose).major;
547 }
548
549 exports.minor = minor;
550 function minor(a, loose) {
551   return new SemVer(a, loose).minor;
552 }
553
554 exports.patch = patch;
555 function patch(a, loose) {
556   return new SemVer(a, loose).patch;
557 }
558
559 exports.compare = compare;
560 function compare(a, b, loose) {
561   return new SemVer(a, loose).compare(b);
562 }
563
564 exports.compareLoose = compareLoose;
565 function compareLoose(a, b) {
566   return compare(a, b, true);
567 }
568
569 exports.rcompare = rcompare;
570 function rcompare(a, b, loose) {
571   return compare(b, a, loose);
572 }
573
574 exports.sort = sort;
575 function sort(list, loose) {
576   return list.sort(function(a, b) {
577     return exports.compare(a, b, loose);
578   });
579 }
580
581 exports.rsort = rsort;
582 function rsort(list, loose) {
583   return list.sort(function(a, b) {
584     return exports.rcompare(a, b, loose);
585   });
586 }
587
588 exports.gt = gt;
589 function gt(a, b, loose) {
590   return compare(a, b, loose) > 0;
591 }
592
593 exports.lt = lt;
594 function lt(a, b, loose) {
595   return compare(a, b, loose) < 0;
596 }
597
598 exports.eq = eq;
599 function eq(a, b, loose) {
600   return compare(a, b, loose) === 0;
601 }
602
603 exports.neq = neq;
604 function neq(a, b, loose) {
605   return compare(a, b, loose) !== 0;
606 }
607
608 exports.gte = gte;
609 function gte(a, b, loose) {
610   return compare(a, b, loose) >= 0;
611 }
612
613 exports.lte = lte;
614 function lte(a, b, loose) {
615   return compare(a, b, loose) <= 0;
616 }
617
618 exports.cmp = cmp;
619 function cmp(a, op, b, loose) {
620   var ret;
621   switch (op) {
622     case '===':
623       if (typeof a === 'object') a = a.version;
624       if (typeof b === 'object') b = b.version;
625       ret = a === b;
626       break;
627     case '!==':
628       if (typeof a === 'object') a = a.version;
629       if (typeof b === 'object') b = b.version;
630       ret = a !== b;
631       break;
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);
639   }
640   return ret;
641 }
642
643 exports.Comparator = Comparator;
644 function Comparator(comp, loose) {
645   if (comp instanceof Comparator) {
646     if (comp.loose === loose)
647       return comp;
648     else
649       comp = comp.value;
650   }
651
652   if (!(this instanceof Comparator))
653     return new Comparator(comp, loose);
654
655   ;
656   this.loose = loose;
657   this.parse(comp);
658
659   if (this.semver === ANY)
660     this.value = '';
661   else
662     this.value = this.operator + this.semver.version;
663
664   ;
665 }
666
667 var ANY = {};
668 Comparator.prototype.parse = function(comp) {
669   var r = this.loose ? re[COMPARATORLOOSE] : re[COMPARATOR];
670   var m = comp.match(r);
671
672   if (!m)
673     throw new TypeError('Invalid comparator: ' + comp);
674
675   this.operator = m[1];
676   if (this.operator === '=')
677     this.operator = '';
678
679   // if it literally is just '>' or '' then allow anything.
680   if (!m[2])
681     this.semver = ANY;
682   else
683     this.semver = new SemVer(m[2], this.loose);
684 };
685
686 Comparator.prototype.inspect = function() {
687   return '<SemVer Comparator "' + this + '">';
688 };
689
690 Comparator.prototype.toString = function() {
691   return this.value;
692 };
693
694 Comparator.prototype.test = function(version) {
695   ;
696
697   if (this.semver === ANY)
698     return true;
699
700   if (typeof version === 'string')
701     version = new SemVer(version, this.loose);
702
703   return cmp(version, this.operator, this.semver, this.loose);
704 };
705
706
707 exports.Range = Range;
708 function Range(range, loose) {
709   if ((range instanceof Range) && range.loose === loose)
710     return range;
711
712   if (!(this instanceof Range))
713     return new Range(range, loose);
714
715   this.loose = loose;
716
717   // First, split based on boolean or ||
718   this.raw = range;
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
723     return c.length;
724   });
725
726   if (!this.set.length) {
727     throw new TypeError('Invalid SemVer Range: ' + range);
728   }
729
730   this.format();
731 }
732
733 Range.prototype.inspect = function() {
734   return '<SemVer Range "' + this.range + '">';
735 };
736
737 Range.prototype.format = function() {
738   this.range = this.set.map(function(comps) {
739     return comps.join(' ').trim();
740   }).join('||').trim();
741   return this.range;
742 };
743
744 Range.prototype.toString = function() {
745   return this.range;
746 };
747
748 Range.prototype.parseRange = function(range) {
749   var loose = this.loose;
750   range = range.trim();
751   ;
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);
755   ;
756   // `> 1.2.3 < 1.2.5` => `>1.2.3 <1.2.5`
757   range = range.replace(re[COMPARATORTRIM], comparatorTrimReplace);
758   ;
759
760   // `~ 1.2.3` => `~1.2.3`
761   range = range.replace(re[TILDETRIM], tildeTrimReplace);
762
763   // `^ 1.2.3` => `^1.2.3`
764   range = range.replace(re[CARETTRIM], caretTrimReplace);
765
766   // normalize spaces
767   range = range.split(/\s+/).join(' ');
768
769   // At this point, the range is completely trimmed and
770   // ready to be split into comparators.
771
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+/);
776   if (this.loose) {
777     // in loose mode, throw out any that are not valid comparators
778     set = set.filter(function(comp) {
779       return !!comp.match(compRe);
780     });
781   }
782   set = set.map(function(comp) {
783     return new Comparator(comp, loose);
784   });
785
786   return set;
787 };
788
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) {
794       return c.value;
795     }).join(' ').trim().split(' ');
796   });
797 }
798
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) {
803   ;
804   comp = replaceCarets(comp, loose);
805   ;
806   comp = replaceTildes(comp, loose);
807   ;
808   comp = replaceXRanges(comp, loose);
809   ;
810   comp = replaceStars(comp, loose);
811   ;
812   return comp;
813 }
814
815 function isX(id) {
816   return !id || id.toLowerCase() === 'x' || id === '*';
817 }
818
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);
828   }).join(' ');
829 }
830
831 function replaceTilde(comp, loose) {
832   var r = loose ? re[TILDELOOSE] : re[TILDE];
833   return comp.replace(r, function(_, M, m, p, pr) {
834     ;
835     var ret;
836
837     if (isX(M))
838       ret = '';
839     else if (isX(m))
840       ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
841     else if (isX(p))
842       // ~1.2 == >=1.2.0- <1.3.0-
843       ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
844     else if (pr) {
845       ;
846       if (pr.charAt(0) !== '-')
847         pr = '-' + pr;
848       ret = '>=' + M + '.' + m + '.' + p + pr +
849             ' <' + M + '.' + (+m + 1) + '.0';
850     } else
851       // ~1.2.3 == >=1.2.3 <1.3.0
852       ret = '>=' + M + '.' + m + '.' + p +
853             ' <' + M + '.' + (+m + 1) + '.0';
854
855     ;
856     return ret;
857   });
858 }
859
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);
869   }).join(' ');
870 }
871
872 function replaceCaret(comp, loose) {
873   ;
874   var r = loose ? re[CARETLOOSE] : re[CARET];
875   return comp.replace(r, function(_, M, m, p, pr) {
876     ;
877     var ret;
878
879     if (isX(M))
880       ret = '';
881     else if (isX(m))
882       ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
883     else if (isX(p)) {
884       if (M === '0')
885         ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
886       else
887         ret = '>=' + M + '.' + m + '.0 <' + (+M + 1) + '.0.0';
888     } else if (pr) {
889       ;
890       if (pr.charAt(0) !== '-')
891         pr = '-' + pr;
892       if (M === '0') {
893         if (m === '0')
894           ret = '>=' + M + '.' + m + '.' + p + pr +
895                 ' <' + M + '.' + m + '.' + (+p + 1);
896         else
897           ret = '>=' + M + '.' + m + '.' + p + pr +
898                 ' <' + M + '.' + (+m + 1) + '.0';
899       } else
900         ret = '>=' + M + '.' + m + '.' + p + pr +
901               ' <' + (+M + 1) + '.0.0';
902     } else {
903       ;
904       if (M === '0') {
905         if (m === '0')
906           ret = '>=' + M + '.' + m + '.' + p +
907                 ' <' + M + '.' + m + '.' + (+p + 1);
908         else
909           ret = '>=' + M + '.' + m + '.' + p +
910                 ' <' + M + '.' + (+m + 1) + '.0';
911       } else
912         ret = '>=' + M + '.' + m + '.' + p +
913               ' <' + (+M + 1) + '.0.0';
914     }
915
916     ;
917     return ret;
918   });
919 }
920
921 function replaceXRanges(comp, loose) {
922   ;
923   return comp.split(/\s+/).map(function(comp) {
924     return replaceXRange(comp, loose);
925   }).join(' ');
926 }
927
928 function replaceXRange(comp, loose) {
929   comp = comp.trim();
930   var r = loose ? re[XRANGELOOSE] : re[XRANGE];
931   return comp.replace(r, function(ret, gtlt, M, m, p, pr) {
932     ;
933     var xM = isX(M);
934     var xm = xM || isX(m);
935     var xp = xm || isX(p);
936     var anyX = xp;
937
938     if (gtlt === '=' && anyX)
939       gtlt = '';
940
941     if (xM) {
942       if (gtlt === '>' || gtlt === '<') {
943         // nothing is allowed
944         ret = '<0.0.0';
945       } else {
946         // nothing is forbidden
947         ret = '*';
948       }
949     } else if (gtlt && anyX) {
950       // replace X with 0
951       if (xm)
952         m = 0;
953       if (xp)
954         p = 0;
955
956       if (gtlt === '>') {
957         // >1 => >=2.0.0
958         // >1.2 => >=1.3.0
959         // >1.2.3 => >= 1.2.4
960         gtlt = '>=';
961         if (xm) {
962           M = +M + 1;
963           m = 0;
964           p = 0;
965         } else if (xp) {
966           m = +m + 1;
967           p = 0;
968         }
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.
972         gtlt = '<'
973         if (xm)
974           M = +M + 1
975         else
976           m = +m + 1
977       }
978
979       ret = gtlt + M + '.' + m + '.' + p;
980     } else if (xm) {
981       ret = '>=' + M + '.0.0 <' + (+M + 1) + '.0.0';
982     } else if (xp) {
983       ret = '>=' + M + '.' + m + '.0 <' + M + '.' + (+m + 1) + '.0';
984     }
985
986     ;
987
988     return ret;
989   });
990 }
991
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) {
995   ;
996   // Looseness is ignored here.  star is always as loose as it gets!
997   return comp.trim().replace(re[STAR], '');
998 }
999
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) {
1008
1009   if (isX(fM))
1010     from = '';
1011   else if (isX(fm))
1012     from = '>=' + fM + '.0.0';
1013   else if (isX(fp))
1014     from = '>=' + fM + '.' + fm + '.0';
1015   else
1016     from = '>=' + from;
1017
1018   if (isX(tM))
1019     to = '';
1020   else if (isX(tm))
1021     to = '<' + (+tM + 1) + '.0.0';
1022   else if (isX(tp))
1023     to = '<' + tM + '.' + (+tm + 1) + '.0';
1024   else if (tpr)
1025     to = '<=' + tM + '.' + tm + '.' + tp + '-' + tpr;
1026   else
1027     to = '<=' + to;
1028
1029   return (from + ' ' + to).trim();
1030 }
1031
1032
1033 // if ANY of the sets match ALL of its comparators, then pass
1034 Range.prototype.test = function(version) {
1035   if (!version)
1036     return false;
1037
1038   if (typeof version === 'string')
1039     version = new SemVer(version, this.loose);
1040
1041   for (var i = 0; i < this.set.length; i++) {
1042     if (testSet(this.set[i], version))
1043       return true;
1044   }
1045   return false;
1046 };
1047
1048 function testSet(set, version) {
1049   for (var i = 0; i < set.length; i++) {
1050     if (!set[i].test(version))
1051       return false;
1052   }
1053
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++) {
1061       ;
1062       if (set[i].semver === ANY)
1063         continue;
1064
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)
1070           return true;
1071       }
1072     }
1073
1074     // Version has a -pre, but it's not one of the ones we like.
1075     return false;
1076   }
1077
1078   return true;
1079 }
1080
1081 exports.satisfies = satisfies;
1082 function satisfies(version, range, loose) {
1083   try {
1084     range = new Range(range, loose);
1085   } catch (er) {
1086     return false;
1087   }
1088   return range.test(version);
1089 }
1090
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);
1097   })[0] || null;
1098 }
1099
1100 exports.validRange = validRange;
1101 function validRange(range, loose) {
1102   try {
1103     // Return '*' instead of '' so that truthiness works.
1104     // This will throw if it's invalid anyway
1105     return new Range(range, loose).range || '*';
1106   } catch (er) {
1107     return null;
1108   }
1109 }
1110
1111 // Determine if version is less than all the versions possible in the range
1112 exports.ltr = ltr;
1113 function ltr(version, range, loose) {
1114   return outside(version, range, '<', loose);
1115 }
1116
1117 // Determine if version is greater than all the versions possible in the range.
1118 exports.gtr = gtr;
1119 function gtr(version, range, loose) {
1120   return outside(version, range, '>', loose);
1121 }
1122
1123 exports.outside = outside;
1124 function outside(version, range, hilo, loose) {
1125   version = new SemVer(version, loose);
1126   range = new Range(range, loose);
1127
1128   var gtfn, ltefn, ltfn, comp, ecomp;
1129   switch (hilo) {
1130     case '>':
1131       gtfn = gt;
1132       ltefn = lte;
1133       ltfn = lt;
1134       comp = '>';
1135       ecomp = '>=';
1136       break;
1137     case '<':
1138       gtfn = lt;
1139       ltefn = gte;
1140       ltfn = gt;
1141       comp = '<';
1142       ecomp = '<=';
1143       break;
1144     default:
1145       throw new TypeError('Must provide a hilo val of "<" or ">"');
1146   }
1147
1148   // If it satisifes the range it is not outside
1149   if (satisfies(version, range, loose)) {
1150     return false;
1151   }
1152
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.
1155
1156   for (var i = 0; i < range.set.length; ++i) {
1157     var comparators = range.set[i];
1158
1159     var high = null;
1160     var low = null;
1161
1162     comparators.forEach(function(comparator) {
1163       if (comparator.semver === ANY) {
1164         comparator = new Comparator('>=0.0.0')
1165       }
1166       high = high || comparator;
1167       low = low || comparator;
1168       if (gtfn(comparator.semver, high.semver, loose)) {
1169         high = comparator;
1170       } else if (ltfn(comparator.semver, low.semver, loose)) {
1171         low = comparator;
1172       }
1173     });
1174
1175     // If the edge version comparator has a operator then our version
1176     // isn't outside it
1177     if (high.operator === comp || high.operator === ecomp) {
1178       return false;
1179     }
1180
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)) {
1185       return false;
1186     } else if (low.operator === ecomp && ltfn(version, low.semver)) {
1187       return false;
1188     }
1189   }
1190   return true;
1191 }
1192
1193 // Use the define() function if we're in AMD land
1194 if (typeof define === 'function' && define.amd)
1195   define(exports);
1196
1197 })(
1198   typeof exports === 'object' ? exports :
1199   typeof define === 'function' && define.amd ? {} :
1200   semver = {}
1201 );