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.
|
||||
|
||||
CPF/CNPJ: Retorna no CPF ou CNPJ de Acordo com o Tamanho da Entrada.
|
||||
Conta Bancária: 1234-5
|
||||
|
||||
### Exemplos
|
||||
|
||||
|
|
131
src/lhmask.js
131
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue