semver API reference
Strict Semantic Versioning 2.0.0 (https://semver.org): parse, compare, increment, and range-match version numbers - the full surface a package registry or dependency resolver needs. A pure-Jennifer reference module (no Go, no system library): parsing uses the canonical SemVer regex (via the regex library); precedence comparison, sorting, and range matching are hand-written Jennifer.
Ranges follow the npm / Composer grammar: caret ^1.2.0, tilde ~1.2, comparators >=1.0.0 <2.0.0 (space or comma = AND), OR sets ^1 || ^2, hyphen ranges 1.2.3 - 2.3.4, x-ranges 1.x / 1.2.*, and * for any. A prerelease version satisfies a range only when a comparator in the same clause pins a prerelease at the same major.minor.patch (the npm rule).
Import with import "semver.j" as semver;. See the semver guide for prose and examples.
Contents
| Name | What it is |
|---|---|
clean | Normalise a version string: trim whitespace and a leading = / v, then return the canonical form if it i... |
coerce | Extract a version from a loose string - a git tag, a partial, or text with a version-like run - and return ... |
compare | Compare two versions by SemVer precedence: numeric core, then a prerelease ranks below its release, then pr... |
diff | The kind of change from a to b: "major", "minor", "patch", "prerelease" (only the prerelease tag differs), ... |
eq | Report whether a and b have equal precedence (build metadata ignored). |
gt | Report whether a orders after b. |
gte | Report whether a orders at or after b. |
gtr | Report whether a version is greater than every version a range allows (beyond its upper extreme). |
incMajor | Bump the major version, resetting minor / patch and clearing the tags. |
incMinor | Bump the minor version, resetting patch and clearing the tags. |
incPatch | Bump the patch version, clearing the tags. |
intersects | Report whether two ranges share at least one satisfying version, prereleases included (a prerelease overlap... |
isPrerelease | Report whether the version carries a prerelease tag. |
isStable | Report whether the version is stable: a released (major >= 1) version with no prerelease tag. |
isValid | Report whether a string is a valid SemVer 2.0.0 version. |
lt | Report whether a orders before b. |
lte | Report whether a orders at or before b. |
ltr | Report whether a version is less than every version a range allows (below its lower extreme). |
maxSatisfying | Pick the highest version from a list that satisfies the range. |
minSatisfying | Pick the lowest version from a list that satisfies the range. |
minVersion | The lowest version that could satisfy a range (its floor), or "" when the range is empty or invalid. |
neq | Report whether a and b differ in precedence. |
outside | Report whether a version is beyond a range's extremes (above it or below it). |
parse | Parse a version string into a Version. |
rsort | Return a new list ordered descending by SemVer precedence (highest first). |
satisfies | Report whether a concrete version satisfies a range. |
simplifyRange | Simplify a range against a known list of versions: return the shortest range that matches exactly the same ... |
sort | Return a new list ordered ascending by SemVer precedence. |
subset | Report whether every version allowed by inner is also allowed by outer (inner is the tighter / implied ... |
toString | Render a Version back to its canonical string form. |
validRange | Report whether a range expression is well-formed (parseable). |
Version | struct: A parsed SemVer version: numeric core plus optional prerelease / build tags. |
Functions
clean
semver.clean(s as string)Normalise a version string: trim whitespace and a leading = / v, then return the canonical form if it is a valid full version, else "". Strict (unlike coerce): clean("v1.2.3") -> "1.2.3", but clean("1.2") -> "".
s- the version text
- Returns
string - the canonical version, or "" if not valid
coerce
semver.coerce(s as string)Extract a version from a loose string - a git tag, a partial, or text with a version-like run - and return canonical major.minor.patch (missing parts are 0). Handles a leading v and surrounding noise. Returns "" when no numeric core is found. coerce("v1.2.3") -> "1.2.3", coerce("1.2") -> "1.2.0".
s- the loose text
- Returns
string - a canonical version, or "" if none was found
compare
semver.compare(a as Version, b as Version)Compare two versions by SemVer precedence: numeric core, then a prerelease ranks below its release, then prerelease fields. Build metadata is ignored.
a- the left version
b- the right version
- Returns
int - -1 if a < b, 0 if equal, 1 if a > b
diff
semver.diff(a as Version, b as Version)The kind of change from a to b: "major", "minor", "patch", "prerelease" (only the prerelease tag differs), or "" when the two are equal. The highest-order difference wins.
a- the left version
b- the right version
- Returns
string - the release-type difference
eq
semver.eq(a as Version, b as Version)Report whether a and b have equal precedence (build metadata ignored).
a- the left version
b- the right version
- Returns
bool - true when a and b compare equal
gt
semver.gt(a as Version, b as Version)Report whether a orders after b.
a- the left version
b- the right version
- Returns
bool - true when a > b by SemVer precedence
gte
semver.gte(a as Version, b as Version)Report whether a orders at or after b.
a- the left version
b- the right version
- Returns
bool - true when a >= b
gtr
semver.gtr(version as string, range as string)Report whether a version is greater than every version a range allows (beyond its upper extreme). False for an unbounded range or a version in an interior gap.
version- the version to test
range- the range expression
- Returns
bool - true when version is above the whole range
incMajor
semver.incMajor(v as Version)Bump the major version, resetting minor / patch and clearing the tags.
v- the starting version
- Returns
Version - a new version with major + 1 and minor = patch = 0
incMinor
semver.incMinor(v as Version)Bump the minor version, resetting patch and clearing the tags.
v- the starting version
- Returns
Version - a new version with minor + 1 and patch = 0
incPatch
semver.incPatch(v as Version)Bump the patch version, clearing the tags.
v- the starting version
- Returns
Version - a new version with patch + 1
intersects
semver.intersects(rangeA as string, rangeB as string)Report whether two ranges share at least one satisfying version, prereleases included (a prerelease overlap needs both ranges to pin the same major.minor.patch). ^1.2.0 intersects >=1.5.0 (true) but not ^2.0.0. Invalid ranges never intersect.
rangeA- the first range
rangeB- the second range
- Returns
bool - true when the ranges overlap
isPrerelease
semver.isPrerelease(v as Version)Report whether the version carries a prerelease tag.
v- the version to classify
- Returns
bool - true when a prerelease tag is present
isStable
semver.isStable(v as Version)Report whether the version is stable: a released (major >= 1) version with no prerelease tag. A 0.y.z version is unstable by SemVer convention.
v- the version to classify
- Returns
bool - true when major >= 1 and there is no prerelease tag
isValid
semver.isValid(s as string)Report whether a string is a valid SemVer 2.0.0 version.
s- the candidate version text
- Returns
bool - true exactly when
parse(s)succeeds
lt
semver.lt(a as Version, b as Version)Report whether a orders before b.
a- the left version
b- the right version
- Returns
bool - true when a < b by SemVer precedence
lte
semver.lte(a as Version, b as Version)Report whether a orders at or before b.
a- the left version
b- the right version
- Returns
bool - true when a <= b
ltr
semver.ltr(version as string, range as string)Report whether a version is less than every version a range allows (below its lower extreme).
version- the version to test
range- the range expression
- Returns
bool - true when version is below the whole range
maxSatisfying
semver.maxSatisfying(versions as list of string, range as string)Pick the highest version from a list that satisfies the range. Versions that are not valid SemVer are skipped. Returns "" when none match.
versions- the candidate versions
range- the range expression
- Returns
string - the highest satisfying version, or "" if none match
- Throws
Error - kind "value" when the range is not valid
minSatisfying
semver.minSatisfying(versions as list of string, range as string)Pick the lowest version from a list that satisfies the range. Versions that are not valid SemVer are skipped. Returns "" when none match.
versions- the candidate versions
range- the range expression
- Returns
string - the lowest satisfying version, or "" if none match
- Throws
Error - kind "value" when the range is not valid
minVersion
semver.minVersion(range as string)The lowest version that could satisfy a range (its floor), or "" when the range is empty or invalid. Prerelease-precise: minVersion(">=1.2.3-rc.1") is "1.2.3-rc.1", minVersion("^1.2.0") is "1.2.0", minVersion(">1.2.3") is "1.2.4".
range- the range expression
- Returns
string - the lowest satisfying version, or ""
neq
semver.neq(a as Version, b as Version)Report whether a and b differ in precedence.
a- the left version
b- the right version
- Returns
bool - true when a and b are not equal
outside
semver.outside(version as string, range as string)Report whether a version is beyond a range's extremes (above it or below it). A version in an interior gap of a multi-clause range is not "outside".
version- the version to test
range- the range expression
- Returns
bool - true when version is above or below the whole range
parse
semver.parse(s as string)Parse a version string into a Version.
s- the version text (e.g. "1.2.3-rc.1+build.5")
- Returns
Version - the parsed version
- Throws
Error - when s is not a valid SemVer 2.0.0 string
rsort
semver.rsort(vs as list of Version)Return a new list ordered descending by SemVer precedence (highest first).
vs- the versions to order
- Returns
list of Version - a new list sorted descending
satisfies
semver.satisfies(version as string, range as string)Report whether a concrete version satisfies a range. The range grammar is the npm / Composer set: caret ^1.2.0, tilde ~1.2, comparators >=1.0.0 <2.0.0 (space or comma = AND), OR sets ^1 || ^2, hyphen ranges 1.2.3 - 2.3.4, x-ranges 1.x / 1.2.*, and * / "" / "any" for any release. A prerelease version matches only when a comparator in the same clause pins a prerelease at the same major.minor.patch. An invalid version never satisfies anything; an invalid range (see validRange) throws.
version- the concrete version to test (e.g. "1.4.0")
range- the range expression
- Returns
bool - true when version satisfies range
- Throws
Error - kind "value" when the range is not valid
simplifyRange
semver.simplifyRange(versions as list of string, range as string)Simplify a range against a known list of versions: return the shortest range that matches exactly the same subset of versions. Runs of consecutive matching versions collapse to >=lo <=hi clauses joined by ||; the original range is kept when it is already at least as short. "*" when every listed version matches, <0.0.0-0 (matches nothing) when none do.
versions- the known versions
range- the range to simplify
- Returns
string - the simplified range
- Throws
Error - kind "value" when the range is not valid
sort
semver.sort(vs as list of Version)Return a new list ordered ascending by SemVer precedence. lists.sort is scalar-only, so this is a merge sort over compare() - O(n log n), where an insertion sort was O(n^2).
vs- the versions to order
- Returns
list of Version - a new list sorted ascending
subset
semver.subset(inner as string, outer as string)Report whether every version allowed by inner is also allowed by outer (inner is the tighter / implied constraint), prereleases included. subset("^1.5.0", "^1.0.0") is true; subset("^1.0.0", "^1.5.0") is false.
inner- the candidate subset range
outer- the superset range
- Returns
bool - true when inner is a subset of outer
toString
semver.toString(v as Version)Render a Version back to its canonical string form.
v- the version to format
- Returns
string - the "major.minor.patch[-prerelease][+build]" text
validRange
semver.validRange(range as string)Report whether a range expression is well-formed (parseable). Does not evaluate it against any version. Each operand is a full SemVer version, a wildcard, or a partial / x-range of one to three parts (numbers without leading zeros, at most 9223372036854775806, and wildcards only after them: 1.x but not 1.x.3); a prerelease or build needs all three numbers.
range- the range expression
- Returns
bool - true when the range is valid
Structs
Version
A parsed SemVer version: numeric core plus optional prerelease / build tags.
def struct Version {
major as int,
minor as int,
patch as int,
prerelease as string,
build as string
};major- the major version
minor- the minor version
patch- the patch version
prerelease- the prerelease tag, or "" if none
build- the build metadata, or "" if none