node-semver icon indicating copy to clipboard operation
node-semver copied to clipboard

[FEATURE] add methods to update a range, including operator

Open bnb opened this issue 5 years ago • 5 comments

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.

bnb avatar Jan 30 '20 14:01 bnb

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
*/

isaacs avatar Apr 14 '20 17:04 isaacs

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.

isaacs avatar Apr 18 '20 22:04 isaacs

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.

lukekarrys avatar Apr 10 '22 22:04 lukekarrys

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

lukekarrys avatar Oct 27 '22 17:10 lukekarrys

Updated the title of the issue to increase its scope.

lukekarrys avatar Oct 27 '22 17:10 lukekarrys