Luhn validation library for OCaml
  • OCaml 74.7%
  • Makefile 14.5%
  • Dune 10.8%
Find a file
2026-06-14 16:45:24 +02:00
lib feat: inital commit 2026-06-11 01:59:22 +02:00
test feat: add more tests 2026-06-11 20:15:02 +02:00
.gitignore feat: inital commit 2026-06-11 01:59:22 +02:00
dune-project chore: add ounit2 dependency to dune-project and opam files 2026-06-12 10:40:21 +02:00
LICENSE docs: add LICENSE file 2026-06-11 02:16:31 +02:00
luna.opam chore: add ounit2 dependency to dune-project and opam files 2026-06-12 10:40:21 +02:00
Makefile feat: inital commit 2026-06-11 01:59:22 +02:00
README.md docs: add opam installation command to README 2026-06-14 16:45:24 +02:00

Luna

Luna is a validation library implementing the Luhn algorithm for OCaml.

Installation

To start using the library, you can install it via opam :

opam install luna

Usage

Luna can be used as follows:

Validating a number

let is_valid nb =
    if Luna.validate nb then
    Printf.printf "%s: Valid number !\n" nb
    else Printf.printf "%s: Invalid number !\n" nb


let () =
    is_valid "17893729974";
    is_valid "28924292203"

(*
    17893729974: Valid number !
    28924292203: Invalid number !
*)

Calculating the check digit

Given s as the input number string, the check digit is the smallest digit (possibly zero) that can be added to s to make it Luhn-valid.

let get_digit nb =
  let result = Luna.checksum_digit nb in
  Printf.printf "The check digit for number %s is %d\n" nb result

let () =
    get_digit "1789372997";
    get_digit "28924292203"

(*
    The check digit for number 1789372997 is 4
    The check digit for number 28924292203 is 6
*)

License

This project is licensed under the MIT License - see the LICENSE file for details.