npm-compatible semantic versioning.
Parses and matches 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.
Examples
iex> NPMSemver.matches?("1.2.3", "^1.0.0")
true
iex> NPMSemver.matches?("2.0.0", "^1.0.0")
false
iex> NPMSemver.matches?("1.5.0", ">=1.2.3 <2.0.0")
true
iex> NPMSemver.matches?("1.2.4", "~1.2.3")
true
iex> NPMSemver.matches?("2.1.3", "2.x.x")
trueTest fixtures ported from node-semver.
Summary
Functions
Check if a version satisfies a range.
Find the highest version in a list that satisfies the range.
Parse a range string into a NPMSemver.Range struct.
Parse a version string into a NPMSemver.Version struct.
Convert an npm range string to a hex_solver-compatible Elixir requirement string.
Convert an npm range string to a HexSolver constraint.
Functions
Check if a version satisfies a range.
iex> NPMSemver.matches?("1.8.1", "^1.2.3")
true
iex> NPMSemver.matches?("0.1.2", "^0.1")
true
Find the highest version in a list that satisfies the range.
iex> NPMSemver.max_satisfying(["1.0.0", "1.5.0", "2.0.0"], "^1.0.0")
"1.5.0"
iex> NPMSemver.max_satisfying(["0.1.0", "0.2.0"], "^1.0.0")
nil
@spec parse_range( String.t(), keyword() ) :: {:ok, NPMSemver.Range.t()} | :error
Parse a range string into a NPMSemver.Range struct.
iex> {:ok, _range} = NPMSemver.parse_range("^1.2.3")
@spec parse_version( String.t(), keyword() ) :: {:ok, NPMSemver.Version.t()} | :error
Parse a version string into a NPMSemver.Version struct.
iex> {:ok, v} = NPMSemver.parse_version("1.2.3-beta.1")
iex> {v.major, v.minor, v.patch, v.pre}
{1, 2, 3, ["beta", 1]}
Convert an npm range string to a hex_solver-compatible Elixir requirement string.
iex> NPMSemver.to_elixir_requirement("^1.2.3")
{:ok, ">= 1.2.3 and < 2.0.0-0"}
iex> NPMSemver.to_elixir_requirement("~1.2.3")
{:ok, ">= 1.2.3 and < 1.3.0-0"}
iex> NPMSemver.to_elixir_requirement(">=1.0.0 <2.0.0 || >=3.0.0")
{:ok, ">= 1.0.0 and < 2.0.0 or >= 3.0.0"}
@spec to_hex_constraint( String.t(), keyword() ) :: {:ok, HexSolver.constraint()} | :error
Convert an npm range string to a HexSolver constraint.
Returns an opaque constraint value that can be used with HexSolver.run/4
and returned from HexSolver.Registry callbacks.
iex> {:ok, _constraint} = NPMSemver.to_hex_constraint("^1.2.3")