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 calculate(
digits: List(Int),
) -> Result(Int, CheckDigitError)
Calculate the check digit for a list of digits.
Implements the GS1 Modulo 10 algorithm:
- Multiply digits alternately by 3 and 1 from right to left
- Sum all products
- Calculate modulo 10 of the sum
- 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")