Skip to content

Commit

Permalink
fix: intersects with v0.0.0 and v0.0.0-0
Browse files Browse the repository at this point in the history
Also fix options not being passed through from semver.intersects
  • Loading branch information
wraithgar committed Apr 6, 2023
1 parent da08e01 commit c652c8a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 30 deletions.
10 changes: 10 additions & 0 deletions classes/comparator.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ class Comparator {
return new Range(this.value, options).test(comp.semver)
}

// Special cases where nothing can possibly be lower
if (options.includePrerelease &&
(this.value === '<0.0.0-0' || comp.value === '<0.0.0-0')) {
return false
}
if (!options.includePrerelease &&
(this.value.startsWith('<0.0.0') || comp.value.startsWith('<0.0.0'))) {
return false
}

const sameDirectionIncreasing =
(this.operator === '>=' || this.operator === '>') &&
(comp.operator === '>=' || comp.operator === '>')
Expand Down
2 changes: 1 addition & 1 deletion ranges/intersects.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ const Range = require('../classes/range')
const intersects = (r1, r2, options) => {
r1 = new Range(r1, options)
r2 = new Range(r2, options)
return r1.intersects(r2)
return r1.intersects(r2, options)
}
module.exports = intersects
23 changes: 12 additions & 11 deletions test/classes/comparator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ test('tostrings', (t) => {

test('intersect comparators', (t) => {
t.plan(comparatorIntersection.length)
comparatorIntersection.forEach(([c0, c1, expect]) => t.test(`${c0} ${c1} ${expect}`, t => {
const comp0 = new Comparator(c0)
const comp1 = new Comparator(c1)

t.equal(comp0.intersects(comp1, false), expect,
`${c0} intersects ${c1}`)

t.equal(comp1.intersects(comp0, { loose: false }), expect,
`${c1} intersects ${c0}`)
t.end()
}))
comparatorIntersection.forEach(([c0, c1, expect, includePrerelease]) =>
t.test(`${c0} ${c1} ${expect}`, t => {
const comp0 = new Comparator(c0)
const comp1 = new Comparator(c1)

t.equal(comp0.intersects(comp1, { includePrerelease }), expect,
`${c0} intersects ${c1}`)

t.equal(comp1.intersects(comp0, { includePrerelease }), expect,
`${c1} intersects ${c0}`)
t.end()
}))
})

test('intersect demands another comparator', t => {
Expand Down
8 changes: 7 additions & 1 deletion test/fixtures/comparator-intersection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// c0, c1, expected intersection
// c0, c1, expected intersection, includePrerelease
module.exports = [
// One is a Version
['1.3.0', '>=1.3.0', true],
Expand Down Expand Up @@ -33,4 +33,10 @@ module.exports = [
['', '', true],
['', '>1.0.0', true],
['<=2.0.0', '', true],
['<0.0.0', '<0.1.0', false],
['<0.1.0', '<0.0.0', false],
['<0.0.0-0', '<0.1.0', false],
['<0.1.0', '<0.0.0-0', false],
['<0.0.0-0', '<0.1.0', false, true],
['<0.1.0', '<0.0.0-0', false, true],
]
34 changes: 17 additions & 17 deletions test/ranges/intersects.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ const rangeIntersection = require('../fixtures/range-intersection.js')

test('intersect comparators', t => {
t.plan(comparatorIntersection.length)
comparatorIntersection.forEach(([c0, c1, expect]) => t.test(`${c0} ${c1} ${expect}`, t => {
const comp0 = new Comparator(c0)
const comp1 = new Comparator(c1)
comparatorIntersection.forEach(([c0, c1, expect, includePrerelease]) =>
t.test(`${c0} ${c1} ${expect}`, t => {
const opts = { loose: false, includePrerelease }
const comp0 = new Comparator(c0)
const comp1 = new Comparator(c1)

t.equal(intersects(comp0, comp1), expect, `${c0} intersects ${c1} objects`)
t.equal(intersects(comp1, comp0), expect, `${c1} intersects ${c0} objects`)
t.equal(intersects(comp0, comp1, true), expect,
`${c0} intersects ${c1} loose, objects`)
t.equal(intersects(comp1, comp0, true), expect,
`${c1} intersects ${c0} loose, objects`)
t.equal(intersects(c0, c1), expect, `${c0} intersects ${c1}`)
t.equal(intersects(c1, c0), expect, `${c1} intersects ${c0}`)
t.equal(intersects(c0, c1, true), expect,
`${c0} intersects ${c1} loose`)
t.equal(intersects(c1, c0, true), expect,
`${c1} intersects ${c0} loose`)
t.end()
}))
t.equal(intersects(comp0, comp1, opts), expect, `${c0} intersects ${c1} objects`)
t.equal(intersects(comp1, comp0, opts), expect, `${c1} intersects ${c0} objects`)
t.equal(intersects(c0, c1, opts), expect, `${c0} intersects ${c1}`)
t.equal(intersects(c1, c0, opts), expect, `${c1} intersects ${c0}`)

opts.loose = true
t.equal(intersects(comp0, comp1, opts), expect, `${c0} intersects ${c1} loose, objects`)
t.equal(intersects(comp1, comp0, opts), expect, `${c1} intersects ${c0} loose, objects`)
t.equal(intersects(c0, c1, opts), expect, `${c0} intersects ${c1} loose`)
t.equal(intersects(c1, c0, opts), expect, `${c1} intersects ${c0} loose`)
t.end()
}))
})

test('ranges intersect', (t) => {
Expand Down

0 comments on commit c652c8a

Please # to comment.