@storybook/semver
Version:
The semantic version parser used by npm for browser.
173 lines (130 loc) • 5.66 kB
JavaScript
"use strict";
require("core-js/modules/es.symbol");
require("core-js/modules/es.symbol.description");
require("core-js/modules/es.symbol.iterator");
require("core-js/modules/es.array.iterator");
require("core-js/modules/es.object.define-property");
require("core-js/modules/es.object.to-string");
require("core-js/modules/es.regexp.exec");
require("core-js/modules/es.string.iterator");
require("core-js/modules/es.string.match");
require("core-js/modules/web.dom-collections.iterator");
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var ANY = Symbol('SemVer ANY'); // hoisted class for cyclic dependency
var Comparator = /*#__PURE__*/function () {
_createClass(Comparator, null, [{
key: "ANY",
get: function get() {
return ANY;
}
}]);
function Comparator(comp, options) {
_classCallCheck(this, Comparator);
if (!options || _typeof(options) !== 'object') {
options = {
loose: !!options,
includePrerelease: false
};
}
if (comp instanceof Comparator) {
if (comp.loose === !!options.loose) {
return comp;
} else {
comp = comp.value;
}
}
debug('comparator', comp, options);
this.options = options;
this.loose = !!options.loose;
this.parse(comp);
if (this.semver === ANY) {
this.value = '';
} else {
this.value = this.operator + this.semver.version;
}
debug('comp', this);
}
_createClass(Comparator, [{
key: "parse",
value: function parse(comp) {
var r = this.options.loose ? re[t.COMPARATORLOOSE] : re[t.COMPARATOR];
var m = comp.match(r);
if (!m) {
throw new TypeError("Invalid comparator: ".concat(comp));
}
this.operator = m[1] !== undefined ? m[1] : '';
if (this.operator === '=') {
this.operator = '';
} // if it literally is just '>' or '' then allow anything.
if (!m[2]) {
this.semver = ANY;
} else {
this.semver = new SemVer(m[2], this.options.loose);
}
}
}, {
key: "toString",
value: function toString() {
return this.value;
}
}, {
key: "test",
value: function test(version) {
debug('Comparator.test', version, this.options.loose);
if (this.semver === ANY || version === ANY) {
return true;
}
if (typeof version === 'string') {
try {
version = new SemVer(version, this.options);
} catch (er) {
return false;
}
}
return cmp(version, this.operator, this.semver, this.options);
}
}, {
key: "intersects",
value: function intersects(comp, options) {
if (!(comp instanceof Comparator)) {
throw new TypeError('a Comparator is required');
}
if (!options || _typeof(options) !== 'object') {
options = {
loose: !!options,
includePrerelease: false
};
}
if (this.operator === '') {
if (this.value === '') {
return true;
}
return new Range(comp.value, options).test(this.value);
} else if (comp.operator === '') {
if (comp.value === '') {
return true;
}
return new Range(this.value, options).test(comp.semver);
}
var sameDirectionIncreasing = (this.operator === '>=' || this.operator === '>') && (comp.operator === '>=' || comp.operator === '>');
var sameDirectionDecreasing = (this.operator === '<=' || this.operator === '<') && (comp.operator === '<=' || comp.operator === '<');
var sameSemVer = this.semver.version === comp.semver.version;
var differentDirectionsInclusive = (this.operator === '>=' || this.operator === '<=') && (comp.operator === '>=' || comp.operator === '<=');
var oppositeDirectionsLessThan = cmp(this.semver, '<', comp.semver, options) && (this.operator === '>=' || this.operator === '>') && (comp.operator === '<=' || comp.operator === '<');
var oppositeDirectionsGreaterThan = cmp(this.semver, '>', comp.semver, options) && (this.operator === '<=' || this.operator === '<') && (comp.operator === '>=' || comp.operator === '>');
return sameDirectionIncreasing || sameDirectionDecreasing || sameSemVer && differentDirectionsInclusive || oppositeDirectionsLessThan || oppositeDirectionsGreaterThan;
}
}]);
return Comparator;
}();
module.exports = Comparator;
var _require = require('../internal/re'),
re = _require.re,
t = _require.t;
var cmp = require('../functions/cmp');
var debug = require('../internal/debug');
var SemVer = require('./semver');
var Range = require('./range');