Skip to content

Commit

Permalink
Exact versions can be compatible with ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
Suneil Nyamathi committed Jul 19, 2017
1 parent aa829c5 commit afbe592
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
"lint": "eslint .",
"test": "jenkins-mocha tests/unit --recursive"
},
"version": "1.1.0"
"version": "1.1.1"
}
9 changes: 7 additions & 2 deletions semver-intersect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const semver = require('semver');
const regex = {
condition: /^([<=>]+)?/,
majorVersion: /\d+/,
minMax: /^>=([\d]+\.[\d]+\.[\d]+(?:-[\w.]+)?) <([\d]+\.[\d]+\.[\d]+)$/,
minMax: /^>=([\d]+\.[\d]+\.[\d]+(?:-[\w.]+)?) <=?([\d]+\.[\d]+\.[\d]+)$/,
version: /([\d]+\.[\d]+\.[\d]+(?:-[\w.]+)?)$/,
whitespace: /\s+/
};
Expand All @@ -14,6 +14,10 @@ function createShorthand (range) {
}

const [ min, max ] = match.slice(1);
if (min === max) {
// Exact range
return min;
}

// Special handling for major version 0
if (semver.major(min) === 0 && semver.major(max) === 0) {
Expand Down Expand Up @@ -72,7 +76,8 @@ function intersect (...ranges) {
// Exact version number specified, must be compatible with both bounds
if (condition === '=') {
ensureCompatible(range, lowerBound, upperBound);
lowerBound = upperBound = range;
lowerBound = '>=' + range;
upperBound = '<=' + range;
}

// New lower bound must be less than existing upper bound
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/semver-intersect.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ describe('intersect', () => {
const result = intersect('^4.0.0', '~4.3.0');
expect(result).to.equal('~4.3.0');
});
it('should return an exact version intersected with a range', () => {
const result = intersect('1.5.16', '^1.0.0');
expect(result).to.equal('1.5.16');
});
it('should simplify redundant ranges', () => {
const result = intersect('^4.0.0', '~4.3.89', '~4.3.24', '~4.3.63');
expect(result).to.equal('~4.3.89');
Expand Down

0 comments on commit afbe592

Please sign in to comment.