Skip to content

Commit

Permalink
Range intersect supports wildcards and ~
Browse files Browse the repository at this point in the history
- Added undefined check for range operator, wildcards defaults to empty.
- Comparator and Range test returns true if either version is a wildcard
- Ensure ranges intersect supports wildcards and ~

Fix #271
  • Loading branch information
Asthetic authored and isaacs committed May 22, 2019
1 parent 78ea02a commit 7b072bd
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 32 deletions.
14 changes: 8 additions & 6 deletions semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ Comparator.prototype.parse = function (comp) {
throw new TypeError('Invalid comparator: ' + comp)
}

this.operator = m[1]
this.operator = m[1] !== undefined ? m[1] : ''
if (this.operator === '=') {
this.operator = ''
}
Expand All @@ -805,7 +805,7 @@ Comparator.prototype.toString = function () {
Comparator.prototype.test = function (version) {
debug('Comparator.test', version, this.options.loose)

if (this.semver === ANY) {
if (this.semver === ANY || version === ANY) {
return true
}

Expand All @@ -831,9 +831,15 @@ Comparator.prototype.intersects = function (comp, options) {
var rangeTmp

if (this.operator === '') {
if (this.value === '') {
return true
}
rangeTmp = new Range(comp.value, options)
return satisfies(this.value, rangeTmp, options)
} else if (comp.operator === '') {
if (comp.value === '') {
return true
}
rangeTmp = new Range(this.value, options)
return satisfies(comp.semver, rangeTmp, options)
}
Expand Down Expand Up @@ -1250,10 +1256,6 @@ function hyphenReplace ($0,

// if ANY of the sets match ALL of its comparators, then pass
Range.prototype.test = function (version) {
if (!version) {
return false
}

if (typeof version === 'string') {
version = new SemVer(version, this.options)
}
Expand Down
94 changes: 68 additions & 26 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ test('comparison tests', function (t) {
['1.2.3-a.b', '1.2.3-a'],
['1.2.3-a.b.c.10.d.5', '1.2.3-a.b.c.5.d.100'],
['1.2.3-r2', '1.2.3-r100'],
['1.2.3-r100', '1.2.3-R2']
['1.2.3-r100', '1.2.3-R2'],
].forEach(function (v) {
var v0 = v[0]
var v1 = v[1]
Expand Down Expand Up @@ -333,7 +333,6 @@ test('negative range tests', function (t) {
['blerg', '1.2.3'],
['git+https://user:password0123@github.com/foo', '123.0.0', true],
['^1.2.3', '2.0.0-pre'],
['^1.2.3', false]
].forEach(function (v) {
var range = v[0]
var ver = v[1]
Expand Down Expand Up @@ -919,31 +918,74 @@ test('ranges intersect', function (t) {
['<1.6.16 || >=1.7.0 <1.7.11 || >=1.8.0 <1.8.2', '>=1.6.16 <1.7.0 || >=1.7.11 <1.8.0 || >=1.8.2', false],
['<=1.6.16 || >=1.7.0 <1.7.11 || >=1.8.0 <1.8.2', '>=1.6.16 <1.7.0 || >=1.7.11 <1.8.0 || >=1.8.2', true],
['>=1.0.0', '<=1.0.0', true],
['>1.0.0 <1.0.0', '<=0.0.0', false]
['>1.0.0 <1.0.0', '<=0.0.0', false],
['*', '0.0.1', true],
['*', '>=1.0.0', true],
['*', '>1.0.0', true],
['*', '~1.0.0', true],
['*', '<1.6.0', true],
['*', '<=1.6.0', true],
['1.*', '0.0.1', false],
['1.*', '2.0.0', false],
['1.*', '1.0.0', true],
['1.*', '<2.0.0', true],
['1.*', '>1.0.0', true],
['1.*', '<=1.0.0', true],
['1.*', '^1.0.0', true],
['1.0.*', '0.0.1', false],
['1.0.*', '<0.0.1', false],
['1.0.*', '>0.0.1', true],
['*', '1.3.0 || <1.0.0 >2.0.0', true],
['1.3.0 || <1.0.0 >2.0.0', '*', true],
['1.*', '1.3.0 || <1.0.0 >2.0.0', true],
['x', '0.0.1', true],
['x', '>=1.0.0', true],
['x', '>1.0.0', true],
['x', '~1.0.0', true],
['x', '<1.6.0', true],
['x', '<=1.6.0', true],
['1.x', '0.0.1', false],
['1.x', '2.0.0', false],
['1.x', '1.0.0', true],
['1.x', '<2.0.0', true],
['1.x', '>1.0.0', true],
['1.x', '<=1.0.0', true],
['1.x', '^1.0.0', true],
['1.0.x', '0.0.1', false],
['1.0.x', '<0.0.1', false],
['1.0.x', '>0.0.1', true],
['x', '1.3.0 || <1.0.0 >2.0.0', true],
['1.3.0 || <1.0.0 >2.0.0', 'x', true],
['1.x', '1.3.0 || <1.0.0 >2.0.0', true],
['*', '*', true],
['x', '', true],
].forEach(function (v) {
var range1 = new Range(v[0])
var range2 = new Range(v[1])
var expect = v[2]
var actual1 = range1.intersects(range2)
var actual2 = range2.intersects(range1)
var actual3 = semver.intersects(v[1], v[0])
var actual4 = semver.intersects(v[0], v[1])
var actual5 = semver.intersects(v[1], v[0], true)
var actual6 = semver.intersects(v[0], v[1], true)
var actual7 = semver.intersects(range1, range2)
var actual8 = semver.intersects(range2, range1)
var actual9 = semver.intersects(range1, range2, true)
var actual0 = semver.intersects(range2, range1, true)
t.equal(actual1, expect)
t.equal(actual2, expect)
t.equal(actual3, expect)
t.equal(actual4, expect)
t.equal(actual5, expect)
t.equal(actual6, expect)
t.equal(actual7, expect)
t.equal(actual8, expect)
t.equal(actual9, expect)
t.equal(actual0, expect)
t.test(v[0] + ' <~> ' + v[1], t => {
var range1 = new Range(v[0])
var range2 = new Range(v[1])
var expect = v[2]
var actual1 = range1.intersects(range2)
var actual2 = range2.intersects(range1)
var actual3 = semver.intersects(v[1], v[0])
var actual4 = semver.intersects(v[0], v[1])
var actual5 = semver.intersects(v[1], v[0], true)
var actual6 = semver.intersects(v[0], v[1], true)
var actual7 = semver.intersects(range1, range2)
var actual8 = semver.intersects(range2, range1)
var actual9 = semver.intersects(range1, range2, true)
var actual0 = semver.intersects(range2, range1, true)
t.equal(actual1, expect)
t.equal(actual2, expect)
t.equal(actual3, expect)
t.equal(actual4, expect)
t.equal(actual5, expect)
t.equal(actual6, expect)
t.equal(actual7, expect)
t.equal(actual8, expect)
t.equal(actual9, expect)
t.equal(actual0, expect)
t.end()
})
})
t.end()
})
Expand Down

0 comments on commit 7b072bd

Please sign in to comment.