validateCpf function exported ✓ 100.0%

Last updated: 2026-02-24T21:07:57.582Z

Metrics

LOC: 20 Complexity: 4 Params: 1 Coverage: 100.0% (8/8 lines, 57x executed)

Signature

validateCpf(cpf: string): : boolean

Summary

Validates a Brazilian CPF string.

Tags

#@param cpf - CPF string (formatted or raw digits)#@returns `true` if the CPF has valid check digits

Source Code

export function validateCpf(cpf: string): boolean {
  const cleaned = cpf.replace(/\D/g, "");

  if (cleaned.length !== 11) return false;
  if (/^(\d)\1{10}$/.test(cleaned)) return false;

  const digits = cleaned.split("").map(Number);

  const first = calculateCpfCheckDigit(
    digits.slice(0, 9),
    [10, 9, 8, 7, 6, 5, 4, 3, 2],
  );
  if (first !== digits[9]) return false;

  const second = calculateCpfCheckDigit(
    digits.slice(0, 10),
    [11, 10, 9, 8, 7, 6, 5, 4, 3, 2],
  );
  return second === digits[10];
}

Dependencies (Outgoing)

graph LR validateCpf["validateCpf"] calculateCpfCheckDigit["calculateCpfCheckDigit"] validateCpf -->|calls| calculateCpfCheckDigit style validateCpf fill:#dbeafe,stroke:#2563eb,stroke-width:2px click validateCpf "c2de7b7f656d2bcf.html" click calculateCpfCheckDigit "87cd58e187724918.html"
TargetType
calculateCpfCheckDigit calls
cleaned dynamic_call

No incoming dependencies.