-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Need this for a case where we programmatically generate a list of all the versions subject to a security vulnerability by virtue of depending exclusively on vulnerable versions of a given dependency. While it's not a perfect heuristic, with this method on semver, instead of printing out something long and confusing like this: 8.0.1 || 8.0.2 || 9.0.0 || 9.0.1 || 10.0.0-alpha.0 || 10.0.0-alpha.1 || 10.0.0-alpha.2 || 10.0.0-alpha.3 || 10.0.0-alpha.4 || 10.0.0 || 10.0.1 || 10.0.2 || 10.0.3 || 10.1.0 || 10.1.1 || 10.1.2 || 11.0.0 || 11.1.0 || 12.0.0-candidate.0 || 12.0.0 || 12.0.1 we can show this: 8.0.1 - 11.1.0 || 12.0.0-candidate.0 - 12.0.1
- Loading branch information
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// given a set of versions and a range, create a "simplified" range | ||
// that includes the same versions that the original range does | ||
// If the original range is shorter than the simplified one, return that. | ||
const satisfies = require('../functions/satisfies.js') | ||
const compare = require('../functions/compare.js') | ||
module.exports = (versions, range, options) => { | ||
const set = [] | ||
let min = null | ||
let prev = null | ||
const v = versions.sort((a, b) => compare(a, b, options)) | ||
for (const version of v) { | ||
const included = satisfies(version, range, options) | ||
if (included) { | ||
prev = version | ||
if (!min) | ||
min = version | ||
} else { | ||
if (prev) { | ||
set.push([min, prev]) | ||
} | ||
prev = null | ||
min = null | ||
} | ||
} | ||
if (min) | ||
set.push([min, null]) | ||
|
||
const ranges = [] | ||
for (const [min, max] of set) { | ||
if (min === max) | ||
ranges.push(min) | ||
else if (!max && min === v[0]) | ||
ranges.push('*') | ||
else if (!max) | ||
ranges.push(`>=${min}`) | ||
else if (min === v[0]) | ||
ranges.push(`<=${max}`) | ||
else | ||
ranges.push(`${min} - ${max}`) | ||
} | ||
const simplified = ranges.join(' || ') | ||
const original = typeof range.raw === 'string' ? range.raw : String(range) | ||
return simplified.length < original.length ? simplified : range | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const simplify = require('../../ranges/simplify.js') | ||
const Range = require('../../classes/range.js') | ||
const t = require('tap') | ||
const versions = [ | ||
'1.0.0', | ||
'1.0.1', | ||
'1.0.2', | ||
'1.0.3', | ||
'1.0.4', | ||
'1.1.0', | ||
'1.1.1', | ||
'1.1.2', | ||
'1.2.0', | ||
'1.2.1', | ||
'1.2.2', | ||
'1.2.3', | ||
'1.2.4', | ||
'1.2.5', | ||
'2.0.0', | ||
'2.0.1', | ||
'2.1.0', | ||
'2.1.1', | ||
'2.1.2', | ||
'2.2.0', | ||
'2.2.1', | ||
'2.2.2', | ||
'2.3.0', | ||
'2.3.1', | ||
'2.4.0', | ||
'3.0.0', | ||
'3.1.0', | ||
'3.2.0', | ||
'3.3.0', | ||
] | ||
|
||
t.equal(simplify(versions, '1.x'), '1.x') | ||
t.equal(simplify(versions, '1.0.0 || 1.0.1 || 1.0.2 || 1.0.3 || 1.0.4'), '<=1.0.4') | ||
t.equal(simplify(versions, new Range('1.0.0 || 1.0.1 || 1.0.2 || 1.0.3 || 1.0.4')), '<=1.0.4') | ||
t.equal(simplify(versions, '>=3.0.0 <3.1.0'), '3.0.0') | ||
t.equal(simplify(versions, '3.0.0 || 3.1 || 3.2 || 3.3'), '>=3.0.0') | ||
t.equal(simplify(versions, '1 || 2 || 3'), '*') | ||
t.equal(simplify(versions, '2.1 || 2.2 || 2.3'), '2.1.0 - 2.3.1') |