Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

Markdown · 1.5 KiB · at 503e839

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, MINOR and PATCH are non-negative integers written in decimal digits with no leading zeros (0 is fine, 01 is not). They can be larger than Number.MAX_SAFE_INTEGER and must still compare exactly.
  • PRERELEASE and BUILD are 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, then MINOR, then PATCH numerically.
  • 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.