gl_gtin/check_digit

Check digit calculation module for GTIN codes.

Implements the GS1 Modulo 10 algorithm for calculating and validating GTIN check digits.

Types

Error type for check digit operations

pub type CheckDigitError {
  InvalidLength(got: Int)
  InvalidCharacters
}

Constructors

  • InvalidLength(got: Int)
  • InvalidCharacters

Values

pub fn append(
  digits: List(Int),
) -> Result(List(Int), CheckDigitError)

Append the computed check digit to a digit body.

Validates the body via the same guard as calculate (non-empty and every element in 0..9), computes the check digit with the GS1 Modulo 10 algorithm, and returns the original body with that check digit appended as its final element. This is the inverse of valid: valid(append(body)) is always True for any well-formed body.

Arguments

  • digits - List of body digits (without a check digit)

Returns

Ok(digits ++ [check_digit]) if the body is well-formed, Error(InvalidLength(got: 0)) for an empty list, or Error(InvalidCharacters) if any element is outside 0..9.

Examples

append([6, 2, 9, 1, 0, 4, 1, 5, 0, 0, 2, 1])
// -> Ok([6, 2, 9, 1, 0, 4, 1, 5, 0, 0, 2, 1, 3])

append([])
// -> Error(InvalidLength(got: 0))
pub fn calculate(
  digits: List(Int),
) -> Result(Int, CheckDigitError)

Calculate the check digit for a list of digits.

Implements the GS1 Modulo 10 algorithm:

  1. Multiply digits alternately by 3 and 1 from right to left
  2. Sum all products
  3. Calculate modulo 10 of the sum
  4. If result is 0, check digit is 0; otherwise check digit is (10 - result)

Arguments

  • digits - List of digits (without check digit)

Returns

Ok(check_digit) if successful, Error if the digit list is invalid.

Examples

calculate([6, 2, 9, 1, 0, 4, 1, 5, 0, 0, 2, 1])
// -> Ok(3)
pub fn generate(code: String) -> Result(String, CheckDigitError)

Generate a complete GTIN with calculated check digit.

Takes an incomplete GTIN (7, 11, 12, or 13 digits) and calculates the check digit to produce a complete GTIN (8, 12, 13, or 14 digits respectively).

Arguments

  • code - Incomplete GTIN string

Returns

Ok(complete_gtin) if successful, Error otherwise.

Examples

generate("629104150021")
// -> Ok("6291041500213")

generate("123456789012")
// -> Ok("1234567890128")
pub fn valid(digits: List(Int)) -> Bool

Validate a digit list whose final element is its check digit.

Splits the list into its leading body and final element, computes the check digit for the body via the GS1 Modulo 10 algorithm, and returns True only when the final element equals that computed check digit. An empty list has no body to check against and returns False; any input for which calculate errors (for example a body containing an out-of-range element) also returns False.

Arguments

  • digits - List of digits whose last element is treated as the check digit

Returns

True if the last element matches the computed check digit, False otherwise.

Examples

valid([6, 2, 9, 1, 0, 4, 1, 5, 0, 0, 2, 1, 3])
// -> True

valid([])
// -> False
✨ Search Document