npm-compatible semantic versioning for Elixir.
Parse and match version ranges using npm's semver syntax: ^1.2.3,
~1.2.3, >=1.0.0 <2.0.0, 1.x, 1.0.0 - 2.0.0, || unions.
def deps do
[{:npm_semver, "~> 0.1.0"}]
endNPMSemver.matches?("1.2.3", "^1.0.0")
# => true
NPMSemver.matches?("2.0.0", "^1.0.0")
# => false
NPMSemver.matches?("1.5.0", ">=1.2.3 <2.0.0")
# => true
NPMSemver.matches?("2.1.3", "2.x.x")
# => trueNPMSemver.max_satisfying(["1.0.0", "1.5.0", "2.0.0"], "^1.0.0")
# => "1.5.0"Convert npm ranges to constraints for hex_solver (PubGrub dependency resolver):
{:ok, constraint} = NPMSemver.to_hex_constraint("^1.2.3")Or get the Elixir requirement string directly:
NPMSemver.to_elixir_requirement("^1.2.3")
# => {:ok, ">= 1.2.3 and < 2.0.0-0"}| Syntax | Example | Expands to |
|---|---|---|
| Caret | ^1.2.3 |
>=1.2.3 <2.0.0 |
| Tilde | ~1.2.3 |
>=1.2.3 <1.3.0 |
| X-range | 1.2.x, 1.*, * |
>=1.2.0 <1.3.0 |
| Comparator | >=1.0.0 <2.0.0 |
as written |
| Hyphen | 1.0.0 - 2.0.0 |
>=1.0.0 <=2.0.0 |
| Union | ^1.0 || ^2.0 |
either range |
| Intersection | >=1.2.1 <=1.2.8 |
space-separated AND |
loose: true— acceptv-prefixed versions and pre-release tags without-separatorinclude_prerelease: true— x-ranges and*match pre-release versions
216 test cases ported from node-semver fixtures.
npm_semver brings npm-compatible semantic version ranges to Elixir.
It is part of a frontend stack that runs inside the BEAM — builds, JS runtimes, icons, and Vue-to-LiveView compilation as supervised parts of the application instead of external toolchain processes. See the Elixir Volt organization for the rest, and Building Blocks for the Future Web for the thesis, architecture, and roadmap that tie them together.
MIT