Skip to content

Instantly share code, notes, and snippets.

@victorwpbastos
Created October 17, 2014 16:39
Show Gist options
  • Save victorwpbastos/dc8779296cc8b7975f55 to your computer and use it in GitHub Desktop.
Save victorwpbastos/dc8779296cc8b7975f55 to your computer and use it in GitHub Desktop.
Validador de Matricula
var _ = require('underscore');
module.exports = function(matricula) {
var arrNumerosSoma = {
DV_1: [2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 1, 2, 3, 4, 0, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
DV_2: [1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 1, 2, 3, 0, 0, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
},
somatoria = {
DV_1: [],
DV_2: []
},
dv = [];
console.log(matricula);
// Calculo do Digito Verificador 1
_(arrNumerosSoma.DV_1).each(function(num, index) {
somatoria.DV_1.push( num * parseInt(matricula[index], 10) );
});
somatoria.DV_1 = _(somatoria.DV_1).reduce(function(memo, num) { return memo + num; });
console.log(somatoria.DV_1);
dv.push( parseInt((somatoria.DV_1 % 11).toString().charAt(0), 10) );
// Calculo do Digito Verificador 2
_(arrNumerosSoma.DV_2).each(function(num, index) {
if(matricula[index]) {
somatoria.DV_2.push( num * parseInt(matricula[index], 10) );
} else {
somatoria.DV_2.push( num * dv[0] );
}
});
somatoria.DV_2 = _(somatoria.DV_2).reduce(function(memo, num) { return memo + num; });
console.log(somatoria.DV_2);
dv.push( parseInt((somatoria.DV_2 % 11).toString().charAt(0), 10) );
console.log( dv );
// Retorno
return matricula.slice(-2) === dv.join('');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment