Utilities for the Brazilian CPF (Cadastro de Pessoas Físicas).
A CPF is an 11-digit identifier assigned to individuals: a 9-digit
base number followed by 2 check digits. It is commonly displayed as
"XXX.XXX.XXX-XX".
cpf() = <<_:88>>
A raw CPF: 11 ASCII digits, e.g. <<"82178537464">>.
formatted_cpf() = <<_:112>>
A display-formatted CPF, e.g. <<"821.785.374-64">>.
| format/1 | Formats a valid CPF for display, adding the standard visual
aid symbols: <<"XXX.XXX.XXX-XX">>. |
| generate/0 | Generates a random valid CPF as a raw, numbers-only binary. |
| is_valid/1 | Returns whether the given term is a valid CPF: a binary of
exactly 11 ASCII digits, not a sequence of one repeated digit
(<<"00000000000">> and the like are reserved and always invalid),
whose two check digits match its 9-digit base number. |
| remove_symbols/1 | Removes the formatting symbols . and - from a CPF string. |
format(Cpf::binary()) -> {ok, formatted_cpf()} | {error, invalid}
Formats a valid CPF for display, adding the standard visual
aid symbols: <<"XXX.XXX.XXX-XX">>.
The input must be a raw, numbers-only CPF accepted by
is_valid/1; anything else yields {error, invalid}.
1> brutils_cpf:format(<<"82178537464">>).
{ok,<<"821.785.374-64">>}
2> brutils_cpf:format(<<"11111111111">>).
{error,invalid}
generate() -> cpf()
Generates a random valid CPF as a raw, numbers-only binary.
The 9-digit base is drawn uniformly (never the reserved all-zeros
base) and the two check digits are computed from it, so the result
always satisfies is_valid/1.
1> brutils_cpf:generate(). <<"10895948109">>
is_valid(Cpf::term()) -> boolean()
Returns whether the given term is a valid CPF: a binary of
exactly 11 ASCII digits, not a sequence of one repeated digit
(<<"00000000000">> and the like are reserved and always invalid),
whose two check digits match its 9-digit base number.
Only the format is verified — the CPF is not checked for existence.
Formatting symbols are not stripped, so a formatted CPF such as
<<"821.785.374-64">> is invalid; clean it with
remove_symbols/1 first. The function is total: any
non-binary term returns false rather than raising.
1> brutils_cpf:is_valid(<<"82178537464">>). true 2> brutils_cpf:is_valid(<<"82178537460">>). false
remove_symbols(Cpf::binary()) -> binary()
Removes the formatting symbols . and - from a CPF string.
Only those two characters are removed; any other character (letters, spaces, slashes, ...) is kept unchanged. This is a pure character filter: it does not validate the input.
1> brutils_cpf:remove_symbols(<<"123.456.789-01">>). <<"12345678901">> 2> brutils_cpf:remove_symbols(<<"abc//1">>). <<"abc//1">>
Generated by EDoc