Barlix.UPCE (barlix v0.6.3) View Source

Implements UPC-E.

Link to this section Summary

Functions

Encodes the given value using UPC-E. The given code is validated first.

Accepts the same arguments as encode/1 but raises on error.

Validate an UPC-E code.

Link to this section Functions

Specs

encode(String.t()) :: {:error, String.t()} | {:ok, Barlix.code()}

Encodes the given value using UPC-E. The given code is validated first.

Examples

iex> Barlix.UPCE.encode("04252614")
{:ok, {:D1, [
  1, 0, 1,
  0, 0, 1, 1, 1, 0, 1,
  0, 0, 1, 0, 0, 1, 1,
  0, 1, 1, 1, 0, 0, 1,
  0, 0, 1, 1, 0, 1, 1,
  0, 1, 0, 1, 1, 1, 1,
  0, 0, 1, 1, 0, 0, 1,
  0, 1, 0, 1, 0, 1
]}}

iex> Barlix.UPCE.encode("123456")
{:error, "expected a string with exactly 8 chars, received 6 chars instead"}

iex> Barlix.UPCE.encode("06543214")
{:error, "validation failed: expected checksum digit 7 but received 4"}

Specs

encode!(String.t()) :: Barlix.code() | no_return()

Accepts the same arguments as encode/1 but raises on error.

Specs

validate(String.t()) :: {:ok, [non_neg_integer()]} | {:error, String.t()}

Validate an UPC-E code.

Examples

iex> Barlix.UPCE.validate("04252614")
{:ok, [0, 4, 2, 5, 2, 6, 1, 4]}

iex> Barlix.UPCE.validate("123456789")
{:error, "expected a string with exactly 8 chars, received 9 chars instead"}

iex> Barlix.UPCE.validate("16543217")
{:error, "validation failed: expected checksum digit 4 but received 7"}

iex> Barlix.UPCE.validate(123)
{:error, "unexpected input"}