Skip to content

Commit

Permalink
Add ATIN (U.S. Adoption Taxpayer Identification Number).
Browse files Browse the repository at this point in the history
  • Loading branch information
kupolak committed Jan 27, 2024
1 parent ca66cc3 commit decc5b9
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/us/atin.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
exception Invalid_format

let atin_re = Str.regexp "^9\\([0-9][0-9]\\)-93-\\([0-9][0-9][0-9][0-9]\\)$"

let validate number =
if Str.string_match atin_re number 0 then number else raise Invalid_format

let is_valid number =
try
ignore (validate number);
true
with Failure _ -> false

let format_number number =
if String.length number = 9 then
String.sub number 0 3 ^ "-" ^ String.sub number 3 2 ^ "-"
^ String.sub number 5 4
else number
22 changes: 22 additions & 0 deletions lib/us/atin.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(*
ATIN (U.S. Adoption Taxpayer Identification Number).
An Adoption Taxpayer Identification Number (ATIN) is a temporary
nine-digit number issued by the United States IRS for a child for whom the
adopting parents cannot obtain a Social Security Number.
*)

exception Invalid_format

val atin_re : Str.regexp
(** Regular expression for matching ATINs *)

val validate : string -> string
(** Check if the number is a valid ATIN.
This checks the length and formatting if it is present. *)

val is_valid : string -> bool
(** Check if the number is a valid ATIN. *)

val format_number : string -> string
(** Reformat the number to the standard presentation format. *)
4 changes: 4 additions & 0 deletions lib/us/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(library
(name us)
(modules atin)
(libraries tools))
4 changes: 4 additions & 0 deletions test/us/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(test
(name test_atin)
(libraries alcotest us)
(modules test_atin))
24 changes: 24 additions & 0 deletions test/us/test_atin.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
let test_validate () =
let input = "900-93-0000" in
let expected_result = "900-93-0000" in
let result = Us.Atin.validate input in
Alcotest.(check string) "test_validate" expected_result result

let test_is_valid () =
let input = "900-93-0000" in
let expected_result = true in
let result = Us.Atin.is_valid input in
Alcotest.(check bool) "test_is_valid" expected_result result

let test_is_not_valid () =
Alcotest.check_raises "Invalid Length" Us.Atin.Invalid_format (fun () ->
ignore (Us.Atin.is_valid "123"))

let suite =
[
("test_validate", `Quick, test_validate)
; ("test_is_valid", `Quick, test_is_valid)
; ("test_is_not_valid", `Quick, test_is_not_valid)
]

let () = Alcotest.run "ATIN" [ ("suite", suite) ]

0 comments on commit decc5b9

Please sign in to comment.