Advertisement
Guest User

Untitled

a guest
Oct 4th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.43 KB | None | 0 0
  1. module BrazilianUtils.Helpers
  2.  
  3. open System
  4.  
  5. let isNotRepdigit (value: int list) =
  6.     value
  7.     |> Seq.forall (fun elem -> elem = value.[0])
  8.     |> not
  9.  
  10. let charToInt (c : char) = int c - int '0'
  11.  
  12. let onlyNumbers value =
  13.     value
  14.     |> String.filter Char.IsDigit
  15.  
  16. let stringToIntList value =
  17.     value
  18.     |> onlyNumbers
  19.     |> Seq.map charToInt
  20.     |> Seq.toList
  21.  
  22. let hasValue value =
  23.     Seq.length value <> 0
  24.  
  25. let calculateModulus11 weights (value: int list) =
  26.     weights
  27.     |> List.mapi (fun i weight -> value.[i] * weight)
  28.     |> List.sum
  29.     |> (fun x -> x % 11)
  30.  
  31. let generateRandomNumbers count =
  32.     let rnd = System.Random()
  33.     List.init count (fun _ -> rnd.Next(0, 9))
  34.  
  35. let hasLength length value =
  36.     List.length value = length
  37.  
  38. module BrazilianUtils.Cpf
  39.  
  40. open Helpers
  41. open System.Text
  42.  
  43. let private cpfLength = 11
  44. let private firstCheckDigitWeights = [ 10..-1..2 ]
  45. let private secondCheckDigitWeights = [ 11..-1..2 ]
  46.  
  47. let private digitRule digit =
  48.     match digit with
  49.     | 0 | 1 -> 0
  50.     | _ -> 11 - digit
  51.  
  52. let private calculateDigit weights value =
  53.     value
  54.     |> calculateModulus11 weights
  55.     |> digitRule
  56.  
  57. let private validateCheckDigit value weights checkDigit =
  58.     value
  59.     |> calculateDigit weights
  60.     |> ((=) checkDigit)
  61.  
  62. let private isValidFirstCheckDigit (value : int list) =
  63.     let checkDigitPos = 9
  64.     let checkDigit = value.[checkDigitPos]
  65.     validateCheckDigit value firstCheckDigitWeights checkDigit
  66.  
  67. let private isValidSecondCheckDigit (value : int list) =
  68.     let checkDigitPos = 10
  69.     let checkDigit = value.[checkDigitPos]
  70.     validateCheckDigit value secondCheckDigitWeights checkDigit
  71.  
  72. let private hasCpfLength = hasLength cpfLength
  73.  
  74. //  Visible members
  75. let IsValid cpf =
  76.     let cpf' = stringToIntList cpf
  77.    [ hasValue; hasCpfLength; isNotRepdigit; isValidFirstCheckDigit; isValidSecondCheckDigit ]
  78.    |> Seq.forall (fun validator -> validator cpf')
  79.  
  80. let Format cpf =
  81.     let clearValue = onlyNumbers cpf
  82.     StringBuilder(clearValue).Insert(3, ".").Insert(7, ".").Insert(11, "-").ToString()
  83.  
  84. let Generate () =
  85.     let baseCpf = generateRandomNumbers (cpfLength - 2)
  86.     let firstCheckDigit = calculateDigit firstCheckDigitWeights baseCpf
  87.     let secondCheckDigit = calculateDigit secondCheckDigitWeights (baseCpf@[firstCheckDigit])
  88.     baseCpf @ [firstCheckDigit] @ [secondCheckDigit]
  89.     |> List.map (fun x -> x.ToString())
  90.     |> String.concat ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement