The Final and definitive solution to handle the Italian Tax Code
TS utilities to handle Italian Codice Fiscale
-
Why should I need a library? Can't I use just a RegExp? A RegExp would just check the form of a CodiceFiscale, not coherence between birth date and place, not validity of 16th check digit char
-
What about the omocodes (Omocodia/Omocodice) problem, does it work? Absolutely! There's a dedicated class to encode or decode such kind of CodiceFiscale and it's properly integrated in Parser, Validator and Pattern methods
-
Why it's ~240KB? Seems a lot It contains all you need in a FE or BE environment to properly check and validate a CodiceFiscale by its own or against spare personal informations, the full list of all Italian cities and foregin countries since 1861 including belfiore codes, province for cities, begin and end date
-
Should I trust such city/country informations and do I need them? Sure, every single information is provided with original source referrals like website, license and name. The script used to create the embedded dataset uses only official informations provided by Italian istitutional sources under CC-BY like 'Agenzia delle entrate', 'Ministero dell'interno' or 'ISTAT'; Give a look at the Assets Licenses and Authors chapters
-
Can I use this library in a FE project with other frameworks? Sure, it's built to work both in node and browser environments! Give a look at the Demo section
π CHANGELOG
π ROADMAP
npm i -s @marketto/codice-fiscale-utils
const CodiceFiscaleUtils = require('@marketto/codice-fiscale-utils');
import CodiceFiscaleUtils from '@marketto/codice-fiscale-utils';
import * as CodiceFiscaleUtils from '@marketto/codice-fiscale-utils';
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://unpkg.com/@marketto/diacritic-remover/dist/diacritic-remover.bundle.min.js"></script>
<script src="https://unpkg.com/@marketto/codice-fiscale-utils/dist/codice-fiscale-utils.bundle.min.js"></script>
Class with static methods
const { Parser } = CodiceFiscaleUtils;
Show code examples
Parser.cfDeomocode("KKALMNVMAPLB331Z"); //KKALMN91A30B331P
Parser.cfDeomocode("kkalmnvmaplb331z"); //kkalmn91a30b331p
Parser.cfOmocodeId("VRNGNYLtdsucprmt"); //127
Parser.cfOmocodeId("kkalmn91as0b331z"); //16
Parser.cfOmocodeId("kkalmn91a30b331z"); //0
Parser.cfOmocode("VRNGNY07d68c351v", 0); //VRNGNY07d68c351v
Parser.cfOmocode("VRNGNY07d68c351v", 1); //VRNGNY07d68c35Mn
Parser.cfOmocode("VRNGNY07d68c351v", 2); //VRNGNY07d68c3R1h
Parser.cfOmocode("VRNGNY07d68c351v", 3); //VRNGNY07d68c3RMz
Parser.cfOmocode("VRNGNY07d68c351v", 8); //VRNGNY07d6Uc351s
Parser.cfOmocode("VRNGNY07d68c351v", 32); //VRNGNY0Td68c351h
Parser.cfOmocode("VRNGNY07d68c351v", 127); //VRNGNYLTdSUcPRMt
//Re-omocode
Parser.cfOmocode("kkalmnvmaplb331z", 0); //kkalmn91a30b331p
Parser.cfOmocode("kkalmnvmaplb331z", 1); //kkalmn91a30b33Mh
Parser.cfOmocode("kkalmnvmaplb331z", 74); //kkalmnv1a3lb3P1t
Parser.cfOmocode("kkalmnvmaplb331z", 127); //kkalmnvmaplbPPMe
Parser.cfToLastName('WYZ'); //W*Y*Z*
Parser.cfToFirstName('ZZZWAE'); //WAE*
Parser.cfToGender('XXXYYY90B20'); //M
Parser.cfToGender('XXXYYY90B63'); //F
Parser.cfToBirthDay('XXXYYY90B71'); //31
Parser.cfToBirthMonth('XXXYYY92C'); //2
Parser will consider dates that can be both 19xx and 20xx as 20xx if they would be valid in the last 100 years range from now
Parser.cfToBirthYear('XXXYYY92'); //1992
Parser.cfToBirthYear('XXXYYY12'); //2012
const dt = Parser.cfToBirthDate('XXXYYY81A63'); //Date
dt.toJSON(); //1981-01-23T...
const birthPlace = Parser.cfToBirthPlace('XXXYYY92B20H501');
/*
{
firstName: "ROMA",
belfioreCode: "H501",
creationDate: Date("1884-09-10T22:00:00.000Z"),
expirationDate: Date("9999-12-31T22:59:59.999Z"),
province: "RM",
dataSource: {
"name": "Ministero dell\'Interno",
"url": "https://developers.italia.it/en/anpr",
"license": "cc-by-4.0",
"licenseUrl": "https://creativecommons.org/licenses/by/4.0/legalcode.it",
"termsAndConditions": "https://github.com/italia/anpr/blob/master/src/archivi/ANPR_archivio_comuni_legenda.md",
"authors": "https://github.com/italia/anpr/blob/master/AUTHORS"
}
}
*/
Parser.cfDecode('VRNGNY07D68C351V');
/*
{
lastName: 'V*R*N*',
firstName: 'G*N*Y*',
day: 28,
month: 3,
year: 2017,
gender: 'F',
place: 'CATANIA'
}
*/
Parser.lastNameToCf('Rossi'); //RSS
Parser.lastNameToCf('RΓ©no'); //RNE
Parser.lastNameToCf('Aieie'); //AIE
Parser.firstNameToCf('DΓ²minique'); //DNQ
Parser.firstNameToCf('Mark'); //MRK
Parser.firstNameToCf('Tom'); //TMO
Parser.firstNameToCf('Ania'); //NAI
Parser.yearToCf('1990'); //90
Parser.yearToCf(2010); //10
Parser.yearToCf('02'); //02
Parser.monthToCf(0); //A
Parser.monthToCf(4); //E
Parser.monthToCf(8); //P
Parser.monthToCf(0); //A
Parser.monthToCf(4); //E
Parser.monthToCf(8); //P
Parser.dayGenderToCf(3, 'M'); //03
Parser.dayGenderToCf(7, 'F'); //47
Parser.dateGenderToCf([2016, 3, 23], 'M'); //16D23
Parser.dateGenderToCf('1987-09-22', 'F'); //87P62
Parser.dateGenderToCf(new Date(2016, 2, 23, 12), 'M'); //16C23
Parser.dateGenderToCf(moment(1988, 7, 3, 12), 'F'); //88M43
Parser.placeToCf('Bologna');
/*
{
belfioreCode: 'A944',
firstName: 'BOLOGNA',
creationDate: 1861-03-16T23:00:00.000Z,
expirationDate: 9999-12-31T22:59:59.999Z,
dataSource: {...},
province: 'BO'
}
*/
Parser.placeToCf([1990],'Unione Sovietica');
/*
{
belfioreCode: 'Z135',
firstName: 'Unione Sovietica',
creationDate: 1860-12-31T23:00:00.000Z,
expirationDate: 1991-12-31T22:59:59.999Z,
dataSource: {...},
iso3166: 'SU'
}
*/
Parser.placeToCf([2000],'Unione Sovietica'); //null
Parser.encodeCf({
lastName: 'Veronesi',
firstName: 'Genny',
year: 1907,
month: 3,
day: 28,
gender: 'F',
place: 'Catania'
}); //VRNGNY07D68C351V
Class with static methods
const { Pattern } = CodiceFiscaleUtils;
Show code examples
Pattern.cfLastName().test('KST'); //true
Pattern.cfLastName().test('AST'); //false
Pattern.cfLastName('Alex').test('KST'); //false
Pattern.cfLastName('Alex').test('LXA'); //true
Pattern.cfFirstName().test('NIX'); //true
Pattern.cfFirstName().test('UIK'); //false
Pattern.cfFirstName('Dominique').test('DMN'); //false
Pattern.cfFirstName('Dominique').test('DNQ'); //true
Pattern.cfYear().test('07'); //true
Pattern.cfYear().test('3'); //false
Pattern.cfYear(1907).test('07'); //true
Pattern.cfYear(1986).test('U6'); //true - omocode
Pattern.cfYear(1986).test('87'); //false
Pattern.cfMonth().test('C'); //true
Pattern.cfMonth().test('Z'); //false
Pattern.cfMonth(3).test('D'); //true
Pattern.cfMonth(3).test('A'); //false
Pattern.cfDay().test('0M'); //true - omocode
Pattern.cfDay().test('33'); //false
Pattern.cfDay(12).test('12'); //true - male
Pattern.cfDay(12).test('52'); //true - female
Pattern.cfDay(12).test('MN'); //true - omocode
Pattern.cfDay(12).test('22'); //false
Pattern.cfDayGender().test('0M'); //true
Pattern.cfDayGender().test('73'); //false
Pattern.cfDayGender(9, 'F').test('RM'); //true
Pattern.cfDayGender(1, 'F').test('41'); //true
Pattern.cfDayGender(1, 'M').test('41'); //false
Pattern.cfDateGender().test('83D22'); //true
Pattern.cfDateGender().test('83Z32'); //false
Pattern.cfDateGender([1983, 3, 22], 'M').test('U3D2N'); //true
Pattern.cfDateGender("1995-05-01", 'F').test('V5EQ1'); //true
Pattern.cfDateGender([1983, 3, 22], 'M').test('83D62'); //false
Pattern.cfPlace().test('A662'); //true
Pattern.cfPlace().test('Z974'); //false
Pattern.cfPlace('Bari').test('H501'); //true
Pattern.cfPlace([1933], 'Fiume').test('D620'); //true
Pattern.cfPlace([2000], 'Fiume').test('D620'); //false - Always invalid
Pattern.codiceFiscale().test('VRNGNY07D68C351V'); //true
Pattern.codiceFiscale().test('MRNMIA02E45L2193'); //false
//Partial info
Pattern.codiceFiscale({
lastName: 'Veronesi',
firstName: 'Genny',
gender: 'F',
place: 'Catania'
}).test('VRNGNY97A65C351V'); //true
//Full info
Pattern.codiceFiscale({
lastName: 'Veronesi',
firstName: 'Genny',
year: 1907,
month: 3,
day: 28,
gender: 'F',
place: 'Catania'
}).test('VRNGNY07D68C351V'); //true
Pattern.lastName().test('Kristersen'); //true
Pattern.lastName('VLD').test('VΓ lidΓ tΓ²r'); //true
Pattern.lastName('AIX').test('Air'); //false
Pattern.firstName().test('Rossi'); //true
Pattern.firstName('XYZAIE').test('Aieie'); //true
Pattern.firstName('XYZAIX').test('Air'); //false
Pattern.date().test('1995'); //true
Pattern.date().test('1985-01'); //true
Pattern.date().test('1970-03-03'); //true
Pattern.date().test('1970-03-'); //false
Pattern.date('XYZXYZ88H61').test('1988-06-21'); //true
Pattern.date('XYZXYZ92C16').test('1992-03-26'); //false
Pattern.gender().test('F'); //true
Pattern.gender().test('X'); //false
Pattern.gender('XYZXYZ88H61').test('F'); //true
Pattern.gender('XYZXYZ88H61').test('M'); //false
Pattern.place().test('Roma'); //true
Pattern.place('XYZXYZ92C16A662').test('Bari'); //true
Pattern.place('XYZXYZ12S30A662').test('Bologna'); //false
Class with static methods
const { Validator } = CodiceFiscaleUtils;
Show code examples
Validator.isLastNameValid("Test"); //true
Validator.isLastNameValid("TΓ©st N'Γ me"); //true
Validator.isLastNameValid(""); //false
Validator.isLastNameValid("@!#"); //false
Validator.isLastNameInvalid("Test"); //false
Validator.isLastNameInvalid("TΓ©st N'Γ me"); //false
Validator.isLastNameInvalid(""); //false
Validator.isLastNameInvalid("@!#"); //true
Validator.isFirstNameValid("Test"); //true
Validator.isFirstNameValid("TΓ©st N'Γ me"); //true
Validator.isFirstNameValid(""); //false
Validator.isFirstNameValid("@!#"); //false
Validator.isFirstNameInvalid("Test"); //false
Validator.isFirstNameInvalid("TΓ©st N'Γ me"); //false
Validator.isFirstNameInvalid(""); //false
Validator.isFirstNameInvalid("@!#"); //true
Validator.isBirthDateValid("1999-01-01"); //true
Validator.isBirthDateValid([1999, 0, 1]); //true
Validator.isBirthDateValid(""); //false
Validator.isBirthDateValid("2000-02-30"); //false
Validator.isBirthDateValid("No date"); //false
Validator.isBirthDateValid("@!#"); //false
Validator.isBirthDateInvalid("1999-01-01"); //false
Validator.isBirthDateInvalid([1999, 0, 1]); //false
Validator.isBirthDateInvalid(""); //false
Validator.isBirthDateInvalid("2000-02-30"); //true
Validator.isBirthDateInvalid("No date"); //true
Validator.isBirthDateInvalid("@!#"); //true
Validator.isBirthPlaceValid("Roma"); //true
Validator.isBirthPlaceValid("H501"); //true
Validator.isBirthPlaceValid(""); //false
Validator.isBirthPlaceValid("Moon"); //false
//With scoped BelfioreConnector
//By places active at the given date
Validator.isBirthPlaceValid("Unione sovietica", Belfiore.active()); //false
Validator.isBirthPlaceValid("Federazione Russa", Belfiore.active()); //true
Validator.isBirthPlaceValid("Unione sovietica", Belfiore.active([1980])); //true
Validator.isBirthPlaceValid("Federazione Russa", Belfiore.active([1980])); //false
//By places active from a given date
Validator.isBirthPlaceValid("Unione sovietica", Belfiore.from()); //false
Validator.isBirthPlaceValid("Federazione Russa", Belfiore.from()); //true
Validator.isBirthPlaceValid("Unione sovietica", Belfiore.from([1980])); //true
Validator.isBirthPlaceValid("Federazione Russa", Belfiore.from([1980])); //true
//By cities
Validator.isBirthPlaceValid("Francia", Belfiore.cities); //false
Validator.isBirthPlaceValid("A662", Belfiore.cities) //true
//By countries
Validator.isBirthPlaceValid("Belgio", Belfiore.countries); //true
//By province
Validator.isBirthPlaceValid("Vibo Valentia", Belfiore.byProvince("VV")); //true
Validator.isBirthPlaceValid("H501", Belfiore.byProvince("VV")); //false
Validator.isBirthPlaceInvalid("Roma"); //false
Validator.isBirthPlaceInvalid("H501"); //false
Validator.isBirthPlaceInvalid(""); //false
Validator.isBirthPlaceInvalid("Moon"); //true
//With scoped BelfioreConnector
//By places active at the given date
Validator.isBirthPlaceInvalid("Unione sovietica", Belfiore.active()); //true
Validator.isBirthPlaceInvalid("Federazione Russa", Belfiore.active()); //false
Validator.isBirthPlaceInvalid("Unione sovietica", Belfiore.active([1980])); //false
Validator.isBirthPlaceInvalid("Federazione Russa", Belfiore.active([1980])); //true
//By places active from a given date
Validator.isBirthPlaceInvalid("Unione sovietica", Belfiore.from()); //true
Validator.isBirthPlaceInvalid("Federazione Russa", Belfiore.from()); //false
Validator.isBirthPlaceInvalid("Unione sovietica", Belfiore.from([1980])); //false
Validator.isBirthPlaceInvalid("Federazione Russa", Belfiore.from([1980])); //false
//By cities
Validator.isBirthPlaceInvalid("Francia", Belfiore.cities); //true
Validator.isBirthPlaceInvalid("A662", Belfiore.cities); //false
//By countries
Validator.isBirthPlaceInvalid("Belgio", Belfiore.countries); //false
//By province
Validator.isBirthPlaceInvalid("Vibo Valentia", Belfiore.byProvince("VV")); //false
Validator.isBirthPlaceInvalid("H501", Belfiore.byProvince("VV")); //true
Validator.birthDatePlaceMatch("1990-05-21", "Repubblica Socialista Federale di Jugoslavia"); //true
Validator.birthDatePlaceMatch(new Date(), "Repubblica Socialista Federale di Jugoslavia"); //false
Validator.birthDatePlaceMatch("1988-03-11", "Roma"); //true
Validator.birthDatePlaceMatch(new Date(), "Roma"); //true
Validator.birthDatePlaceMatch(new Date(), ""); //false
Validator.birthDatePlaceMatch("", "Palermo"); //false
Validator.birthDatePlaceMatch("", ""); //false
Validator.birthDatePlaceMismatch("1990-05-21", "Repubblica Socialista Federale di Jugoslavia"); //false
Validator.birthDatePlaceMismatch(new Date(), "Repubblica Socialista Federale di Jugoslavia"); //true
Validator.birthDatePlaceMismatch("1988-03-11", "Roma"); //false
Validator.birthDatePlaceMismatch(new Date(), "Roma"); //false
Validator.birthDatePlaceMismatch(new Date(), ""); //false
Validator.birthDatePlaceMismatch("", "Palermo"); //false
Validator.birthDatePlaceMismatch("", ""); //false
Validator.codiceFiscale("VRNGNY07D68C351V")
.matchPersonalInfo({
day: 28,
firstName: "GΓ©nny",
gender: "F",
lastName: "VerΓ²nesi",
month: 3,
place: "Catania",
year: 1907,
}); //true
Validator.codiceFiscale("VRNGNY07D68C351V")
.mismatchPersonalInfo({
day: 28,
firstName: "GΓ©nny",
gender: "F",
lastName: "VerΓ²nesi",
month: 3,
place: "Firenze",
year: 1907,
}); //false
Validator.codiceFiscale("VRNGNY07D68C351V")
.mismatchPersonalInfo({
day: 28,
firstName: "GΓ©nny",
gender: "F",
lastName: "VerΓ²nesi",
month: 3,
place: "Catania",
year: 1907,
}); //false
Validator.codiceFiscale("VRNGNY07D68C351V")
.mismatchPersonalInfo({
day: 28,
firstName: "GΓ©nny",
}); //false
Class instance
Validator.codiceFiscale("VRNGNY07D68C351V"); //CFMismatchValidator
Show code examples
Validator.codiceFiscale("VRNGNY07D68C351V").valid //true
Validator.codiceFiscale("MRNMIA02E45L219X").valid //true
Validator.codiceFiscale("GSTPPP31C06D620Z").valid //true
Validator.codiceFiscale("VRNGNY07D68C351K").valid //false - invalid check digit
Validator.codiceFiscale("GSTPPP99C06D620V").valid //false - invalid birth date/place
Validator.codiceFiscale("").valid; //false - empty cf
Validator.codiceFiscale("VRNGNY07D68C351V").invalid //false - OK
Validator.codiceFiscale("MRNMIA02E45L219X").invalid //false - OK
Validator.codiceFiscale("GSTPPP31C06D620Z").invalid //false - OK
Validator.codiceFiscale("VRNGNY07D68C351K").invalid //true - invalid check digit
Validator.codiceFiscale("GSTPPP99C06D620V").invalid //true - invalid birth date/place
Validator.codiceFiscale("").invalid //false - empty cf is not invalid!
Validator.codiceFiscale("VRNGNY07D68C351V").matchLastName("Vareni"); //true
Validator.codiceFiscale("VRN").matchLastName("Vareni"); //true
Validator.codiceFiscale("").matchLastName("Vareni"); //false
Validator.codiceFiscale("VRNGNY07D68C351V").matchLastName("John"); //false
Validator.codiceFiscale("VRNGNY07D68C351V").matchLastName("V"); //false
Validator.codiceFiscale("VRNGNY07D68C351V").matchLastName(""); //false
Validator.codiceFiscale("VRNGNY07D68C351V").mismatchLastName("Vareni"); //false
Validator.codiceFiscale("VRN").mismatchLastName("Vareni"); //false
Validator.codiceFiscale("").mismatchLastName("Vareni"); //false
Validator.codiceFiscale("VRNGNY07D68C351V").mismatchLastName("John"); //true
Validator.codiceFiscale("VRNGNY07D68C351V").mismatchLastName("V"); //true
Validator.codiceFiscale("VRNGNY07D68C351V").mismatchLastName(""); //false
Validator.codiceFiscale("VRNGNY07D68C351V").matchFirstName("Genny"); //true
Validator.codiceFiscale("VRNGNY").matchFirstName("Genny"); //true
Validator.codiceFiscale("").matchFirstName("Genny"); //false
Validator.codiceFiscale("VRNGNY07D68C351V").matchFirstName("John"); //false
Validator.codiceFiscale("VRNGNY07D68C351V").matchFirstName("G"); //false
Validator.codiceFiscale("VRNGNY07D68C351V").matchFirstName(""); //false
Validator.codiceFiscale("VRNGNY07D68C351V").mismatchFirstName("Genny"); //false
Validator.codiceFiscale("VRN").mismatchFirstName("Genny"); //false
Validator.codiceFiscale("").mismatchFirstName("Genny"); //false
Validator.codiceFiscale("VRNGNY07D68C351V").mismatchFirstName("John"); //true
Validator.codiceFiscale("VRNGNY07D68C351V").mismatchFirstName("G"); //true
Validator.codiceFiscale("VRNGNY07D68C351V").mismatchFirstName(""); //false
Validator.codiceFiscale("VRNGNY07D68C351V").matchBirthDate("2007-04-28"); //true
Validator.codiceFiscale("VRNGNY07D68").matchBirthDate("2007-04-28"); //true
Validator.codiceFiscale("").matchBirthDate("2007-04-28"); //false
Validator.codiceFiscale("VRNGNY07D68C351V").matchBirthDate("2008-02-16"); //false
Validator.codiceFiscale("VRNGNY07D68C351V").matchBirthDate(""); //false
Validator.codiceFiscale("VRNGNY07D68C351V").mismatchBirthDate("2007-04-28"); //false
Validator.codiceFiscale("VRNGNY07D68").mismatchBirthDate("2007-04-28"); //false
Validator.codiceFiscale("").mismatchBirthDate("2007-04-28"); //false
Validator.codiceFiscale("VRNGNY07D68C351V").mismatchBirthDate("2008-02-16"); //true
Validator.codiceFiscale("VRNGNY07D68C351V").mismatchBirthDate(""); //false
Validator.codiceFiscale("VRNGNY07D68C351V").matchGender("F"); //true
Validator.codiceFiscale("VRNGNY07D68").matchGender("F"); //true
Validator.codiceFiscale("VRNGNY07D6").matchGender("F"); //true
Validator.codiceFiscale("").matchGender("F"); //false
Validator.codiceFiscale("VRNGNY07D68C351V").matchGender("M"); //false
Validator.codiceFiscale("VRNGNY07D68C351V").matchGender(""); //false
Validator.codiceFiscale("VRNGNY07D68C351V").mismatchGender("F"); //false
Validator.codiceFiscale("VRNGNY07D68").mismatchGender("F"); //false
Validator.codiceFiscale("VRNGNY07D6").mismatchGender("F"); //false
Validator.codiceFiscale("").mismatchGender("F"); //false
Validator.codiceFiscale("VRNGNY07D68C351V").mismatchGender("M"); //true
Validator.codiceFiscale("VRNGNY07D68C351V").mismatchGender(""); //false
Validator.codiceFiscale("VRNGNY07D68C351V").matchBirthPlace("CATANIA"); //true
Validator.codiceFiscale("VRNGNY07D68C351").matchBirthPlace("CATANIA"); //true
Validator.codiceFiscale("").matchBirthPlace("CATANIA"); //false
Validator.codiceFiscale("VRNGNY07D68C351V").matchBirthPlace("ROMA"); //false
Validator.codiceFiscale("VRNGNY07D68C351V").matchBirthPlace(""); //false
Validator.codiceFiscale("VRNGNY07D68C351V").mismatchBirthPlace("CATANIA"); //false
Validator.codiceFiscale("VRNGNY07D68C351").mismatchBirthPlace("CATANIA"); //false
Validator.codiceFiscale("").mismatchBirthPlace("CATANIA"); //false
Validator.codiceFiscale("VRNGNY07D68C351V").mismatchBirthPlace("ROMA"); //true
Validator.codiceFiscale("VRNGNY07D68C351V").mismatchBirthPlace(""); //false
- NodeJs
- Chrome
- Firefox
- Edge
All names, informations, and fiscal codes used in this README and all unit tests are fictitious. No identification with actual persons (living or deceased) is intended or should be inferred