feat: formatadores BRL, CPF e telefone
This commit is contained in:
parent
1339a9d16a
commit
b6cdc05404
30
src/lib/formatters.ts
Normal file
30
src/lib/formatters.ts
Normal 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, '')
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user