-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ATIN (U.S. Adoption Taxpayer Identification Number).
- Loading branch information
Showing
5 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. *) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(library | ||
(name us) | ||
(modules atin) | ||
(libraries tools)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(test | ||
(name test_atin) | ||
(libraries alcotest us) | ||
(modules test_atin)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ] |