Skip to content
Jennifer Programming Language

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

NameWhat it is
cleanNormalise a version string: trim whitespace and a leading = / v, then return the canonical form if it i...
coerceExtract a version from a loose string - a git tag, a partial, or text with a version-like run - and return ...
compareCompare two versions by SemVer precedence: numeric core, then a prerelease ranks below its release, then pr...
diffThe kind of change from a to b: "major", "minor", "patch", "prerelease" (only the prerelease tag differs), ...
eqReport whether a and b have equal precedence (build metadata ignored).
gtReport whether a orders after b.
gteReport whether a orders at or after b.
gtrReport whether a version is greater than every version a range allows (beyond its upper extreme).
incMajorBump the major version, resetting minor / patch and clearing the tags.
incMinorBump the minor version, resetting patch and clearing the tags.
incPatchBump the patch version, clearing the tags.
intersectsReport whether two ranges share at least one satisfying version, prereleases included (a prerelease overlap...
isPrereleaseReport whether the version carries a prerelease tag.
isStableReport whether the version is stable: a released (major >= 1) version with no prerelease tag.
isValidReport whether a string is a valid SemVer 2.0.0 version.
ltReport whether a orders before b.
lteReport whether a orders at or before b.
ltrReport whether a version is less than every version a range allows (below its lower extreme).
maxSatisfyingPick the highest version from a list that satisfies the range.
minSatisfyingPick the lowest version from a list that satisfies the range.
minVersionThe lowest version that could satisfy a range (its floor), or "" when the range is empty or invalid.
neqReport whether a and b differ in precedence.
outsideReport whether a version is beyond a range's extremes (above it or below it).
parseParse a version string into a Version.
rsortReturn a new list ordered descending by SemVer precedence (highest first).
satisfiesReport whether a concrete version satisfies a range.
simplifyRangeSimplify a range against a known list of versions: return the shortest range that matches exactly the same ...
sortReturn a new list ordered ascending by SemVer precedence.
subsetReport whether every version allowed by inner is also allowed by outer (inner is the tighter / implied ...
toStringRender a Version back to its canonical string form.
validRangeReport whether a range expression is well-formed (parseable).
Versionstruct: A parsed SemVer version: numeric core plus optional prerelease / build tags.

Functions

clean

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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.

jennifer
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