validateCpf function exported ✓ 100.0%
Last updated: 2026-02-24T21:07:57.582Z
Location
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)
| Target | Type |
|---|---|
| calculateCpfCheckDigit | calls |
| cleaned | dynamic_call |
No incoming dependencies.