atLeastfunctionfunction atLeast(have: string, want: string): booleanWhether dotted version have is at least want.
Versions: semantic versions by the rules of semver.org 2.0.0 (parsed, printed, ordered and bumped), and the looser dotted versions that programs print (2.9.8, 1.4.4, 26.13.1), compared part by part (commons 0001-formats FR-015); and npm's ranges, read and satisfied as node-semver reads them (commons 0001-formats FR-014). Taken from erga's kernel (semver.ts) and toolchain (atLeast), Stolos's tools/version.ts and the machine setup.
import { atLeast, compareSemVer, parseSemVer } from "./semver.ts";
atLeast("2.10.0", "2.9.8"); // true
const a = parseSemVer("1.0.0-rc.1"), b = parseSemVer("1.0.0");
if (a.kind === "ok" && b.kind === "ok") compareSemVer(a.value, b.value); // negative: a pre-release comes firstatLeastfunctionfunction atLeast(have: string, want: string): booleanWhether dotted version have is at least want.
bumpfunctionfunction bump(v: SemVer, level: Level): SemVerThe next version at a level: the parts after it reset to zero, and the pre-release and build dropped.
bumpSufficesfunctionfunction bumpSuffices(before: SemVer, after: SemVer, change: Change): booleanWhether moving from before to after is enough for change; before 1.0 a breaking change needs only the minor raised.
compareDottedfunctionfunction compareDotted(a: string, b: string): numberNegative, zero or positive as dotted version a is below, equal to or above b, part by part; a missing part counts as 0.
compareSemVerfunctionfunction compareSemVer(a: SemVer, b: SemVer): numberNegative where a comes before b, positive where after, zero where they have the same precedence (semver.org 2.0.0 §11): numbers numerically, a pre-release before its release, pre-release identifiers one by one (numeric ones numerically and before the others), and the build ignored.
dottedPartsfunctionfunction dottedParts(version: string): readonly number[]The numbers of a dotted version: split at ., +, _ and -, each part's leading digits, and 0 where a part has none.
formatRangefunctionfunction formatRange(range: Range): stringA range as text: each set's comparators joined by spaces (= written as the bare version, an empty set as *), the sets by ||.
formatSemVerfunctionfunction formatSemVer(v: SemVer): stringA version as text: 1.2.3, 1.2.3-rc.1 or 1.2.3-rc.1+build.5.
maxSatisfyingfunctionfunction maxSatisfying(versions: readonly SemVer[], range: Range, options?: SatisfiesOptions): SemVer | undefinedThe highest of the versions that satisfy the range, or undefined where none does.
parseRangefunctionfunction parseRange(text: string): Result<Range, string>Text read as an npm range (commons 0001-formats FR-014), by node-semver's grammar and its desugaring: comparators (<, <=, >, >=, =) joined by spaces, sets joined by ||, hyphen ranges (1.2.3 - 2.3.4), x-ranges (*, x, X or a missing part), tilde (~, ~>) and caret (^) ranges, a space allowed after an operator, a leading v and builds ignored. Anything else is an error naming the part.
parseSemVerfunctionfunction parseSemVer(text: string): Result<SemVer, string>Text read as a semantic version (a leading v allowed, as tags carry it), or why it is not one.
satisfiesfunctionfunction satisfies(version: SemVer, range: Range, options?: SatisfiesOptions): booleanWhether a version satisfies a range (commons 0001-formats FR-014): every comparator of one of its sets holds. A pre-release version satisfies a set only where one of its comparators is a pre-release of the same major, minor and patch (1.2.3-beta.4 satisfies ^1.2.3-beta.2, 1.2.4-beta does not satisfy ^1.2.3), as node-semver has it, unless includePrerelease is given, when the comparators alone decide.
versionInfunctionfunction versionIn(text: string): string | undefinedThe first dotted version in a program's output (bun 1.4.4 (...) gives 1.4.4), or undefined.
Changetypeno test imports ittype Change = { readonly kind: "none" } | { readonly kind: "fix" } | { readonly kind: "addition" } | { readonly kind: "breaking" };The change a surface made, and so the least its version must change by.
Comparatortypeno test imports ittype Comparator = { readonly op: Operator; readonly version: SemVer };A version compared with one bound: >=1.2.3, <2.0.0-0.
Leveltypeno test imports ittype Level = "major" | "minor" | "patch";Which part of a version a change raises.
Operatortypeno test imports ittype Operator = "<" | "<=" | ">" | ">=" | "=";How a comparator compares a version with its bound.
Rangetypeno test imports ittype Range = { readonly kind: "range"; readonly sets: readonly (readonly Comparator[])[] };A range: sets of comparators joined by ||. A version satisfies it where it satisfies every comparator of one set; an empty set holds every version (*), and no set at all holds none.
SatisfiesOptionstypeno test imports ittype SatisfiesOptions = { readonly includePrerelease?: boolean };Whether pre-releases are held to the comparators alone (includePrerelease), rather than only where a bound names their release.
SemVertypetype SemVer = {
readonly major: number;
readonly minor: number;
readonly patch: number;
readonly pre?: string;
readonly build?: string;
};A semantic version: major, minor and patch, and an optional pre-release and build, each as its dot-separated text.