go-npm-version
go-npm-version is a library for parsing npm versions and version constraints, and verifying versions against a set of constraints.
go-npm-version can sort a collection of versions properly, handles prerelease versions, etc.
Versions used with go-npm-version must follow Semantic Versioning.
Constraints used with go-npm-version must follow npm rules.
For more details, see here
Usage
Version Parsing and Comparison
See example
v1, _ := npm.NewVersion("1.2.3-alpha")
v2, _ := npm.NewVersion("1.2.3")
// Comparison example. There is also GreaterThan, Equal, and just
// a simple Compare that returns an int allowing easy >=, <=, etc.
if v1.LessThan(v2) {
fmt.Printf("%s is less than %s", v1, v2)
}
Version Constraints
See example
v, _ := npm.NewVersion("2.1.0")
c, _ := npm.NewConstraints(">= 1.0, < 1.4 || > 2.0")
if c.Check(v) {
fmt.Printf("%s satisfies constraints '%s'", v, c)
}
Version Sorting
See example
versionsRaw := []string{"1.1.0", "0.7.1", "1.4.0-alpha", "1.4.0-beta", "1.4.0", "1.4.0-alpha.1"}
versions := make([]npm.Version, len(versionsRaw))
for i, raw := range versionsRaw {
v, _ := npm.NewVersion(raw)
versions[i] = v
}
// After this, the versions are properly sorted
sort.Sort(npm.Collection(versions))
CLI
Build
$ go build -o version cmd/version/main.go
Compare
$ ./version compare 0.1.2 0.1.3
-1
Constraint
$ ./version satisfy 0.1.2 ">0.1.1"
true
Status
go-npm-version doesn't support a range of versions yet.
-
^
(e.g. ^0.13.0)
-
~
(e.g. ~0.13.0)
-
>
-
>=
-
<
-
<=
-
=
-
-
(e.g. 2.1.0 - 2.6.2)
-
||
(e.g. < 2.1 || > 2.6)