Grav v1.14.0

Semver

import "std/data" (included in import "std") · namespace std.data

Semantic versions (semver.org 2.0): parsing, ordering (a prerelease comes before its release, numeric identifiers compare as numbers) and ranges in npm's syntax: =, >, >=, <, <=, ^ (the same leftmost non-zero part), ~ (the same minor), x wildcards, hyphen ranges (1.2 - 1.4), a space for "and" and || for "or". As in npm, a prerelease version satisfies a range only when a comparator names a prerelease of the same version, so ^1.0 never picks 2.0.0-beta.

grav
import "std"

fn main() {
    print(Semver.satisfies("1.4.2", "^1.2"))
    print(Semver.satisfies("2.0.0-beta.1", "^1.0"))
    print(Semver.maxSatisfying(["1.2.0", "1.9.9", "2.1.0"], "~1.9"))
    print(Semver.sort(["1.0.0", "1.0.0-rc.1", "1.0.0-beta.11", "1.0.0-beta.2"]))
    print(Semver.parse("1.2.3").nextMinor().toString())
}
Output
true
false
1.9.9
["1.0.0-beta.2", "1.0.0-beta.11", "1.0.0-rc.1", "1.0.0"]
1.3.0

class Semver

Constructors

  • new Semver(major: long, minor: long, patch: long)

Static members

  • static fn parse(text: string) -> Semver — A version ("1.2.3", "v1.2.3", "1.2.3-beta.2+sha.5"); ValueError otherwise.
  • static fn tryParse(text: string) -> Semver — The version, or null when text is not one.
  • static fn satisfies(version: string, range: string) -> bool — Whether version is in range; false for text that is not a version.
  • static fn maxSatisfying(versions: Array<string>, range: string) -> string — The highest of versions in range ("" when none is).
  • static fn sort(versions: Array<string>) -> Array<string> — Versions in ascending order (ones that are not versions are dropped).

Fields

  • major: long
  • minor: long
  • patch: long
  • prerelease: Array<string> = new Array<string>() — "rc.1" in 1.0.0-rc.1, split at dots ([] for a release).
  • build: string = "" — "build.5" in 1.0.0+build.5 (ignored when ordering).

Methods

  • fn isPrerelease() -> bool
  • fn compare(o: Semver) -> int — <0, 0, >0 by precedence (build metadata ignored).
  • fn equals(o: Semver) -> bool
  • fn toString() -> string
  • fn nextMajor() -> Semver — The next major, minor or patch release.
  • fn nextMinor() -> Semver
  • fn nextPatch() -> Semver

class SemverComparator

One comparator of a range: an operator and a version.

Constructors

  • new SemverComparator(op: string, v: Semver)

Fields

  • op: string
  • v: Semver

Methods

  • fn test(x: Semver) -> bool

class SemverPartial

A version as a range writes it: parts may be missing (-1).

Constructors

  • new SemverPartial(major: long, minor: long, patch: long, pre: Array<string>)

Fields

  • major: long
  • minor: long
  • patch: long
  • pre: Array<string>

class SemverRange

Static members

  • static fn all(v: Semver, cs: Array<SemverComparator>) -> bool — Whether v passes every comparator (and the prerelease rule).
  • static fn comparators(text: string) -> Array<SemverComparator> — The comparators of one alternative ("" or "*": any release), or null when malformed.
  • static fn partial(text: string) -> SemverPartial — The parts of "1", "1.2", "1.2.3", "1.x" (-1 for a missing or wildcard part; a full version keeps its prerelease); null when malformed.
  • static fn floor(p: SemverPartial) -> Semver

Edit this page on GitHub