Semantic Versioning 2.0.0 for MoonBit: versions, precedence and ranges.
// moon.pkg
import {
"moonbitstack/moonsver",
}fn demo() -> Unit raise @moonsver.Refused {
// Parse and print
let v = @moonsver.Version::parse("1.2.3-alpha.1+build.5")
println(v.to_string()) // 1.2.3-alpha.1+build.5
println(v.core_string()) // 1.2.3
println(v.is_prerelease()) // true
// Precedence (§11; build metadata stays out of it)
let a = @moonsver.Version::parse("1.0.0-alpha")
let b = @moonsver.Version::parse("1.0.0")
println(a.precedence_cmp(b)) // -1
// Ranges
let r = @moonsver.Range::parse("^1.2.3")
println(r.to_string()) // >=1.2.3, <2.0.0
println(r.satisfies(@moonsver.Version::parse("1.9.0"))) // true
println(r.satisfies(@moonsver.Version::parse("2.0.0"))) // false
// Bumps
println(@moonsver.Version::parse("1.2.3").inc(@moonsver.Major).to_string()) // 2.0.0
}| Method | What it does |
|---|---|
| Version::parse(s, syntax?) | Parse a string, raising Refused; the npm grammar is the default |
| Version::new(major, minor, patch) | Build from three Ints |
| Version::from_bigint(major, minor, patch) | Build from three BigInts |
| major() / minor() / patch() | The numbers, as BigInt |
| prerelease() / build() | Identifier lists, as ArrayView |
| is_prerelease() / is_stable() | Whether a prerelease is present |
| same_core(other) | Do the three numbers match? |
| core() / core_string() | Drop prerelease and build metadata |
| precedence_cmp(other) | §11 precedence: -1, 0 or 1 |
| compare(other) | Total order: precedence, then build metadata |
| inc(release) | Apply a bump |
| to_string() | The canonical text form |
| Method | What it does |
|---|---|
| Range::parse(s, syntax?) | Parse a range, raising Refused |
| Range::any() | Any version (*) |
| satisfies(version) / satisfies_with(version, policy) | Does the version fit? |
| max_satisfying(versions) / min_satisfying(versions) | The extreme versions that fit |
| satisfying(versions) | Everything that fits, sorted ascending |
| sets() | The disjuncts, as ArrayView[ComparatorSet] |
| policy() | The prerelease policy the range was parsed with |
| is_any() | Whether this is the same as any version |
| to_string() | Canonical text that re-parses under the same grammar |
| Input | Bump | Result |
|---|---|---|
| 1.0.0-pre | Major | 1.0.0 |
| 1.2.0-pre | Major | 2.0.0 |
| 1.2.3-pre | Patch | 1.2.3 |
| 1.2.3 | Prerelease | 1.2.4-0 |
| 1.2.3-alpha.1.2 | Prerelease | 1.2.3-alpha.1.3 |
| 1.2.3-alpha.beta | Prerelease | 1.2.3-alpha.beta.0 |
| 1.2.3-rc.1+build | Stable | 1.2.3 |
| Preset | A bare version means | ^ | ~ | \|\| | Hyphen | 1.x | != |
|---|---|---|---|---|---|---|---|
| Syntax::npm() | exact | yes | yes | yes | yes | yes | no |
| Syntax::cargo() | caret | yes | yes | no | no | yes | no |
| Syntax::ruby() | exact | no | ~> | no | no | no | yes |
| Syntax::strict() | exact | no | no | no | no | no | no |
| Field | What it controls |
|---|---|
| default_op | What a bare version means: Exact, Caret or Tilde |
| tilde_bump | How ~ finds its upper bound: Patch or LastSpecified |
| allow_caret | Accept ^ |
| allow_tilde | Accept ~ (~> only under LastSpecified) |
| allow_ne | Accept != |
| allow_or | Accept \|\| |
| allow_hyphen_range | Accept 1.2.3 - 2.0.0 |
| allow_xrange | Accept 1.x, 1.2.* and bare 1.2 |
| allow_empty | Treat an empty string as any version |
| allow_v_prefix | Accept v1.2.3 |
| allow_equals_prefix | Accept =1.2.3 as a version literal |
| allow_build_metadata | Accept a +build segment |
| trim | Allow whitespace around a version literal |
| prerelease_policy | How prereleases are matched |
| Written | npm | Cargo | Ruby |
|---|---|---|---|
| 1.2.3 | =1.2.3 | >=1.2.3, <2.0.0 | =1.2.3 |
| ~1.2 | >=1.2.0, <1.3.0 | >=1.2.0, <1.3.0 | ~> 1.2 gives >=1.2.0, <2.0.0 |
| 1.2.* | >=1.2.0, <1.3.0 | >=1.2.0, <1.3.0 | not supported |
1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2
< 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0| Policy | Meaning | Example |
|---|---|---|
| SameTuple | Only through a comparator with the same major.minor.patch that carries a prerelease | >=1.2.3-alpha takes 1.2.3-beta, refuses 1.2.4-beta |
| Always | Compare a prerelease like any other version | >=1.0.0 takes 1.5.0-beta |
| Never | Prereleases never match | >=1.0.0 refuses 1.5.0-beta |
$ moon run src/bin -- range "^0.2.3"
>=0.2.3, <0.3.0
$ moon run src/bin -- satisfies 1.9.0 "^1.2.3"
true
$ moon run src/bin -- sort 2.0.0 1.0.0-beta 1.0.0 0.9.9
0.9.9
1.0.0-beta
1.0.0
2.0.0let j = @json.to_json(@moonsver.Range::parse("^1.2.3"))
// Json::string(">=1.2.3, <2.0.0")| Example | What it shows |
|---|---|
| src/examples/tour | The smallest useful walk through the package |
| src/examples/ranges | The same range under each preset, wildcards, hyphen ranges and policies |
| src/examples/bumps | Every Release form against a set of inputs |
| src/examples/jsonio | A JSON round trip for a version and a range |
$ moon run src/examples/tourpub(all) suberror Refused {
Empty(at~ : Int, String)
Unexpected(at~ : Int, String)
Truncated(at~ : Int, String)
Number(at~ : Int, String)
LeadingZero(at~ : Int, String)
EmptyIdentifier(at~ : Int, String)
Identifier(at~ : Int, String)
Unsupported(at~ : Int, String)
Range(at~ : Int, String)
} derive(Eq)impl Eq for Comparatorimpl Show for Comparatorimpl Debug for Comparatorimpl Eq for ComparatorSetimpl Show for ComparatorSetpub(all) enum DefaultOp {
Exact
Caret
Tilde
}impl Compare for Identifierimpl Eq for Identifierimpl Hash for Identifierimpl Show for Identifierimpl Debug for Identifierpub(all) enum Op {
Eq
Ne
Gt
Ge
Lt
Le
}pub(all) enum PrereleasePolicy {
SameTuple
Always
Never
}impl Eq for PrereleasePolicyimpl Debug for PrereleasePolicypub(all) enum Release {
Major
Minor
Patch
PreMajor
PreMinor
PrePatch
Prerelease
Stable
}pub(all) struct Syntax {
allow_v_prefix : Bool
allow_equals_prefix : Bool
allow_build_metadata : Bool
trim : Bool
default_op : DefaultOp
tilde_bump : TildeBump
allow_caret : Bool
allow_tilde : Bool
allow_ne : Bool
allow_or : Bool
allow_hyphen_range : Bool
allow_xrange : Bool
allow_empty : Bool
prerelease_policy : PrereleasePolicy
}pub(all) enum TildeBump {
Patch
LastSpecified
}Install
Download zipSemantic Versioning 2.0.0 for MoonBit: versions, precedence and ranges.