UNPKG

cie-calculator-flap

Version:

Easily generate CIE References

70 lines (56 loc) 1.62 kB
'use strict'; var CIEStrategy = require('./cie_strategy.js').CIEStrategy; var CIEUtils = require('./cie_utils.js').CIEUtils; var cieUtils = new CIEUtils(); var Alg14 = function() {}; Alg14.prototype = new CIEStrategy(); Alg14.prototype = { execute: function(input) { var validationRules = { reference: {length: {min: 1,max: 18}} }; if (!cieUtils.validateInput(input, validationRules)) { var error = new Error('Longitud invalida'); error.status = '422'; throw error; } return mVerificador(input.reference); } }; module.exports.Alg14 = Alg14; function mVerificador(ref){ let charToNum = { A:1,J:1, B:2,K:2,S:2, C:3,L:3,T:3, D:4,M:4,U:4, E:5,N:5,V:5, F:6,O:6,W:6, G:7,P:7,X:7, H:8,Q:8,Y:8, I:9,R:9,Z:9 }; let valoresPonderadores = [13,17,19,23,11]; let valores = []; //let cadena = '000SAR4280'; let indice = ref.length-2; let indiceVP = 0; for(let contador=0; contador<ref.length-1; contador++){ if(indiceVP === valoresPonderadores.length){ indiceVP = 0; } let valorC = ref.charAt(indice); let vp = valoresPonderadores[indiceVP]; let op = (charToNum[valorC]?charToNum[valorC]:parseInt(valorC)) * vp; valores.unshift(op); indice--; indiceVP++; } let suma = valores.reduce((a, b) => a + b, 0); let sumaTotal = suma + 330; let residuo = (sumaTotal % 97); let dVerificador = (residuo+1); let vCadena = dVerificador.toString(); let referencia = ref+vCadena; return referencia; }