1 const ANY = Symbol('SemVer ANY')
2 // hoisted class for cyclic dependency
7 constructor (comp, options) {
8 if (!options || typeof options !== 'object') {
11 includePrerelease: false
15 if (comp instanceof Comparator) {
16 if (comp.loose === !!options.loose) {
23 debug('comparator', comp, options)
24 this.options = options
25 this.loose = !!options.loose
28 if (this.semver === ANY) {
31 this.value = this.operator + this.semver.version
38 const r = this.options.loose ? re[t.COMPARATORLOOSE] : re[t.COMPARATOR]
39 const m = comp.match(r)
42 throw new TypeError(`Invalid comparator: ${comp}`)
45 this.operator = m[1] !== undefined ? m[1] : ''
46 if (this.operator === '=') {
50 // if it literally is just '>' or '' then allow anything.
54 this.semver = new SemVer(m[2], this.options.loose)
63 debug('Comparator.test', version, this.options.loose)
65 if (this.semver === ANY || version === ANY) {
69 if (typeof version === 'string') {
71 version = new SemVer(version, this.options)
77 return cmp(version, this.operator, this.semver, this.options)
80 intersects (comp, options) {
81 if (!(comp instanceof Comparator)) {
82 throw new TypeError('a Comparator is required')
85 if (!options || typeof options !== 'object') {
88 includePrerelease: false
92 if (this.operator === '') {
93 if (this.value === '') {
96 return new Range(comp.value, options).test(this.value)
97 } else if (comp.operator === '') {
98 if (comp.value === '') {
101 return new Range(this.value, options).test(comp.semver)
104 const sameDirectionIncreasing =
105 (this.operator === '>=' || this.operator === '>') &&
106 (comp.operator === '>=' || comp.operator === '>')
107 const sameDirectionDecreasing =
108 (this.operator === '<=' || this.operator === '<') &&
109 (comp.operator === '<=' || comp.operator === '<')
110 const sameSemVer = this.semver.version === comp.semver.version
111 const differentDirectionsInclusive =
112 (this.operator === '>=' || this.operator === '<=') &&
113 (comp.operator === '>=' || comp.operator === '<=')
114 const oppositeDirectionsLessThan =
115 cmp(this.semver, '<', comp.semver, options) &&
116 (this.operator === '>=' || this.operator === '>') &&
117 (comp.operator === '<=' || comp.operator === '<')
118 const oppositeDirectionsGreaterThan =
119 cmp(this.semver, '>', comp.semver, options) &&
120 (this.operator === '<=' || this.operator === '<') &&
121 (comp.operator === '>=' || comp.operator === '>')
124 sameDirectionIncreasing ||
125 sameDirectionDecreasing ||
126 (sameSemVer && differentDirectionsInclusive) ||
127 oppositeDirectionsLessThan ||
128 oppositeDirectionsGreaterThan
133 module.exports = Comparator
135 const {re, t} = require('../internal/re')
136 const cmp = require('../functions/cmp')
137 const debug = require('../internal/debug')
138 const SemVer = require('./semver')
139 const Range = require('./range')