/* Center all the app's masks here, format them, improve them and add new ones. */ const formatBRL = (value: number) => { return new Intl.NumberFormat('pt-BR', { style: 'currency', currency: 'BRL', }).format(value) } function maskCep(value: string) { if (value == '') return '' return value .replace(/\D/g, '') .replace(/(\d{5})(\d)/, '$1-$2') .replace(/(-\d{3})\d+?$/, '$1') } function maskPhone(value: string) { return value .replace(/\D/g, '') .replace(/^(\d{2})(\d)/g, '($1) $2') .replace(/(\d)(\d{4})$/, '$1-$2') } function maskCNPJ(value: string) { return value .replace(/\D+/g, '') .replace(/(\d{2})(\d)/, '$1.$2') .replace(/(\d{3})(\d)/, '$1.$2') .replace(/(\d{3})(\d)/, '$1/$2') .replace(/(\d{4})(\d)/, '$1-$2') .replace(/(-\d{2})\d+?$/, '$1') } function maskCPF(value: string) { return value .replace(/\D+/g, '') .replace(/(\d{3})(\d)/, '$1.$2') .replace(/(\d{3})(\d)/, '$1.$2') .replace(/(\d{3})(\d)/, '$1-$2') .replace(/(-\d{2})\d+?$/, '$1') } //create a mask birthdate with format DD/MM/AAAA function maskBirthdate(value: string) { return value .replace(/\D+/g, '') .replace(/(\d{2})(\d)/, '$1/$2') .replace(/(\d{2})(\d)/, '$1/$2') } function removeBlankSpace(value: string) { return value.trim() } export type MaskProps = | 'cpf' | 'cep' | 'phone' | 'cnpj' | 'brl' | 'birthdate' | undefined function maskAnything(mask: MaskProps, text: string) { switch (mask) { case 'cpf': return maskCPF(text) case 'cep': return maskCep(text) case 'phone': return maskPhone(text) case 'cnpj': return maskCNPJ(text) case 'brl': return formatBRL(Number(text)) case 'birthdate': return maskBirthdate(text) default: return text } } export { formatBRL, maskCep, maskPhone, maskCNPJ, removeBlankSpace, maskAnything, }