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