CPF v0.6.0 CPF View Source
CPF module that provides functions to verify if a CPF is valid.
Link to this section Summary
Functions
Returns a tuple with the eleven digits of the given cpf.
Cleans up all characters of a given input except numbers. It can make CPF validation more flexbile. For example
Returns a formatted string from a given cpf.
Initializes a CPF.
Builds a CPF struct by validating its digits and format. Returns an ok/error tuple for valid/invalid CPFs.
Builds a CPF struct by validating its digits and format. Returns an CPF type
or raises an CPF.ParsingError exception.
Returns a integer representation of the given cpf.
Returns true the given cpf is valid, otherwise false.
Link to this section Types
t()
View Source
(opaque)
t()
t()
The CPF type. It' composed of eleven digits(0-9]).
Link to this section Functions
digits(cpf) View Source
Returns a tuple with the eleven digits of the given cpf.
flex(input) View Source
Cleans up all characters of a given input except numbers. It can make CPF validation more flexbile. For example:
Examples
iex> CPF.flex(" 04.4 .8*58().476-08 ")
"04485847608"
iex> " 04.4 .8*58().476-08 " |> CPF.flex() |> CPF.valid?()
true
format(cpf) View Source
Returns a formatted string from a given cpf.
Examples
iex> 563_606_676_73 |> CPF.new() |> CPF.format()
"563.606.676-73"
new(cpf)
View Source
new(String.t() | pos_integer()) :: t()
new(String.t() | pos_integer()) :: t()
Initializes a CPF.
Examples
iex> CPF.new(563_606_676_73)
#CPF<563.606.676-73>
iex> CPF.new("56360667673")
#CPF<563.606.676-73>
This function doesn't check if CPF numbers are valid, only use this function
if the given String.t or the integer was validated before.
parse(cpf)
View Source
parse(String.t() | pos_integer()) ::
{:ok, t()} | {:error, CPF.ParsingError.t()}
parse(String.t() | pos_integer()) :: {:ok, t()} | {:error, CPF.ParsingError.t()}
Builds a CPF struct by validating its digits and format. Returns an ok/error tuple for valid/invalid CPFs.
Examples
iex> {:ok, cpf} = CPF.parse(563_606_676_73)
iex> cpf
#CPF<563.606.676-73>
iex> CPF.parse(563_606_676_72)
{:error, %CPF.ParsingError{reason: :invalid_verifier}}
parse!(cpf)
View Source
parse!(String.t() | pos_integer()) :: t()
parse!(String.t() | pos_integer()) :: t()
Builds a CPF struct by validating its digits and format. Returns an CPF type
or raises an CPF.ParsingError exception.
Examples
iex> CPF.parse!(563_606_676_73)
#CPF<563.606.676-73>
iex> CPF.parse!(563_606_676_72)
** (CPF.ParsingError) invalid_verifier
to_integer(cpf)
View Source
to_integer(t()) :: pos_integer()
to_integer(t()) :: pos_integer()
Returns a integer representation of the given cpf.
Examples
iex> 4_485_847_608 |> CPF.new() |> CPF.to_integer()
4_485_847_608
valid?(cpf)
View Source
valid?(input :: String.t() | pos_integer()) :: boolean()
valid?(input :: String.t() | pos_integer()) :: boolean()
Returns true the given cpf is valid, otherwise false.
Examples
iex> CPF.valid?(563_606_676_73)
true
iex> CPF.valid?(563_606_676_72)
false
iex> CPF.valid?("563.606.676-73")
true
iex> CPF.valid?("563/60.6-676/73")
false
iex> CPF.valid?("563.606.676-72")
false
iex> CPF.valid?("56360667673")
true
iex> CPF.valid?("56360667672")
false