node-semver
node-semver copied to clipboard
[FEATURE] add methods to update a range, including operator
What / Why
There doesn't seem to currently be a way to get the operator of a passed semver range. Adding something like semver.operator() to get the operator would be helpful.
Current Behavior
There does not seem to be a way to get an operator of a passed version easily.
Expected Behavior
Have an easy way to get the operator of the passed semver range.
I'm not sure exactly what you're looking for. Can you share some example code of how you might expect this to work and/or a description of what you'd use it for?
If you have a range like 1.x || >=2.0.3 <3, you can get the operators of all the comparators in the range like so:
const semver = require('semver')
const range = new semver.Range('1.x || >=2.0.3 <3')
for (const set of range.set) {
console.log('set:')
for (const comparator of set) {
console.log(comparator.operator, comparator.semver.version)
}
}
// outputs
/*
set:
>= 1.0.0
< 2.0.0
set:
>= 2.0.3
< 3.0.0
*/
Note that some comparators will have their .semver member set to an internal Symbol('ANY'), which is used for stars, empty strings, and >=0.0.0.
Reopening after some research into other issues. I think providing a supported way to do this without reaching into range.set[][].operator would be nice, similar to what was discussed for getting the bounds of a range in #323.
One area this would be useful is when getting a range and attempting to increase the range somehow. Maybe there is less use of an .operator() method if that existed?
Digging up #10 as an old issue asking for something like this. Some other prior art here:
- https://github.com/raineorshine/npm-check-updates/issues/581
Updated the title of the issue to increase its scope.