Skip to content

Instantly share code, notes, and snippets.

@mausch
Created June 7, 2012 00:28
Show Gist options
  • Save mausch/2885737 to your computer and use it in GitHub Desktop.
Save mausch/2885737 to your computer and use it in GitHub Desktop.
Validar CUIL
[Test]
public void IsValidatedCUIL_WithValidatedCuil() {
var returnValue = CUILValidation.ValidateCUIL("20-33817701-2");
Assert.IsTrue(returnValue.IsOk);
}
[Test]
public void IsValidatedCUIL_WithInvalidatedCuil() {
var returnValue = CUILValidation.ValidateCUIL("20-33817701-3");
Assert.IsTrue(returnValue.IsInvalidDigit);
}
[Test]
public void IsInvalidatedString_WithInvalidatedCuil() {
var returnValue = CUILValidation.ValidateCUIL("20338177013");
Assert.IsTrue(returnValue.IsInvalidFormat);
}
// original: https://dl.dropbox.com/u/14102783/Program.fs
// https://twitter.com/AugustoPedraza/statuses/210514248768557056
module CUILValidation
open System
open System.Text.RegularExpressions
let getCheckDigit =
let validateDigits = [5;4;3;2;7;6;5;4;3;2]
fun cuil ->
match 11 - Seq.sum (Seq.map2 (*) validateDigits cuil) % 11 with
| 11 -> 0
| 10 -> 9
| x -> x
let inline charToInt c = int c - 48
type CUILResult = Ok | InvalidFormat | InvalidDigit
let ValidateCUIL (cuil : string) =
if Regex.IsMatch(cuil, @"^\d{2}-\d{8}-\d{1}$") then
let digits = Regex.Replace(cuil, "-", "")
let checkDigit = digits.[..digits.Length-2] |> Seq.map charToInt |> getCheckDigit
if checkDigit = charToInt cuil.[cuil.Length-1]
then Ok
else InvalidDigit
else InvalidFormat
//
//open System
//
//printfn "%A" (ValidateCUIL("20-33817701-2"))
//printfn "%A" (ValidateCUIL("20-33817701-3"))
//printfn "%A" (ValidateCUIL("33-70991844-9"))
//printfn "%A" (ValidateCUIL("20338177013"))
//printfn "%A" (ValidateCUIL("20338177013"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment