BrDocs.CPF (br_docs v0.1.0)

Copy Markdown View Source

Functions for formatting and validating CPF values.

Summary

Types

A formatted CPF follows the XXX.XXX.XXX-XX mask.

A normalized CPF has 11 digits and no punctuation.

Functions

Calculates the two numeric check digits for a 9-digit CPF base.

Formats a CPF using the XXX.XXX.XXX-XX mask.

Formats a CPF, raising ArgumentError when the input is invalid.

Removes punctuation from a CPF.

Removes punctuation from a CPF, raising on invalid input.

Returns true when the CPF format and check digits are valid.

Returns true when the CPF has a supported shape.

Types

formatted()

@type formatted() :: String.t()

A formatted CPF follows the XXX.XXX.XXX-XX mask.

normalized()

@type normalized() :: String.t()

A normalized CPF has 11 digits and no punctuation.

Functions

check_digits(base)

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

Calculates the two numeric check digits for a 9-digit CPF base.

Examples

iex> BrDocs.CPF.check_digits("123456789")
"09"

format(cpf)

@spec format(String.t()) :: {:ok, formatted()} | {:error, :invalid_format}

Formats a CPF using the XXX.XXX.XXX-XX mask.

The input may be provided with or without punctuation.

Examples

iex> BrDocs.CPF.format("12345678909")
{:ok, "123.456.789-09"}

iex> BrDocs.CPF.format("123")
{:error, :invalid_format}

format!(cpf)

@spec format!(String.t()) :: formatted()

Formats a CPF, raising ArgumentError when the input is invalid.

Examples

iex> BrDocs.CPF.format!("12345678909")
"123.456.789-09"

unformat(cpf)

@spec unformat(String.t()) :: {:ok, normalized()} | {:error, :invalid_format}

Removes punctuation from a CPF.

Examples

iex> BrDocs.CPF.unformat("123.456.789-09")
{:ok, "12345678909"}

unformat!(cpf)

@spec unformat!(String.t()) :: normalized()

Removes punctuation from a CPF, raising on invalid input.

Examples

iex> BrDocs.CPF.unformat!("123.456.789-09")
"12345678909"

valid?(cpf)

@spec valid?(term()) :: boolean()

Returns true when the CPF format and check digits are valid.

Examples

iex> BrDocs.CPF.valid?("123.456.789-09")
true

iex> BrDocs.CPF.valid?("123.456.789-00")
false

iex> BrDocs.CPF.valid?("111.111.111-11")
false

valid_format?(cpf)

@spec valid_format?(term()) :: boolean()

Returns true when the CPF has a supported shape.

This checks length and allowed characters, but does not calculate check digits. Use valid?/1 for full validation.

Examples

iex> BrDocs.CPF.valid_format?("123.456.789-09")
true

iex> BrDocs.CPF.valid_format?("1234567890A")
false