Module brutils_passport

Utilities for Brazilian passport numbers.

Description

Utilities for Brazilian passport numbers.

A passport number has 8 characters: 2 uppercase letters followed by 6 digits (e.g. <<"AB123456">>). There is no check digit — only the shape is verified, never existence.

All functions operate on UTF-8 binaries.

Data Types

passport()

passport() = <<_:64>>

A passport number: 2 uppercase ASCII letters + 6 ASCII digits.

Function Index

format/1Normalizes and formats a passport number for display: uppercases ASCII letters and strips the symbols -, . and spaces, then validates the result.
generate/0Generates a random valid passport number: 2 uniform uppercase letters followed by 6 uniform digits.
is_valid/1Returns whether the given term is a valid passport number: exactly 2 uppercase ASCII letters followed by exactly 6 digits.
remove_symbols/1Removes the symbols -, . and spaces from a passport string.

Function Details

format/1

format(Passport::binary()) -> {ok, passport()} | {error, invalid}

Normalizes and formats a passport number for display: uppercases ASCII letters and strips the symbols -, . and spaces, then validates the result.

This is the lenient counterpart to the strict is_valid/1: format(<<"ab-123456">>) succeeds where is_valid(<<"ab-123456">>) is false. Input that does not normalize into a valid passport — including non-ASCII letters — yields {error, invalid}.

  1> brutils_passport:format(<<"ab-123456">>).
  {ok,<<"AB123456">>}
  2> brutils_passport:format(<<"111111">>).
  {error,invalid}

generate/0

generate() -> passport()

Generates a random valid passport number: 2 uniform uppercase letters followed by 6 uniform digits.

With no check digit to compute, every output is valid by construction.

  1> brutils_passport:generate().
  <<"HA029151">>

is_valid/1

is_valid(X1::term()) -> boolean()

Returns whether the given term is a valid passport number: exactly 2 uppercase ASCII letters followed by exactly 6 digits.

The check is case-sensitive — lowercase letters are invalid — and no symbols are stripped; use format/1 to normalize dressed or lowercase input first. (The reference implementation's docstring claims mixed case is accepted; its code rejects it, and this port follows the code.) There is no check digit, so any shape-conforming value is valid — existence is never verified. The function is total: any non-binary term returns false rather than raising.

  1> brutils_passport:is_valid(<<"AB123456">>).
  true
  2> brutils_passport:is_valid(<<"Ab123456">>).
  false

remove_symbols/1

remove_symbols(Passport::binary()) -> binary()

Removes the symbols -, . and spaces from a passport string.

Only those three characters are removed; anything else — including underscores and slashes — is kept unchanged, and letter case is preserved (uppercasing is format/1's job). This is a pure character filter: it does not validate the input.

  1> brutils_passport:remove_symbols(<<"Ab -. 123456">>).
  <<"Ab123456">>
  2> brutils_passport:remove_symbols(<<"ab_123/456">>).
  <<"ab_123/456">>


Generated by EDoc