From f3c96616b5bcebe97b3c0ad779f601ed936956d5 Mon Sep 17 00:00:00 2001 From: Leandro Costa Date: Wed, 2 Feb 2022 11:38:46 -0300 Subject: [PATCH] =?UTF-8?q?Separando=20fun=C3=A7=C3=B5es=20para=20poder=20?= =?UTF-8?q?utilizar=20destructure=20na=20formatarCpfCnpj=20e=20n=C3=A3o=20?= =?UTF-8?q?receber=20erro=20por=20causa=20do=20this?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 - src/lhmask.js | 131 ++++++++++++++++++++++++++------------------------ 2 files changed, 69 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 28fcf57..925aa9e 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,6 @@ Biblioteca com funções para formatação de dados. CPF/CNPJ: Retorna no CPF ou CNPJ de Acordo com o Tamanho da Entrada. -Conta Bancária: 1234-5 ### Exemplos diff --git a/src/lhmask.js b/src/lhmask.js index 282b73f..2ee648d 100644 --- a/src/lhmask.js +++ b/src/lhmask.js @@ -1,80 +1,87 @@ -module.exports = { - // Entrada: 12345000 Saída: 12.345-000 - formatarCep(txt) { - const soNumeros = `${txt}`.replace(/[^\d]/g, '') - if (!soNumeros) return soNumeros; +// Entrada: 12345000 Saída: 12.345-000 +function formatarCep(txt) { + const soNumeros = `${txt}`.replace(/[^\d]/g, '') + if (!soNumeros) return soNumeros; - let cep = ''; - cep += soNumeros.slice(0, 2); - if (soNumeros.length > 2) - cep += '.' + soNumeros.slice(2, 5); + let cep = ''; + cep += soNumeros.slice(0, 2); + if (soNumeros.length > 2) + cep += '.' + soNumeros.slice(2, 5); - if (soNumeros.length > 5) - cep += '-' + soNumeros.slice(5, 8); + if (soNumeros.length > 5) + cep += '-' + soNumeros.slice(5, 8); - return cep; - }, - // Entrada: 12345678000100 Saida: 12.345.678/0001-00 - formatarCnpj(txt) { - const soNumeros = `${txt}`.replace(/[^\d]/g, '') - if (!soNumeros) return soNumeros; + return cep; +} - let cnpj = ''; - cnpj += soNumeros.slice(0, 2) +// Entrada: 12345678000100 Saida: 12.345.678/0001-00 +function formatarCnpj(txt) { + const soNumeros = `${txt}`.replace(/[^\d]/g, '') + if (!soNumeros) return soNumeros; - if (soNumeros.length > 2) - cnpj += '.' + soNumeros.slice(2, 5) + let cnpj = ''; + cnpj += soNumeros.slice(0, 2) - if (soNumeros.length > 5) - cnpj += '.' + soNumeros.slice(5, 8) + if (soNumeros.length > 2) + cnpj += '.' + soNumeros.slice(2, 5) - if (soNumeros.length > 8) - cnpj += '/' + soNumeros.slice(8, 12) + if (soNumeros.length > 5) + cnpj += '.' + soNumeros.slice(5, 8) - if (soNumeros.length > 12) - cnpj += '-' + soNumeros.slice(12, 14) + if (soNumeros.length > 8) + cnpj += '/' + soNumeros.slice(8, 12) - return cnpj - }, - // Entrada: 12345678900 Saída: 123.456.789-00 - formatarCpf(txt) { - const soNumeros = `${txt}`.replace(/[^\d]/g, '') - if (!soNumeros) return soNumeros; + if (soNumeros.length > 12) + cnpj += '-' + soNumeros.slice(12, 14) - let cpf = ''; - cpf += soNumeros.slice(0, 3) + return cnpj +} - if (soNumeros.length > 3) - cpf += '.' + soNumeros.slice(3, 6) +// Entrada: 12345678900 Saída: 123.456.789-00 +function formatarCpf(txt) { + const soNumeros = `${txt}`.replace(/[^\d]/g, '') + if (!soNumeros) return soNumeros; - if (soNumeros.length > 6) - cpf += '.' + soNumeros.slice(6, 9) + let cpf = ''; + cpf += soNumeros.slice(0, 3) - if (soNumeros.length > 9) - cpf += '-' + soNumeros.slice(9, 11) + if (soNumeros.length > 3) + cpf += '.' + soNumeros.slice(3, 6) - return cpf; - }, - formatarCpfCnpj(txt) { - const soNumeros = `${txt}`.replace(/[^\d]/g, '') - if (soNumeros.length <= 11) { - return this.formatarCpf(txt); - } else { - return this.formatarCnpj(txt); - } - }, - // Entrada: 12345 Saída: 1234-5 - formatarContaBancaria(txt) { - const soNumeros = e.target.value.replace(/[^\d]/g, '') - if (!soNumeros) return soNumeros; + if (soNumeros.length > 6) + cpf += '.' + soNumeros.slice(6, 9) - let conta = ''; - if (soNumeros.length > 1) { - conta = soNumeros.slice(0, soNumeros.length - 1) + '-' + soNumeros[soNumeros.length - 1]; - } else { - conta = soNumeros; - } + if (soNumeros.length > 9) + cpf += '-' + soNumeros.slice(9, 11) - return conta; + return cpf; +} + +function formatarCpfCnpj(txt) { + const soNumeros = `${txt}`.replace(/[^\d]/g, '') + if (soNumeros.length <= 11) { + return formatarCpf(txt); + } else { + return formatarCnpj(txt); } +} + +// Entrada: 12345 Saída: 1234-5 +function formatarContaBancaria(txt) { + const soNumeros = e.target.value.replace(/[^\d]/g, '') + if (!soNumeros) return soNumeros; + + let conta = ''; + if (soNumeros.length > 1) { + conta = soNumeros.slice(0, soNumeros.length - 1) + '-' + soNumeros[soNumeros.length - 1]; + } else { + conta = soNumeros; + } + + return conta; +} + +module.exports = { + formatarCep, formatarCnpj, formatarCpf, formatarCpfCnpj, + formatarContaBancaria } \ No newline at end of file