Debian Codegraph @

js/extras/semver-mod.js

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