salix/services/client/common/validations/validateDni.js

95 lines
2.8 KiB
JavaScript

module.exports = fi => {
let dni = fi;
let getLetterDni = dni => {
const regExpDni = 'TRWAGMYFPDXBNJZSQVHLCKE';
const letterDni = dni.toUpperCase().substring(0, 1);
let positionLetter = parseInt(dni) % 23;
let getLetter = regExpDni.substring(positionLetter + 1, positionLetter);
switch (letterDni) {
case 'X': case 'Y': case 'Z':
positionLetter = parseInt(dni.replace(letterDni, letterDni.charCodeAt(0) - 88)) % 23;
getLetter = regExpDni.substring(positionLetter + 1, positionLetter);
console.log(letterDni.charCodeAt(0));
}
return getLetter;
};
let getDniSpain = (dniNumeric, dniLetter) => {
let returnValue = false;
switch (dni.length) {
case 9:
if (dniLetter === getLetterDni(dni))
returnValue = true;
}
return returnValue;
};
let getDniForeign = (dniNumeric, dniLetter) => {
let returnValue = false;
switch (dni.length) {
case 9:
if (dniLetter === getLetterDni(dni))
returnValue = true;
}
return returnValue;
};
let getDniBusiness = () => {
if (dni.length == 9)
return true;
return false;
};
let getDniFrance = dniLetterCountry => {
let returnValue = false;
switch (dni.length) {
case 13:
if (dniLetter === 'R')
returnValue = true;
}
return returnValue;
};
let getDniItaly = dniLetterCountry => {
let returnValue = false;
switch (dni.length) {
case 13:
if (dniLetter === 'T')
returnValue = true;
}
return returnValue;
};
let getDni = () => {
const dniNumeric = dni.substring(0, 8);
const dniLetter = dni.substring(8, 9);
const dniLetterCountry = dni.substring(0, 1);
const dniLetterAscii = parseInt(dniLetterCountry.charCodeAt(0));
let dniValue = false;
switch (true) {
case (dniLetterAscii >= 88 && dniLetterAscii <= 90): // X-Z
dniValue = getDniForeign(dniNumeric, dniLetter);
break;
case (dniLetterAscii === 66): // B
dniValue = getDniBusiness();
break;
case (dniLetterAscii === 70): // F
dniValue = getDniFrance(dniLetterCountry);
break;
case (dniLetterAscii === 73): // I
dniValue = getDniItaly(dniLetterCountry);
break;
case (dniLetterAscii >= 48 && dniLetterAscii <= 57): // 0- 9
console.log('dni spain');
dniValue = getDniSpain(dniNumeric, dniLetter);
break;
default:
console.log('default');
dniValue = true;
}
return dniValue;
};
return getDni();
};