formatCpf static method

String formatCpf(
  1. String cpf
)

@formatador_utils

Formata CPF com máscara (XXX.XXX.XXX-XX)

Exemplo de entrada:

formatCpf('12345678901')
formatCpf('123.456.789-01') // aceita já formatado

Exemplo de saída:

'123.456.789-01'
'123.456.789-01'

cpf - CPF sem formatação ou já formatado

Throws ArgumentError se o CPF não tiver 11 dígitos após limpeza

Implementation

static String formatCpf(String cpf) {
  final cleanCpf = ValidacoesUtils.cleanDocumentNumber(cpf);

  if (cleanCpf.length != ValidacoesUtils.tamanhoCpf) {
    throw ArgumentError(
      'CPF deve ter ${ValidacoesUtils.tamanhoCpf} dígitos. Recebido: $cleanCpf',
    );
  }

  return '${cleanCpf.substring(0, 3)}.${cleanCpf.substring(3, 6)}.${cleanCpf.substring(6, 9)}-${cleanCpf.substring(9)}';
}