cpf masking

This commit is contained in:
José Henrique 2025-06-19 19:55:35 -03:00
parent 7acbc48f43
commit c347d5ce24

View File

@ -1,19 +1,15 @@
/** /**
* Formats a CPF string with masking (123.***.789-10) * Formats a CPF string (might include masking) and masks it for display.
* @param cpf - A CPF string (11 digits without punctuation) * @param cpf - A CPF string (11 digits without punctuation)
* @returns Formatted CPF with masking or the original input if invalid * @returns Formatted CPF with masking or the original input if invalid
*/ */
export function maskCpf(cpf: string): string { export function maskCpf(cpf: string): string {
// Clean input, keeping only digits
const cleanCpf = cpf.replace(/\D/g, '');
// Validate if it's 11 digits // Validate if it's 11 digits
if (cleanCpf.length !== 11) { if (cpf.length !== 11) {
return cpf; return cpf;
} }
// Mask with standard punctuation: 123.456.789-10
// Format with mask: 123.***.789-10 return `${cpf.slice(0, 3)}.${cpf.slice(3, 6)}.${cpf.slice(6, 9)}-${cpf.slice(9)}`;
return `${cleanCpf.slice(0, 3)}.***.${cleanCpf.slice(6, 9)}-${cleanCpf.slice(9)}`;
} }
/** /**