View Source CPF (CPF v1.2.0)

CPF module that provides functions to verify if a CPF is valid.

Link to this section Summary

Types

A text in String.t() or a positive integer

t()

The CPF type. It' composed of eleven digits(0-9]).

Functions

Checks if the given argument is CPF.t() type.

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.

Returns a formatted string from a given cpf.

Generates a random valid CPF.

Generates a predictable random valid CPF wit the given seed.

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

@type input() :: String.t() | pos_integer()

A text in String.t() or a positive integer

@opaque t()

The CPF type. It' composed of eleven digits(0-9]).

Link to this section Functions

@spec cpf?(any()) :: true | false

Checks if the given argument is CPF.t() type.

examples

Examples

iex> CPF.cpf?(563_606_676_73)
false

iex> "56360667673" |> CPF.new() |> CPF.cpf?()
true
@spec digits(t()) :: tuple()

Returns a tuple with the eleven digits of the given cpf.

@spec flex(String.t()) :: String.t()

Cleans up all characters of a given input except numbers. It can make CPF validation more flexbile.

examples

Examples

iex> CPF.flex("  04.4 .8*58().476-08  ")
"04485847608"

iex> "  04.4 .8*58().476-08  " |> CPF.flex() |> CPF.valid?()
true
@spec format(cpf :: t()) :: String.t()

Returns a formatted string from a given cpf.

examples

Examples

iex> 563_606_676_73 |> CPF.new() |> CPF.format()
"563.606.676-73"
@spec generate() :: t()

Generates a random valid CPF.

examples

Examples

iex> CPF.generate() |> to_string() |> CPF.valid?
true
@spec generate(seed :: :rand.builtin_alg() | :rand.state() | :rand.export_state()) ::
  t()

Generates a predictable random valid CPF wit the given seed.

examples

Examples

iex> seed = {:exrop, [40_738_532_209_663_091 | 74_220_507_755_601_615]}
iex> seed |> CPF.generate() |> CPF.format()
"671.835.731-68"
@spec new(input()) :: t()

Initializes a CPF.

examples

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.

@spec parse(input()) :: {: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

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}}
@spec parse!(input()) :: t()

Builds a CPF struct by validating its digits and format. Returns an CPF type or raises an CPF.ParsingError exception.

examples

Examples

iex> CPF.parse!(563_606_676_73)
#CPF<563.606.676-73>

iex> CPF.parse!(563_606_676_72)
** (CPF.ParsingError) invalid_verifier
@spec to_integer(t()) :: pos_integer()

Returns a integer representation of the given cpf.

examples

Examples

iex> 4_485_847_608 |> CPF.new() |> CPF.to_integer()
4_485_847_608
@spec valid?(input :: input()) :: boolean()

Returns true the given cpf is valid, otherwise false.

examples

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