Skip to content

Commit efd559e

Browse files
committed
fix: return early in comparator#intersects
1 parent ed24de3 commit efd559e

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

classes/comparator.js

+27-26
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ class Comparator {
9090
return new Range(this.value, options).test(comp.semver)
9191
}
9292

93+
options = parseOptions(options)
94+
9395
// Special cases where nothing can possibly be lower
9496
if (options.includePrerelease &&
9597
(this.value === '<0.0.0-0' || comp.value === '<0.0.0-0')) {
@@ -100,32 +102,31 @@ class Comparator {
100102
return false
101103
}
102104

103-
const sameDirectionIncreasing =
104-
(this.operator === '>=' || this.operator === '>') &&
105-
(comp.operator === '>=' || comp.operator === '>')
106-
const sameDirectionDecreasing =
107-
(this.operator === '<=' || this.operator === '<') &&
108-
(comp.operator === '<=' || comp.operator === '<')
109-
const sameSemVer = this.semver.version === comp.semver.version
110-
const differentDirectionsInclusive =
111-
(this.operator === '>=' || this.operator === '<=') &&
112-
(comp.operator === '>=' || comp.operator === '<=')
113-
const oppositeDirectionsLessThan =
114-
cmp(this.semver, '<', comp.semver, options) &&
115-
(this.operator === '>=' || this.operator === '>') &&
116-
(comp.operator === '<=' || comp.operator === '<')
117-
const oppositeDirectionsGreaterThan =
118-
cmp(this.semver, '>', comp.semver, options) &&
119-
(this.operator === '<=' || this.operator === '<') &&
120-
(comp.operator === '>=' || comp.operator === '>')
121-
122-
return (
123-
sameDirectionIncreasing ||
124-
sameDirectionDecreasing ||
125-
(sameSemVer && differentDirectionsInclusive) ||
126-
oppositeDirectionsLessThan ||
127-
oppositeDirectionsGreaterThan
128-
)
105+
// Same direction increasing (> or >=)
106+
if (this.operator.startsWith('>') && comp.operator.startsWith('>')) {
107+
return true
108+
}
109+
// Same direction decreasing (< or <=)
110+
if (this.operator.startsWith('<') && comp.operator.startsWith('<')) {
111+
return true
112+
}
113+
// same SemVer and both sides are inclusive (<= or >=)
114+
if (
115+
(this.semver.version === comp.semver.version) &&
116+
this.operator.includes('=') && comp.operator.includes('=')) {
117+
return true
118+
}
119+
// opposite directions less than
120+
if (cmp(this.semver, '<', comp.semver, options) &&
121+
this.operator.startsWith('>') && comp.operator.startsWith('<')) {
122+
return true
123+
}
124+
// opposite directions greater than
125+
if (cmp(this.semver, '>', comp.semver, options) &&
126+
this.operator.startsWith('<') && comp.operator.startsWith('>')) {
127+
return true
128+
}
129+
return false
129130
}
130131
}
131132

0 commit comments

Comments
 (0)