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

[FEATURE] Expose regexes

Open Zwyx opened this issue 3 years ago • 2 comments

Hi,

I wanted to inspect a string to find out if it contains a version number (for instance specification-2.3.1.json). To do that I used one of the regexes provided here.

I believe that it would be useful to have these regexes accessible from the node-semver package. It looks like they are defined in internal/re.js, so I'm thinking we could export them as constants. I would be happy to prepare a PR, depending on what the maintainers think of the idea?

Zwyx avatar Apr 29 '22 11:04 Zwyx

They are somewhat accessible now but you're correct that they are mostly for internal use. Could you provide a code example of how you'd like to use them? It's possible to do now but with some hoops to jump through:

const semver = require('semver')
semver.re[semver.tokens.COERCE]
> /(^|[^\d])(\d{1,16})(?:\.(\d{1,16}))?(?:\.(\d{1,16}))?(?:$|[^\d])/

lukekarrys avatar Jun 06 '22 18:06 lukekarrys

Hi Luke, thank you for your answer and sorry for my late reply. What you show is definitely in the same direction of what I was thinking, it'd just need to be more "official". Especially since I'm using TypeScript.

So maybe this regex could be exported and named coerceRegex for example, so we would just need to do this to be able to use it:

import { coerceRegex } from "semver";

Zwyx avatar Jun 25 '22 05:06 Zwyx

I think coupling to the internals is probably not the best idea. We can't remove the regex exports without a semver-breaking change otherwise I'd advocate to do so.

Ideally the way to answer these questions about a semver string is with the SemVer object itself or its exported functions. This is way less prone to shifting if those internals change.

wraithgar avatar Apr 10 '23 17:04 wraithgar