diff --git a/src/lib/formatters.ts b/src/lib/formatters.ts new file mode 100644 index 0000000..9143bcc --- /dev/null +++ b/src/lib/formatters.ts @@ -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, '') +}