24 lines
567 B
TypeScript
24 lines
567 B
TypeScript
function somenteNumeros(v: string): string {
|
|
return v.replace(/\D+/g, "");
|
|
}
|
|
|
|
export function formatarCpfCnpj(v: string): string {
|
|
const d = somenteNumeros(v);
|
|
|
|
// CPF
|
|
if (d.length <= 11) {
|
|
return d
|
|
.replace(/(\d{3})(\d)/, "$1.$2")
|
|
.replace(/(\d{3})(\d)/, "$1.$2")
|
|
.replace(/(\d{3})(\d{1,2})$/, "$1-$2")
|
|
.slice(0, 14);
|
|
}
|
|
|
|
// CNPJ
|
|
return d
|
|
.replace(/^(\d{2})(\d)/, "$1.$2")
|
|
.replace(/^(\d{2})\.(\d{3})(\d)/, "$1.$2.$3")
|
|
.replace(/\.(\d{3})(\d)/, ".$1/$2")
|
|
.replace(/(\d{4})(\d)/, "$1-$2")
|
|
.slice(0, 18);
|
|
}
|