Skip to content

Instantly share code, notes, and snippets.

@lsneucamp
Created April 15, 2016 19:35
Show Gist options
  • Save lsneucamp/a17bb8b2b99b7e5b5f0a500e6d24467a to your computer and use it in GitHub Desktop.
Save lsneucamp/a17bb8b2b99b7e5b5f0a500e6d24467a to your computer and use it in GitHub Desktop.
Js Test Generator
var utils = {
gerarCPF: function () {
var cpf;
var comPontos = true;
var n = 9;
var n1 = this.randomiza(n);
var n2 = this.randomiza(n);
var n3 = this.randomiza(n);
var n4 = this.randomiza(n);
var n5 = this.randomiza(n);
var n6 = this.randomiza(n);
var n7 = this.randomiza(n);
var n8 = this.randomiza(n);
var n9 = this.randomiza(n);
var d1 = n9 * 2 + n8 * 3 + n7 * 4 + n6 * 5 + n5 * 6 + n4 * 7 + n3 * 8 + n2 * 9 + n1 * 10;
d1 = 11 - (this.mod(d1, 11));
if (d1 >= 10) d1 = 0;
var d2 = d1 * 2 + n9 * 3 + n8 * 4 + n7 * 5 + n6 * 6 + n5 * 7 + n4 * 8 + n3 * 9 + n2 * 10 + n1 * 11;
d2 = 11 - (this.mod(d2, 11));
if (d2 >= 10) d2 = 0;
if (comPontos) cpf = '' + n1 + n2 + n3 + '.' + n4 + n5 + n6 + '.' + n7 + n8 + n9 + '-' + d1 + d2;
else cpf = '' + n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + d1 + d2;
return cpf;
},
validarCPF: function (cpf) {
cpf = cpf.replace(/[^\d]+/g, '');
if (cpf === '') return false;
// Elimina CPFs invalidos conhecidos
if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
return false;
// Valida 1o digito
add = 0;
for (i = 0; i < 9; i++)
add += parseInt(cpf.charAt(i)) * (10 - i);
var rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(9)))
return false;
// Valida 2o digito
add = 0;
for (i = 0; i < 10; i++)
add += parseInt(cpf.charAt(i)) * (11 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(10)))
return false;
return true;
}, gerarCNPJ: function () {
var cnpj, comPontos = true;
var n = 9;
var n1 = this.randomiza(n);
var n2 = this.randomiza(n);
var n3 = this.randomiza(n);
var n4 = this.randomiza(n);
var n5 = this.randomiza(n);
var n6 = this.randomiza(n);
var n7 = this.randomiza(n);
var n8 = this.randomiza(n);
var n9 = 0; //randomiza(n);
var n10 = 0; //randomiza(n);
var n11 = 0; //randomiza(n);
var n12 = 1; //randomiza(n);
var d1 = n12 * 2 + n11 * 3 + n10 * 4 + n9 * 5 + n8 * 6 + n7 * 7 + n6 * 8 + n5 * 9 + n4 * 2 + n3 * 3 + n2 * 4 + n1 * 5;
d1 = 11 - (this.mod(d1, 11));
if (d1 >= 10) d1 = 0;
var d2 = d1 * 2 + n12 * 3 + n11 * 4 + n10 * 5 + n9 * 6 + n8 * 7 + n7 * 8 + n6 * 9 + n5 * 2 + n4 * 3 + n3 * 4 + n2 * 5 + n1 * 6;
d2 = 11 - (this.mod(d2, 11));
if (d2 >= 10) d2 = 0;
if (comPontos) cnpj = '' + n1 + n2 + '.' + n3 + n4 + n5 + '.' + n6 + n7 + n8 + '/' + n9 + n10 + n11 + n12 + '-' + d1 + d2;
else cnpj = '' + n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10 + n11 + n12 + d1 + d2;
return cnpj;
}, randomiza: function (n) {
var ranNum = Math.round(Math.random() * n);
return ranNum;
}, mod: function (dividendo, divisor) {
return Math.round(dividendo - (Math.floor(dividendo / divisor) * divisor));
},
gerarString:function(){
return Math.random().toString(36).substring(7);
}
}
var utils = {
gerarCPF: function () {
var cpf;
var comPontos = true;
var n = 9;
var n1 = this.randomiza(n);
var n2 = this.randomiza(n);
var n3 = this.randomiza(n);
var n4 = this.randomiza(n);
var n5 = this.randomiza(n);
var n6 = this.randomiza(n);
var n7 = this.randomiza(n);
var n8 = this.randomiza(n);
var n9 = this.randomiza(n);
var d1 = n9 * 2 + n8 * 3 + n7 * 4 + n6 * 5 + n5 * 6 + n4 * 7 + n3 * 8 + n2 * 9 + n1 * 10;
d1 = 11 - (this.mod(d1, 11));
if (d1 >= 10) d1 = 0;
var d2 = d1 * 2 + n9 * 3 + n8 * 4 + n7 * 5 + n6 * 6 + n5 * 7 + n4 * 8 + n3 * 9 + n2 * 10 + n1 * 11;
d2 = 11 - (this.mod(d2, 11));
if (d2 >= 10) d2 = 0;
if (comPontos) cpf = '' + n1 + n2 + n3 + '.' + n4 + n5 + n6 + '.' + n7 + n8 + n9 + '-' + d1 + d2;
else cpf = '' + n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + d1 + d2;
return cpf;
},
validarCPF: function (cpf) {
cpf = cpf.replace(/[^\d]+/g, '');
if (cpf === '') return false;
// Elimina CPFs invalidos conhecidos
if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
return false;
// Valida 1o digito
add = 0;
for (i = 0; i < 9; i++)
add += parseInt(cpf.charAt(i)) * (10 - i);
var rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(9)))
return false;
// Valida 2o digito
add = 0;
for (i = 0; i < 10; i++)
add += parseInt(cpf.charAt(i)) * (11 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(10)))
return false;
return true;
}, gerarCNPJ: function () {
var cnpj, comPontos = true;
var n = 9;
var n1 = this.randomiza(n);
var n2 = this.randomiza(n);
var n3 = this.randomiza(n);
var n4 = this.randomiza(n);
var n5 = this.randomiza(n);
var n6 = this.randomiza(n);
var n7 = this.randomiza(n);
var n8 = this.randomiza(n);
var n9 = 0; //randomiza(n);
var n10 = 0; //randomiza(n);
var n11 = 0; //randomiza(n);
var n12 = 1; //randomiza(n);
var d1 = n12 * 2 + n11 * 3 + n10 * 4 + n9 * 5 + n8 * 6 + n7 * 7 + n6 * 8 + n5 * 9 + n4 * 2 + n3 * 3 + n2 * 4 + n1 * 5;
d1 = 11 - (this.mod(d1, 11));
if (d1 >= 10) d1 = 0;
var d2 = d1 * 2 + n12 * 3 + n11 * 4 + n10 * 5 + n9 * 6 + n8 * 7 + n7 * 8 + n6 * 9 + n5 * 2 + n4 * 3 + n3 * 4 + n2 * 5 + n1 * 6;
d2 = 11 - (this.mod(d2, 11));
if (d2 >= 10) d2 = 0;
if (comPontos) cnpj = '' + n1 + n2 + '.' + n3 + n4 + n5 + '.' + n6 + n7 + n8 + '/' + n9 + n10 + n11 + n12 + '-' + d1 + d2;
else cnpj = '' + n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10 + n11 + n12 + d1 + d2;
return cnpj;
}, randomiza: function (n) {
var ranNum = Math.round(Math.random() * n);
return ranNum;
}, mod: function (dividendo, divisor) {
return Math.round(dividendo - (Math.floor(dividendo / divisor) * divisor));
},
gerarString:function(){
return Math.random().toString(36).substring(7);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment