riseup-squad20/susconecta/lib/formatters.ts

27 lines
768 B
TypeScript

export const formatCPF = (cpf: string): string => {
const cleaned = cpf.replace(/\D/g, "");
const match = cleaned.match(/^(\d{3})(\d{3})(\d{3})(\d{2})$/);
if (match) {
return `${match[1]}.${match[2]}.${match[3]}-${match[4]}`;
}
return cpf;
};
export const formatCelular = (celular: string): string => {
const cleaned = celular.replace(/\D/g, "");
const match = cleaned.match(/^(\d{2})(\d{5})(\d{4})$/);
if (match) {
return `(${match[1]}) ${match[2]}-${match[3]}`;
}
return celular;
};
export const formatRG = (rg: string): string => {
const cleaned = rg.replace(/\D/g, "");
const match = cleaned.match(/^(\d{2})(\d{3})(\d{3})(\d{1})$/);
if (match) {
return `${match[1]}.${match[2]}.${match[3]}-${match[4]}`;
}
return rg;
};