Skip to content

Commit

Permalink
#1
Browse files Browse the repository at this point in the history
  • Loading branch information
mariohmol committed Nov 18, 2019
1 parent b9a5854 commit c14f3e0
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 85 deletions.
3 changes: 2 additions & 1 deletion addons/pessoas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const TELEFONE_ESTADO = {
ma: 98, mt: 65, ms: 84, mg: 31, pr: 41, pb: 83, pa: 91, pe: 81, pi: 86, rj: 21, rn: 84, rs: 51, ro: 69,
rr: 95, sc: 48, se: 79, sp: 11, to: 63
}
// 154871-843
export const CEP_ESTADO = {
ac: [[69900000, 69999999]],
al: [[57000000, 57999999]],
Expand All @@ -40,7 +41,7 @@ export const CEP_ESTADO = {
rs: [[90000000, 99999999]],
sc: [[88000000, 89999999]],
se: [[49000000, 49999999]],
sp: [[1000000, 19999999]],
sp: [[ 1000000, 19999999]],
to: [[77000000, 77999999]]
}

Expand Down
22 changes: 11 additions & 11 deletions demo/js-brasil.js
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,7 @@ exports.fakerBr = {
}
return parseFloat(x.toFixed(options.decimals));
},
percentage: makeGeneric(mask_1.MASKS['percentage']),
porcentagem: makeGeneric(mask_1.MASKS['porcentagem']),
pessoa: function (options) {
if (options === void 0) { options = {}; }
var faker = _this.fakerBr;
Expand Down Expand Up @@ -2568,7 +2568,7 @@ exports.MASKS = {
suffix: ''
})
},
percentage: {
porcentagem: {
text: '00,00%',
textMask: createNumberMask_1.default({
decimalLimit: 2,
Expand Down Expand Up @@ -2721,14 +2721,14 @@ exports.maskBr = {
}
return conformToMask(numberValue, mask, { guide: false }).conformedValue + ',' + decimals;
},
percentage: function (percentageValue) {
if (!percentageValue) {
porcentagem: function (porcentagemValue) {
if (!porcentagemValue) {
return '';
}
var vals = percentageValue.split(',');
var mask = exports.MASKS.percentage.textMask(vals[0]);
var vals = porcentagemValue.split(',');
var mask = exports.MASKS.porcentagem.textMask(vals[0]);
var decimals = vals.length > 1 ? vals[1] : '00';
return conformToMask(percentageValue, mask, { guide: false }).conformedValue + ',' + decimals;
return conformToMask(porcentagemValue, mask, { guide: false }).conformedValue + ',' + decimals;
},
pispasep: makeGeneric('pispasep'),
placa: makeGeneric('placa'),
Expand Down Expand Up @@ -4046,11 +4046,11 @@ function validate_number(number) {
return regex.test(number);
}
exports.validate_number = validate_number;
function validate_percentage(percentage) {
function validate_porcentagem(porcentagem) {
var regex = /^\d+(?:\.\d{0,2})$/;
return regex.test(percentage);
return regex.test(porcentagem);
}
exports.validate_percentage = validate_percentage;
exports.validate_porcentagem = validate_porcentagem;
function validate_processo(processo) {
var processoClean = processo.replace(/\./g, '');
processoClean = processoClean.replace(/\-/g, '');
Expand Down Expand Up @@ -4217,7 +4217,7 @@ exports.validateBr = {
inscricaoestadual: inscricaoestadual_1.validate_inscricaoestadual,
iptu: validate_iptu,
number: validate_number,
percentage: validate_percentage,
porcentagem: validate_porcentagem,
pispasep: validate_pispasep,
placa: placa_1.validate_placa,
processo: validate_processo,
Expand Down
22 changes: 11 additions & 11 deletions demo/others.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
TIME
-->

<body data-id="percentage">
<body data-id="porcentagem">
<header></header>

<main class="container">
Expand All @@ -33,20 +33,20 @@ <h1>Gerador e Validador Tipos de Dados Brasileiros </h1>
<div class="card">
<div class="card-content">
<span class="card-title activator grey-text text-darken-4">Validar Porcentagem</span>
<input id="validatedpercentage" type="text" class="validate">
<label for="validatedpercentage">Digite o Porcentagem</label>
<input id="validatedporcentagem" type="text" class="validate">
<label for="validatedporcentagem">Digite o Porcentagem</label>

<ul id="validatedResultpercentage" class=" hidden">
<ul id="validatedResultporcentagem" class=" hidden">
<li>
<div class="collapsible-header">
<i class="material-icons" id="valiconpercentage">filter_drama</i>
<span id="valtextpercentage"></span>
<i class="material-icons" id="valiconporcentagem">filter_drama</i>
<span id="valtextporcentagem"></span>
<!-- <span class="new badge">4</span></div> -->
</li>
</ul>
</div>
<div class="card-action">
<a class="waves-effect waves-light btn" id="validatedActionpercentage"><i
<a class="waves-effect waves-light btn" id="validatedActionporcentagem"><i
class="material-icons right">check</i>validar</a>
</div>
</div>
Expand All @@ -56,11 +56,11 @@ <h1>Gerador e Validador Tipos de Dados Brasileiros </h1>
<div class="card">
<div class="card-content">
<span class="card-title activator grey-text text-darken-4">Gerar Porcentagem</span>
<input id="generatedpercentage" type="text" class="validate">
<label for="generatedpercentage"></label>
<input id="generatedporcentagem" type="text" class="validate">
<label for="generatedporcentagem"></label>
</div>
<div class="card-action">
<a class="waves-effect waves-light btn" id="generatedActionpercentage"><i
<a class="waves-effect waves-light btn" id="generatedActionporcentagem"><i
class="material-icons right">add</i>gerar
novo</a>
</div>
Expand Down Expand Up @@ -252,7 +252,7 @@ <h5 class="white-text">Referências</h5>
makeHeader();
generalAction('currency', 'currency');
generalAction('time', 'time');
generalAction('percentage', 'percentage');
generalAction('porcentagem', 'porcentagem');
}

</script>
Expand Down
32 changes: 18 additions & 14 deletions src/faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ export const fakerBr = {
return faker.telefone({ ...options, celular: true });
},
cep: (options: any = {}) => {
if(!options.estado){
if (!options.estado) {
options.estado = randomEstadoSigla();
}

const range = CEP_ESTADO[options.estado];
let cep = randomNumber(range[0][0], range[0][1]);
if(cep<10000000){
if (cep < 10000000) {
cep = '0' + cep.toString();
}else{
} else {
cep = cep.toString();
}
const mask = cep.slice(0, cep.length - 3) + '-' + cep.slice(cep.length-3, cep.length);

const mask = cep.slice(0, cep.length - 3) + '-' + cep.slice(cep.length - 3, cep.length);
return mask;
},
cepState: (state: string | number) => {
Expand Down Expand Up @@ -195,7 +195,7 @@ export const fakerBr = {
empresa: (options: any = {}) => {
const faker = this.fakerBr;
const cnpj = faker.cnpj();
if(!options.estado){
if (!options.estado) {
options.estado = randomEstadoSigla();
}
const endereco = faker.endereco(options);
Expand Down Expand Up @@ -236,11 +236,11 @@ export const fakerBr = {
const fakerBr = this.fakerBr;
const cep = fakerBr.cep();

if(!options.estado){
if (!options.estado) {
options.estado = randomEstadoSigla();
}
const estadoFound = LOCALIZACAO_ESTADOS.find(e=>e.uf.toLowerCase() === options.estado);
const cidades = LOCALIZACAO_CIDADES.filter(c=>c[1] === estadoFound.nome);
const estadoFound = LOCALIZACAO_ESTADOS.find(e => e.uf.toLowerCase() === options.estado);
const cidades = LOCALIZACAO_CIDADES.filter(c => c[1] === estadoFound.nome);
const cidade = randArray(cidades);
let estado = cidade[1].toLowerCase();
estado = LOCALIZACAO_ESTADOS.find(e => e.nome.toLowerCase() === estado.toLowerCase())
Expand Down Expand Up @@ -281,10 +281,10 @@ export const fakerBr = {
}
return parseFloat(x.toFixed(options.decimals));
},
percentage: makeGeneric(MASKS['percentage']),
porcentagem: makeGeneric(MASKS['porcentagem']),
pessoa: (options: any = {}) => {
const faker = this.fakerBr;
if(!options.estado){
if (!options.estado) {
options.estado = randomEstadoSigla();
}
const cpf = faker.cpf();
Expand Down Expand Up @@ -343,7 +343,7 @@ export const fakerBr = {
return renavam.substr(0, renavam.length - 1) + dv;
},
rg: (options: any = {}) => {
if(!options.estado){
if (!options.estado) {
options.estado = randomEstadoSigla();
}
const estado = options.estado.split('');
Expand Down Expand Up @@ -421,8 +421,12 @@ export const fakerBr = {
numbers[numbers.length - 1] = '8';
number = numbers.join();
}
const dig = create_titulo(number);
titulo = number + dig[0] + dig[1];
try {
const dig = create_titulo(number);
titulo = number + dig[0] + dig[1];
} catch (e) {

}
} while (!validate_titulo(titulo));
return titulo;
},
Expand Down
16 changes: 8 additions & 8 deletions src/mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const MASKS = {
suffix: ''
})
},
percentage: {
porcentagem: {
text: '00,00%',
textMask: createNumberMask({
decimalLimit: 2,
Expand Down Expand Up @@ -152,8 +152,8 @@ export const MASKS = {
textMask: [/\d/, /\d/, /\d/, /\d/, '.', /\d/, /\d/, /\d/, /\d/, '.', /[0-2]/, /[0-9]/, /\d/, /\d/]
},
telefone: {
text: '(00) 0000-0000',
textMask: ['(', /[1-9]/, /\d/, ')', ' ', /[1-9]/, /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/],
text: '(00) 00000-0000',
textMask: ['(', /[1-9]/, /\d/, ')', ' ', /[1-9]/, /\d/, /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/],
textMaskFunction: function mask(userInput: any) {
const numbers = userInput.match(/\d/g);
let numberLength = 0;
Expand Down Expand Up @@ -281,16 +281,16 @@ export const maskBr = {
{ guide: false }
).conformedValue + ',' + decimals;
},
percentage: (percentageValue: string) => {
if (!percentageValue) {
porcentagem: (porcentagemValue: string) => {
if (!porcentagemValue) {
return '';
}
const vals = percentageValue.split(',');
const mask = MASKS.percentage.textMask(vals[0]);
const vals = porcentagemValue.split(',');
const mask = MASKS.porcentagem.textMask(vals[0]);
const decimals = vals.length > 1 ? vals[1] : '00';

return conformToMask(
percentageValue,
porcentagemValue,
mask,
{ guide: false }
).conformedValue + ',' + decimals;
Expand Down
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ export function rand(length: number, ...ranges: any[]) {
}

export function randomNumber(begin, end) {
const number = Math.floor(Math.random() * end) + begin;
const dif = end - begin;
const number = Math.floor(Math.random() * dif) + begin;
return number;
}

Expand Down
6 changes: 3 additions & 3 deletions src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ export function validate_number(number: string) {
}


export function validate_percentage(percentage: string) {
export function validate_porcentagem(porcentagem: string) {
const regex = /^\d+(?:\.\d{0,2})$/;
return regex.test(percentage);
return regex.test(porcentagem);
}


Expand Down Expand Up @@ -470,7 +470,7 @@ export const validateBr = {
inscricaoestadual: validate_inscricaoestadual,
iptu: validate_iptu,
number: validate_number,
percentage: validate_percentage,
porcentagem: validate_porcentagem,
pispasep: validate_pispasep,
placa: validate_placa,
processo: validate_processo,
Expand Down
10 changes: 5 additions & 5 deletions test/faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('Faker test', () => {
expect(validateBr.email(email)).to.be.true;
});

it.only('Empresa', () => {
it('Empresa', () => {
const empresa = fakerBr.empresa();
expect(empresa.nome).to.exist;
expect(validateBr.cnpj(empresa.cnpj)).to.be.true;
Expand Down Expand Up @@ -150,10 +150,10 @@ describe('Faker test', () => {
});


it('Percentage - TODO', () => {
// expect(fakerBr.percentage()).to.exist;
// const percentage = fakerBr.percentage();
// expect(validateBr.percentage(percentage)).to.be.true;
it('Porcentagem - TODO', () => {
// expect(fakerBr.porcentagem()).to.exist;
// const porcentagem = fakerBr.porcentagem();
// expect(validateBr.porcentagem(porcentagem)).to.be.true;
});

it('Pessoa', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
document.querySelector('#fakercpf').innerHTML = jsbrasil.fakerBr.cpf();
// document.querySelector('#fakercurrency').innerHTML = jsbrasil.fakerBr.currency();
document.querySelector('#fakerinscricaoestadual').innerHTML = jsbrasil.fakerBr.inscricaoestadual();
document.querySelector('#fakerpercentage').innerHTML = jsbrasil.fakerBr.percentage();
document.querySelector('#fakerporcentagem').innerHTML = jsbrasil.fakerBr.porcentagem();
document.querySelector('#fakerrg').innerHTML = jsbrasil.fakerBr.rg();
document.querySelector('#fakerplaca').innerHTML = jsbrasil.fakerBr.placa();
document.querySelector('#fakertelefone').innerHTML = jsbrasil.fakerBr.telefone();
Expand Down Expand Up @@ -70,7 +70,7 @@ <h2>Faker</h2>
<p>cpf: <span id="fakercpf"></span></p>
<p>rg: <span id="fakerrg"></span></p>
<p>currency: <span id="fakercurrency"></span></p>
<p>percentage: <span id="fakerpercentage"></span></p>
<p>porcentagem: <span id="fakerporcentagem"></span></p>
<p>placa: <span id="fakerplaca"></span></p>
<p>titulo: <span id="fakertitulo"></span></p>
<p>time: <span id="fakertime"></span></p>
Expand Down
4 changes: 2 additions & 2 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('Initial test', () => {
expect(validateBr.cpf).to.exist;
expect(validateBr.currency).to.exist;
expect(validateBr.inscricaoestadual).to.exist;
expect(validateBr.percentage).to.exist;
expect(validateBr.porcentagem).to.exist;
expect(validateBr.rg).to.exist;
expect(validateBr.placa).to.exist;
expect(validateBr.telefone).to.exist;
Expand All @@ -22,7 +22,7 @@ describe('Initial test', () => {
expect(fakerBr.cpf).to.exist;
// expect(fakerBr.currency).to.exist;
expect(fakerBr.inscricaoestadual).to.exist;
expect(fakerBr.percentage).to.exist;
expect(fakerBr.porcentagem).to.exist;
expect(fakerBr.rg).to.exist;
expect(fakerBr.placa).to.exist;
expect(fakerBr.telefone).to.exist;
Expand Down
Loading

0 comments on commit c14f3e0

Please sign in to comment.