NPMSemver (NPMSemver v0.1.0)

Copy Markdown View Source

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")
true

Test 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

matches?(version_string, range_string, opts \\ [])

@spec matches?(String.t(), String.t(), keyword()) :: boolean()

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

max_satisfying(versions, range_string, opts \\ [])

@spec max_satisfying([String.t()], String.t(), keyword()) :: String.t() | nil

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

parse_range(string, opts \\ [])

@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")

parse_version(string, opts \\ [])

@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]}

to_elixir_requirement(range_string, opts \\ [])

@spec to_elixir_requirement(
  String.t(),
  keyword()
) :: {:ok, String.t()} | :error

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"}

to_hex_constraint(range_string, opts \\ [])

@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")