node-semver
node-semver copied to clipboard
[FEATURE] Expose regexes
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?
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])/
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";
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.