98 lines
2.9 KiB
JavaScript
98 lines
2.9 KiB
JavaScript
module.exports = fi => {
|
|
if (fi === undefined || fi === null) {
|
|
return true;
|
|
}
|
|
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);
|
|
}
|
|
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 (dni.length === 9 && !isNaN(dni)): // dni Portugal (9 digitos)
|
|
dniValue = true;
|
|
break;
|
|
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 (dni.length === 9 && dniLetterAscii >= 48 && dniLetterAscii <= 57): // 0- 9
|
|
dniValue = getDniSpain(dniNumeric, dniLetter);
|
|
break;
|
|
default:
|
|
dniValue = true;
|
|
}
|
|
return dniValue;
|
|
};
|
|
|
|
return getDni();
|
|
};
|