Write compareSemver(a, b), which orders two version strings by SemVer 2.0.0 precedence and returns -1 if a is lower, 0 if they have equal precedence, and 1 if a is higher.
A valid version is exactly MAJOR.MINOR.PATCH, optionally followed by -PRERELEASE, optionally followed by +BUILD, with nothing else around it (no v prefix, no whitespace):
MAJOR,MINORandPATCHare non-negative integers written in decimal digits with no leading zeros (0is fine,01is not). They can be larger thanNumber.MAX_SAFE_INTEGERand must still compare exactly.PRERELEASEandBUILDare each one or more dot-separated identifiers. An identifier is non-empty and uses only ASCII letters, digits and-.- A prerelease identifier made only of digits is numeric and must not have leading zeros. Build identifiers have no such rule.
Precedence:
- Compare
MAJOR, thenMINOR, thenPATCHnumerically. - When those are equal, a version without a prerelease is higher than one with a prerelease.
- Two prereleases compare identifier by identifier from left to right: numeric identifiers compare numerically; alphanumeric identifiers compare by ASCII order; a numeric identifier is lower than an alphanumeric one. If all shared identifiers are equal, the version with fewer identifiers is lower.
- Build metadata is ignored for precedence.
Errors: if a version is not valid, fail with { kind: "invalid_version", input } where input is the offending string. Validate a before b.