feat: formatadores BRL, CPF e telefone

This commit is contained in:
Rodribm10 2026-04-13 23:54:43 -03:00
parent 1339a9d16a
commit b6cdc05404

30
src/lib/formatters.ts Normal file
View File

@ -0,0 +1,30 @@
export function formatBRL(cents: number): string {
return (cents / 100).toLocaleString('pt-BR', {
style: 'currency',
currency: 'BRL',
})
}
export function maskCPF(value: string): string {
const digits = value.replace(/\D/g, '').slice(0, 11)
return digits
.replace(/(\d{3})(\d)/, '$1.$2')
.replace(/(\d{3})(\d)/, '$1.$2')
.replace(/(\d{3})(\d{1,2})$/, '$1-$2')
}
export function maskPhone(value: string): string {
const digits = value.replace(/\D/g, '').slice(0, 11)
if (digits.length <= 10) {
return digits
.replace(/(\d{2})(\d)/, '($1) $2')
.replace(/(\d{4})(\d)/, '$1-$2')
}
return digits
.replace(/(\d{2})(\d)/, '($1) $2')
.replace(/(\d{5})(\d)/, '$1-$2')
}
export function onlyDigits(value: string): string {
return value.replace(/\D/g, '')
}