Skip to content

Reduce unnecessary Comparators in Range set #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion classes/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ class Range {
throw new TypeError(`Invalid SemVer Range: ${range}`)
}

// if we have any that are not the null set, throw out null sets.
if (this.set.length > 1) {
// keep the first one, in case they're all null sets
const first = this.set[0]
this.set = this.set.filter(c => !isNullSet(c[0]))
if (this.set.length === 0)
this.set = [first]
else if (this.set.length > 1) {
// if we have any that are *, then the range is just *
for (const c of this.set) {
if (c.length === 1 && isAny(c[0])) {
this.set = [c]
break
}
}
}
}

this.format()
}

Expand Down Expand Up @@ -87,15 +105,31 @@ class Range {
// ready to be split into comparators.

const compRe = loose ? re[t.COMPARATORLOOSE] : re[t.COMPARATOR]
return range
const rangeList = range
.split(' ')
.map(comp => parseComparator(comp, this.options))
.join(' ')
.split(/\s+/)
// >=0.0.0 is equivalent to *
.map(comp => replaceGTE0(comp, this.options))
// in loose mode, throw out any that are not valid comparators
.filter(this.options.loose ? comp => !!comp.match(compRe) : () => true)
.map(comp => new Comparator(comp, this.options))

// if any comparators are the null set, then replace with JUST null set
// if more than one comparator, remove any * comparators
// also, don't include the same comparator more than once
const l = rangeList.length
const rangeMap = new Map()
for (const comp of rangeList) {
if (isNullSet(comp))
return [comp]
rangeMap.set(comp.value, comp)
}
if (rangeMap.size > 1 && rangeMap.has(''))
rangeMap.delete('')

return [...rangeMap.values()]
}

intersects (range, options) {
Expand Down Expand Up @@ -155,6 +189,9 @@ const {
caretTrimReplace
} = require('../internal/re')

const isNullSet = c => c.value === '<0.0.0-0'
const isAny = c => c.value === ''

// take a set of comparators and determine whether there
// exists a version which can satisfy it
const isSatisfiable = (comparators, options) => {
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/range-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = [
['>=0.2.3 || <0.0.1', '>=0.2.3||<0.0.1'],
['>=0.2.3 || <0.0.1', '>=0.2.3||<0.0.1'],
['>=0.2.3 || <0.0.1', '>=0.2.3||<0.0.1'],
['||', '||'],
['||', '*'],
['2.x.x', '>=2.0.0 <3.0.0-0'],
['1.2.x', '>=1.2.0 <1.3.0-0'],
['1.2.x || 2.x', '>=1.2.0 <1.3.0-0||>=2.0.0 <3.0.0-0'],
Expand Down Expand Up @@ -79,12 +79,14 @@ module.exports = [
['>01.02.03', null],
['~1.2.3beta', '>=1.2.3-beta <1.3.0-0', { loose: true }],
['~1.2.3beta', null],
['^ 1.2 ^ 1', '>=1.2.0 <2.0.0-0 >=1.0.0 <2.0.0-0'],
['^ 1.2 ^ 1', '>=1.2.0 <2.0.0-0 >=1.0.0'],
['1.2 - 3.4.5', '>=1.2.0 <=3.4.5'],
['1.2.3 - 3.4', '>=1.2.3 <3.5.0-0'],
['1.2 - 3.4', '>=1.2.0 <3.5.0-0'],
['>1', '>=2.0.0'],
['>1.2', '>=1.3.0'],
['>X', '<0.0.0-0'],
['<X', '<0.0.0-0'],
['<x <* || >* 2.x', '<0.0.0-0'],
['>x 2.x || * || <x', '*'],
]
8 changes: 6 additions & 2 deletions test/ranges/to-comparators.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test('comparators test', (t) => {
['>=0.2.3 || <0.0.1', [['>=0.2.3'], ['<0.0.1']]],
['>=0.2.3 || <0.0.1', [['>=0.2.3'], ['<0.0.1']]],
['>=0.2.3 || <0.0.1', [['>=0.2.3'], ['<0.0.1']]],
['||', [[''], ['']]],
['||', [['']]],
['2.x.x', [['>=2.0.0', '<3.0.0-0']]],
['1.2.x', [['>=1.2.0', '<1.3.0-0']]],
['1.2.x || 2.x', [['>=1.2.0', '<1.3.0-0'], ['>=2.0.0', '<3.0.0-0']]],
Expand Down Expand Up @@ -72,7 +72,11 @@ test('comparators test', (t) => {
['1.2.3 - 3.4', [['>=1.2.3', '<3.5.0-0']]],
['1.2.3 - 3', [['>=1.2.3', '<4.0.0-0']]],
['>*', [['<0.0.0-0']]],
['<*', [['<0.0.0-0']]]
['<*', [['<0.0.0-0']]],
['>X', [['<0.0.0-0']]],
['<X', [['<0.0.0-0']]],
['<x <* || >* 2.x', [['<0.0.0-0']]],
['>x 2.x || * || <x', [['']]],
].forEach(([pre, wanted]) => {
const found = toComparators(pre)
const jw = JSON.stringify(wanted)
Expand Down