Separando funções para poder utilizar destructure na formatarCpfCnpj e não receber erro por causa do this
This commit is contained in:
parent
4f0a68d49a
commit
f3c96616b5
2 changed files with 69 additions and 63 deletions
|
@ -3,7 +3,6 @@
|
||||||
Biblioteca com funções para formatação de dados.
|
Biblioteca com funções para formatação de dados.
|
||||||
|
|
||||||
CPF/CNPJ: Retorna no CPF ou CNPJ de Acordo com o Tamanho da Entrada.
|
CPF/CNPJ: Retorna no CPF ou CNPJ de Acordo com o Tamanho da Entrada.
|
||||||
Conta Bancária: 1234-5
|
|
||||||
|
|
||||||
### Exemplos
|
### Exemplos
|
||||||
|
|
||||||
|
|
131
src/lhmask.js
131
src/lhmask.js
|
@ -1,80 +1,87 @@
|
||||||
module.exports = {
|
// Entrada: 12345000 Saída: 12.345-000
|
||||||
// Entrada: 12345000 Saída: 12.345-000
|
function formatarCep(txt) {
|
||||||
formatarCep(txt) {
|
const soNumeros = `${txt}`.replace(/[^\d]/g, '')
|
||||||
const soNumeros = `${txt}`.replace(/[^\d]/g, '')
|
if (!soNumeros) return soNumeros;
|
||||||
if (!soNumeros) return soNumeros;
|
|
||||||
|
|
||||||
let cep = '';
|
let cep = '';
|
||||||
cep += soNumeros.slice(0, 2);
|
cep += soNumeros.slice(0, 2);
|
||||||
if (soNumeros.length > 2)
|
if (soNumeros.length > 2)
|
||||||
cep += '.' + soNumeros.slice(2, 5);
|
cep += '.' + soNumeros.slice(2, 5);
|
||||||
|
|
||||||
if (soNumeros.length > 5)
|
if (soNumeros.length > 5)
|
||||||
cep += '-' + soNumeros.slice(5, 8);
|
cep += '-' + soNumeros.slice(5, 8);
|
||||||
|
|
||||||
return cep;
|
return cep;
|
||||||
},
|
}
|
||||||
// Entrada: 12345678000100 Saida: 12.345.678/0001-00
|
|
||||||
formatarCnpj(txt) {
|
|
||||||
const soNumeros = `${txt}`.replace(/[^\d]/g, '')
|
|
||||||
if (!soNumeros) return soNumeros;
|
|
||||||
|
|
||||||
let cnpj = '';
|
// Entrada: 12345678000100 Saida: 12.345.678/0001-00
|
||||||
cnpj += soNumeros.slice(0, 2)
|
function formatarCnpj(txt) {
|
||||||
|
const soNumeros = `${txt}`.replace(/[^\d]/g, '')
|
||||||
|
if (!soNumeros) return soNumeros;
|
||||||
|
|
||||||
if (soNumeros.length > 2)
|
let cnpj = '';
|
||||||
cnpj += '.' + soNumeros.slice(2, 5)
|
cnpj += soNumeros.slice(0, 2)
|
||||||
|
|
||||||
if (soNumeros.length > 5)
|
if (soNumeros.length > 2)
|
||||||
cnpj += '.' + soNumeros.slice(5, 8)
|
cnpj += '.' + soNumeros.slice(2, 5)
|
||||||
|
|
||||||
if (soNumeros.length > 8)
|
if (soNumeros.length > 5)
|
||||||
cnpj += '/' + soNumeros.slice(8, 12)
|
cnpj += '.' + soNumeros.slice(5, 8)
|
||||||
|
|
||||||
if (soNumeros.length > 12)
|
if (soNumeros.length > 8)
|
||||||
cnpj += '-' + soNumeros.slice(12, 14)
|
cnpj += '/' + soNumeros.slice(8, 12)
|
||||||
|
|
||||||
return cnpj
|
if (soNumeros.length > 12)
|
||||||
},
|
cnpj += '-' + soNumeros.slice(12, 14)
|
||||||
// Entrada: 12345678900 Saída: 123.456.789-00
|
|
||||||
formatarCpf(txt) {
|
|
||||||
const soNumeros = `${txt}`.replace(/[^\d]/g, '')
|
|
||||||
if (!soNumeros) return soNumeros;
|
|
||||||
|
|
||||||
let cpf = '';
|
return cnpj
|
||||||
cpf += soNumeros.slice(0, 3)
|
}
|
||||||
|
|
||||||
if (soNumeros.length > 3)
|
// Entrada: 12345678900 Saída: 123.456.789-00
|
||||||
cpf += '.' + soNumeros.slice(3, 6)
|
function formatarCpf(txt) {
|
||||||
|
const soNumeros = `${txt}`.replace(/[^\d]/g, '')
|
||||||
|
if (!soNumeros) return soNumeros;
|
||||||
|
|
||||||
if (soNumeros.length > 6)
|
let cpf = '';
|
||||||
cpf += '.' + soNumeros.slice(6, 9)
|
cpf += soNumeros.slice(0, 3)
|
||||||
|
|
||||||
if (soNumeros.length > 9)
|
if (soNumeros.length > 3)
|
||||||
cpf += '-' + soNumeros.slice(9, 11)
|
cpf += '.' + soNumeros.slice(3, 6)
|
||||||
|
|
||||||
return cpf;
|
if (soNumeros.length > 6)
|
||||||
},
|
cpf += '.' + soNumeros.slice(6, 9)
|
||||||
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;
|
|
||||||
|
|
||||||
let conta = '';
|
if (soNumeros.length > 9)
|
||||||
if (soNumeros.length > 1) {
|
cpf += '-' + soNumeros.slice(9, 11)
|
||||||
conta = soNumeros.slice(0, soNumeros.length - 1) + '-' + soNumeros[soNumeros.length - 1];
|
|
||||||
} else {
|
|
||||||
conta = soNumeros;
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue