rafatoração de componentes de entrada

This commit is contained in:
Luiz Silva 2026-01-29 11:27:08 -03:00
parent 6aedf2469f
commit 27c9e4d5e2
45 changed files with 1295 additions and 2605 deletions

View file

@ -0,0 +1,10 @@
function somenteNumeros(valor: string) {
return valor.replace(/\D+/g, "");
}
/** Formata CEP no padrão 00000-000 */
export function formatarCep(valor: string) {
const digitos = somenteNumeros(valor);
if (!digitos) return "";
return digitos.replace(/^(\d{5})(\d)/, "$1-$2").slice(0, 9);
}

View file

@ -0,0 +1,24 @@
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);
}

View file

@ -0,0 +1,29 @@
/**
* Remove tudo que não é número
*/
export function sanitizeTelefone(value: string): string {
return value.replace(/\D+/g, "");
}
/**
* Aplica máscara dinâmica de telefone BR
*/
export function formatTelefone(value: string): string {
const digits = sanitizeTelefone(value);
if (!digits) return "";
// (99) 9999-9999
if (digits.length <= 10) {
return digits
.replace(/^(\d{2})(\d)/, "($1) $2")
.replace(/(\d{4})(\d)/, "$1-$2")
.slice(0, 14);
}
// (99) 99999-9999
return digits
.replace(/^(\d{2})(\d)/, "($1) $2")
.replace(/(\d{5})(\d)/, "$1-$2")
.slice(0, 15);
}