format static method

String format(
  1. String cpf, {
  2. bool anywhere = false,
})

Implementation

static String format(String cpf, {bool anywhere = false}) {
  if (anywhere == false && !isValid(cpf)) {
    throw AssertionError('CPF inválido!');
  }

  var regExp = RegExp(r'^(\d{3})(\d{3})(\d{3})(\d{2})$');

  return strip(cpf).replaceAllMapped(regExp, (Match m) => '${m[1]}.${m[2]}.${m[3]}-${m[4]}');
}