Implementação do FormatarTelefone e Testes Unitários

This commit is contained in:
Leandro Costa 2022-02-05 11:54:09 -03:00
parent 9a69ecd1b3
commit f6733150e6
5 changed files with 7068 additions and 4 deletions

View file

@ -21,6 +21,10 @@ console.log(formatarCpfCnpj('12345678900')); // Saída: 123.456.789-00
console.log(formatarContaBancaria('12345')); // Saída: 1234-5
console.log(formatarTelefone('88912341234')); // Saída: (88)91234-1234.
console.log(formatarTelefone('8834001234')); // Saída: (88)3400-1234.
console.log(formatarTelefone('08001234567')); // Saída: 0800-123-4567
// Retorna no formado especificado pela string.
// possíveis valores: cep, cpf, cnpj, cpfcnpj, contabancaria.
console.log(formatarValor("12345", "contabancaria")); // Saída: 1234-5

6960
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -4,7 +4,7 @@
"description": "Biblioteca com funções para formatação de dados.",
"main": "src/lhmask.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest --watchAll"
},
"repository": {
"type": "git",
@ -16,5 +16,8 @@
"bugs": {
"url": "https://bitbucket.org/leandro_costa/lhmask/issues"
},
"homepage": "https://bitbucket.org/leandro_costa/lhmask#readme"
}
"homepage": "https://bitbucket.org/leandro_costa/lhmask#readme",
"devDependencies": {
"jest": "^27.5.0"
}
}

View file

@ -81,6 +81,55 @@ function formatarContaBancaria(txt) {
return conta;
}
function formatarTelefone(txt) {
const soNumeros = `${txt}`.replace(/[^\d]/g, '')
if (!soNumeros) onChange(soNumeros);
let tel = '';
if (soNumeros.length === 1) {
if (+soNumeros[0] === 0) {
tel = soNumeros;
} else {
tel = '(' + soNumeros;
}
} else if (soNumeros.length === 2) {
if (+soNumeros[0] === 0 && +soNumeros[1] === 8) {
tel = soNumeros;
} else {
tel = '(' + soNumeros;
}
} else if (soNumeros.length > 2) {
if (+soNumeros[2] === 9) { // CELULAR
tel = '(' + soNumeros.slice(0, 2) + ')';
if (soNumeros.length > 2)
tel += soNumeros.slice(2, 7);
if (soNumeros.length > 7)
tel += '-' + soNumeros.slice(7, 11);
} else { // TELEFONE FIXO OU 0800
if (+soNumeros[0] === 0 && +soNumeros[1] === 8) { // 0800
tel = soNumeros.slice(0, 4);
if (soNumeros.length > 4)
tel += '-' + soNumeros.slice(4, 7);
if (soNumeros.length > 7)
tel += '-' + soNumeros.slice(7, 11);
} else {
tel = '(' + soNumeros.slice(0, 2) + ')';
if (soNumeros.length > 2)
tel += soNumeros.slice(2, 6);
if (soNumeros.length > 6)
tel += '-' + soNumeros.slice(6, 10);
}
}
}
return tel;
}
function formatarValor(valor, formato) {
switch (formato) {
case 'cep': return formatarCep(valor);
@ -88,6 +137,7 @@ function formatarValor(valor, formato) {
case 'cnpj': return formatarCnpj(valor);
case 'cpfcnpj': return formatarCpfCnpj(valor);
case 'contabancaria': return formatarContaBancaria(valor);
case 'telefone': return formatarTelefone(valor);
default:
return valor;
}
@ -95,5 +145,6 @@ function formatarValor(valor, formato) {
module.exports = {
formatarCep, formatarCnpj, formatarCpf, formatarCpfCnpj,
formatarContaBancaria, formatarValor
formatarContaBancaria, formatarTelefone,
formatarValor
}

46
src/lhmask.test.js Normal file
View file

@ -0,0 +1,46 @@
const lhmask = require("./lhmask");
const testes = {
"cep": {
'fnName': 'formatarCep',
'valores': [["62800000", "62.800-000"]]
},
"cnpj": {
'fnName': 'formatarCnpj',
'valores': [["12123456000100", "12.123.456/0001-00"]]
},
"cpf": {
'fnName': "formatarCpf",
'valores': [['12345678900', '123.456.789-00']]
},
"cpfcnpj": {
'fnName': 'formatarCpfCnpj',
'valores': [
["12123456000100", "12.123.456/0001-00"],
['12345678900', '123.456.789-00']
]
},
'contabancaria': {
'fnName': "formatarContaBancaria",
'valores': [['1234-5', '1234-5']]
},
'telefone': {
'fnName': 'formatarTelefone',
'valores': [
["88912341234", "(88)91234-1234"],
["8834001234", "(88)3400-1234"],
['08001234567', '0800-123-4567']
]
}
}
describe.each(Object.keys(testes).map(k => [k]))(`%s`, (formato) => {
const fnName = testes[formato].fnName;
const valores = testes[formato].valores;
test.each(valores.map(v => [fnName, v[0], v[1]]))(`%s(%s) => %s)`, (fnName, v0, v1) => {
expect(lhmask[fnName](v0)).toBe(v1);
expect(lhmask.formatarValor(v0, formato)).toBe(v1);
});
});