refactor: replace getInizialiCF with calcolaParteCognome and calcolaParteNome for improved CF generation
This commit is contained in:
parent
8159519c75
commit
ef59e61545
1 changed files with 38 additions and 28 deletions
|
|
@ -21,31 +21,6 @@ export const FormDebug: React.ElementType =
|
|||
)
|
||||
: () => null; // Return null component in production
|
||||
|
||||
const getInizialiCF = (stringa: string) => {
|
||||
const vocali = ["A", "E", "I", "O", "U"];
|
||||
let iniziali = "";
|
||||
for (const c of stringa) {
|
||||
if (iniziali.length < 3) {
|
||||
if (!vocali.includes(c)) {
|
||||
iniziali += c;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (iniziali.length < 3) {
|
||||
for (const c of stringa) {
|
||||
if (iniziali.length < 3) {
|
||||
if (vocali.includes(c)) {
|
||||
iniziali += c;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (iniziali.length === 2) {
|
||||
iniziali += "X";
|
||||
}
|
||||
return iniziali;
|
||||
};
|
||||
|
||||
const lettere_mesi = "ABCDEHLMPRST";
|
||||
const getMonthLetterCF = (month: number) => {
|
||||
return lettere_mesi[month];
|
||||
|
|
@ -80,6 +55,39 @@ const getAlternativeCF = (input: string): string[] => {
|
|||
return result;
|
||||
};
|
||||
|
||||
function calcolaParteCognome(cognome: string): string {
|
||||
const consonanti = cognome
|
||||
.toUpperCase()
|
||||
.replace(/[^BCDFGHJKLMNPQRSTVWXYZ]/g, "");
|
||||
const vocali = cognome.toUpperCase().replace(/[^AEIOU]/g, "");
|
||||
|
||||
if (consonanti.length >= 3) {
|
||||
return consonanti.substring(0, 3);
|
||||
}
|
||||
return `${consonanti + vocali}XXX`.substring(0, 3);
|
||||
}
|
||||
|
||||
function calcolaParteNome(nome: string): string {
|
||||
const consonanti = nome
|
||||
.toUpperCase()
|
||||
.replace(/[^BCDFGHJKLMNPQRSTVWXYZ]/g, "");
|
||||
const vocali = nome.toUpperCase().replace(/[^AEIOU]/g, "");
|
||||
|
||||
if (consonanti.length >= 4) {
|
||||
const c0 = consonanti[0];
|
||||
const c2 = consonanti[2];
|
||||
const c3 = consonanti[3];
|
||||
if (!c0 || !c2 || !c3) {
|
||||
throw new Error("Invalid name for CF generation");
|
||||
}
|
||||
// (4 o più consonanti): 1ª, 3ª e 4ª consonante
|
||||
return c0 + c2 + c3;
|
||||
}
|
||||
// (meno di 4 consonanti): prime consonanti + eventuali vocali
|
||||
const res = `${consonanti + vocali}XXX`.substring(0, 3);
|
||||
return res;
|
||||
}
|
||||
|
||||
const gen15CF = ({
|
||||
cognome,
|
||||
nome,
|
||||
|
|
@ -95,7 +103,7 @@ const gen15CF = ({
|
|||
}): string => {
|
||||
const data = new Date(dataNascita);
|
||||
|
||||
return `${getInizialiCF(cognome)}${getInizialiCF(nome)}${data.getFullYear().toString().slice(-2)}${getMonthLetterCF(data.getMonth())}${getDayCF(data.getDate(), sesso)}${catasto}`;
|
||||
return `${calcolaParteCognome(cognome)}${calcolaParteNome(nome)}${data.getFullYear().toString().slice(-2)}${getMonthLetterCF(data.getMonth())}${getDayCF(data.getDate(), sesso)}${catasto}`;
|
||||
};
|
||||
|
||||
const set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
|
@ -171,6 +179,8 @@ export const checkFiscalCodeValidity = ({
|
|||
sesso,
|
||||
});
|
||||
const control = genCarattereControllo(gen);
|
||||
console.log("Generated CF:", gen + control);
|
||||
console.log("Provided CF:", codiceFiscale);
|
||||
|
||||
if (
|
||||
gen !== codiceFiscale.slice(0, 15) &&
|
||||
|
|
@ -214,7 +224,7 @@ export const checkFiscalCodeValidity = ({
|
|||
});
|
||||
}
|
||||
|
||||
if (codiceFiscale.slice(0, 3) !== getInizialiCF(cognomeUpper)) {
|
||||
if (codiceFiscale.slice(0, 3) !== calcolaParteCognome(cognomeUpper)) {
|
||||
refinementContext.issues.push({
|
||||
code: "custom",
|
||||
input: "",
|
||||
|
|
@ -222,7 +232,7 @@ export const checkFiscalCodeValidity = ({
|
|||
path: [path],
|
||||
});
|
||||
}
|
||||
if (codiceFiscale.slice(3, 6) !== getInizialiCF(nomeUpper)) {
|
||||
if (codiceFiscale.slice(3, 6) !== calcolaParteNome(nomeUpper)) {
|
||||
refinementContext.issues.push({
|
||||
code: "custom",
|
||||
input: "",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue