feat: typescript

This commit is contained in:
Leandro Costa 2024-06-17 20:11:08 -03:00
parent 7d82ee0dd1
commit fed2ff4adf
10 changed files with 2822 additions and 3004 deletions

View file

@ -1,152 +0,0 @@
// 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);
if (soNumeros.length > 5)
cep += '-' + soNumeros.slice(5, 8);
return cep;
}
// Entrada: 12345678000100 Saida: 12.345.678/0001-00
function formatarCnpj(txt) {
const soNumeros = `${txt}`.replace(/[^\d]/g, '')
if (!soNumeros) return soNumeros;
let cnpj = '';
cnpj += soNumeros.slice(0, 2)
if (soNumeros.length > 2)
cnpj += '.' + soNumeros.slice(2, 5)
if (soNumeros.length > 5)
cnpj += '.' + soNumeros.slice(5, 8)
if (soNumeros.length > 8)
cnpj += '/' + soNumeros.slice(8, 12)
if (soNumeros.length > 12)
cnpj += '-' + soNumeros.slice(12, 14)
return cnpj
}
// Entrada: 12345678900 Saída: 123.456.789-00
function formatarCpf(txt) {
const soNumeros = `${txt}`.replace(/[^\d]/g, '')
if (!soNumeros) return soNumeros;
let cpf = '';
cpf += soNumeros.slice(0, 3)
if (soNumeros.length > 3)
cpf += '.' + soNumeros.slice(3, 6)
if (soNumeros.length > 6)
cpf += '.' + soNumeros.slice(6, 9)
if (soNumeros.length > 9)
cpf += '-' + soNumeros.slice(9, 11)
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 = `${txt}`.replace(/[^\dxX]/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;
}
function formatarTelefone(txt) {
const soNumeros = `${txt}`.replace(/[^\d]/g, '')
if (!soNumeros) return 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);
case 'cpf': return formatarCpf(valor);
case 'cnpj': return formatarCnpj(valor);
case 'cpfcnpj': return formatarCpfCnpj(valor);
case 'contabancaria': return formatarContaBancaria(valor);
case 'telefone': return formatarTelefone(valor);
case 'int': return parseInt(valor);
case 'float': return parseFloat(valor);
default:
return valor;
}
}
module.exports = {
formatarCep, formatarCnpj, formatarCpf, formatarCpfCnpj,
formatarContaBancaria, formatarTelefone,
formatarValor
}

View file

@ -1,50 +0,0 @@
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': [
['12345', '1234-5'],
['12345x', '12345-x'],
['12345X', '12345-X']
]
},
'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);
});
});

52
src/lhmask.test.ts Normal file
View file

@ -0,0 +1,52 @@
import * as lhmask from "./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: [
["12345", "1234-5"],
["12345x", "12345-x"],
["12345X", "12345-X"],
],
},
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: string) => {
const nomeFormato = formato as keyof typeof testes;
const teste = testes[nomeFormato];
const fnName = teste.fnName;
const valores = teste.valores;
test.each(valores.map((v) => [fnName, v[0], v[1]]))(`%s(%s) => %s)`, (fnName, v0, v1) => {
const formatFunction = lhmask[fnName as keyof typeof lhmask] as Function;
expect(formatFunction(v0)).toBe(v1);
expect(lhmask.formatarValor(v0, formato)).toBe(v1);
});
});

149
src/lhmask.ts Normal file
View file

@ -0,0 +1,149 @@
// Entrada: 12345000 Saída: 12.345-000
export function formatarCep(txt: string) {
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);
if (soNumeros.length > 5) cep += "-" + soNumeros.slice(5, 8);
return cep;
}
// Entrada: 12345678000100 Saida: 12.345.678/0001-00
export function formatarCnpj(txt: string) {
const soNumeros = `${txt}`.replace(/[^\d]/g, "");
if (!soNumeros) return soNumeros;
let cnpj = "";
cnpj += soNumeros.slice(0, 2);
if (soNumeros.length > 2) cnpj += "." + soNumeros.slice(2, 5);
if (soNumeros.length > 5) cnpj += "." + soNumeros.slice(5, 8);
if (soNumeros.length > 8) cnpj += "/" + soNumeros.slice(8, 12);
if (soNumeros.length > 12) cnpj += "-" + soNumeros.slice(12, 14);
return cnpj;
}
// Entrada: 12345678900 Saída: 123.456.789-00
export function formatarCpf(txt: string) {
const soNumeros = `${txt}`.replace(/[^\d]/g, "");
if (!soNumeros) return soNumeros;
let cpf = "";
cpf += soNumeros.slice(0, 3);
if (soNumeros.length > 3) cpf += "." + soNumeros.slice(3, 6);
if (soNumeros.length > 6) cpf += "." + soNumeros.slice(6, 9);
if (soNumeros.length > 9) cpf += "-" + soNumeros.slice(9, 11);
return cpf;
}
export function formatarCpfCnpj(txt: string) {
const soNumeros = `${txt}`.replace(/[^\d]/g, "");
if (soNumeros.length <= 11) {
return formatarCpf(txt);
} else {
return formatarCnpj(txt);
}
}
// Entrada: 12345 Saída: 1234-5
export function formatarContaBancaria(txt: string) {
const soNumeros = `${txt}`.replace(/[^\dxX]/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;
}
export function formatarTelefone(txt: string) {
const soNumeros = `${txt}`.replace(/[^\d]/g, "");
if (!soNumeros) return 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;
}
export function formatarValor(valor: string, formato: string) {
switch (formato) {
case "cep":
return formatarCep(valor);
case "cpf":
return formatarCpf(valor);
case "cnpj":
return formatarCnpj(valor);
case "cpfcnpj":
return formatarCpfCnpj(valor);
case "contabancaria":
return formatarContaBancaria(valor);
case "telefone":
return formatarTelefone(valor);
case "int":
return parseInt(valor);
case "float":
return parseFloat(valor);
default:
return valor;
}
}
export default {
formatarCep,
formatarCnpj,
formatarCpf,
formatarCpfCnpj,
formatarContaBancaria,
formatarTelefone,
formatarValor,
};