A TypeScript library for calculating, validating, and decoding Italian Fiscal Codes (Codice Fiscale).
For an introduction and description in Italian, check out my blog post.
Check out the live demo to see codice-fiscale-ts in action!
This library was created to address several limitations in existing Italian Fiscal Code libraries:
- ๐ Automatically Updated Municipalities: Other libraries like codice-fiscale-js aren't automatically updated when changes are made to the list of municipal cadastral codes. This library uses the comuni-json repository and the ISTAT API to stay up-to-date.
- ๐ Modern TypeScript Support: Full TypeScript support with proper type definitions, distinguishing between people born in Italy (
ItalianPerson) and abroad (ForeignPerson). - โก Tree Shaking Support: Built with modern ES modules to support tree shaking. For validating a fiscal code (
isValidFiscalCode), the entire library is under 10kB (~5kB with tree shaking enabled)! - ๐ Standardized Country Codes: Uses ISO 3166-1 alpha-2 country codes instead of Italian country names when handling people born abroad.
- ๐งฎ Comprehensive Features: Supports both calculation (personal data โ fiscal code) and reverse calculation (fiscal code โ personal data).
- ๐ Cross-Platform: Works in both browser and Node.js environments, with both CJS and ESM support.
- ๐ Well-Documented and Tested: Every function is documented, tested, and deterministic.
npm install codice-fiscale-ts
# or
yarn add codice-fiscale-ts
# or
pnpm add codice-fiscale-tsimport { calculateFiscalCode, type Person } from "codice-fiscale-ts";
const person: Person = {
firstName: "Mario",
lastName: "Rossi",
birthDate: new Date("1990-01-01"),
gender: "M",
birthPlace: "Roma"
};
const fiscalCode = await calculateFiscalCode(person);
console.log(fiscalCode); // RSSMRA90A01H501W (example)import { isValidFiscalCode } from "codice-fiscale-ts";
const isValid = isValidFiscalCode("RSSMRA90A01H501W");
console.log(isValid); // trueimport { decodeFiscalCode } from "codice-fiscale-ts";
const decodedData = await decodeFiscalCode("RSSMRA90A01H501W");
console.log(decodedData);
/*
{
birthDate: new Date('1990-01-01'),
gender: 'M',
birthPlace: 'Roma',
birthProvince: 'RM'
}
*/calculateFiscalCode(person: Person): Promise<string>- Calculate a fiscal code from personal dataisValidFiscalCode(fiscalCode: string): boolean- Validate a fiscal codedecodeFiscalCode(fiscalCode: string): Promise<FiscalCodeData>- Decode a fiscal code into its components- Other utility functions are also available for more granular control
calculateFirstNameCode(firstName: string): string- Get the three letters representing a first namecalculateLastNameCode(lastName: string): string- Get the three letters representing a last namecalculateYearCode(date: Date): string- Get the two digits representing the yearcalculateMonthCode(date: Date): string- Get the letter representing the monthcalculateDayGenderCode(date: Date, gender: 'M' | 'F'): string- Get the two digits for the day and gendercalculateCheckCharacter(code: string): string- Calculate the final check characternormalizeString(str: string): string- Normalize a string by removing spaces, accents and special charactersextractConsonants(str: string): string- Extract consonants from a stringextractVowels(str: string): string- Extract vowels from a stringvalidatePerson(person: Person): void- Validate that a person object has all required fieldsisItalianPerson(person: Person): boolean- Check if a person was born in ItalyisForeignPerson(person: Person): boolean- Check if a person was born abroadgetMunicipalityByCode(code: string): Promise<Municipality>- Get municipality by cadastral codegetMunicipalCodeFromPlace(place: string): Promise<string>- Get cadastral code from place namegetMunicipalities(): Promise<Municipality[]>- Get all Italian municipalitiesgetForeignCountries(): Promise<Country[]>- Get all supported foreign countriesgetCountryCode(countryCode: string): Promise<string>- Get country code from ISO Alpha2 country codedecodeYear(yearCode: string): number- Decode the year from a fiscal codedecodeMonth(monthCode: string): number- Decode the month from a fiscal codedecodeDay(dayCode: number): number- Decode the day from a fiscal code
The package exports several TypeScript types to help with type checking:
Person- Basic person data needed for fiscal code calculationItalianPerson- Person born in ItalyForeignPerson- Person born abroadFiscalCodeData- The decoded components of a fiscal code
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.