UNPKG

codice-fiscale-ts

Version:

A TypeScript library for calculating, validating, and decoding Italian Fiscal Codes (Codice Fiscale)

3 lines (2 loc) 5.23 kB
function l(t){return"birthPlace"in t&&typeof t.birthPlace=="string"&&t.birthPlace.trim()!==""&&(!("foreignCountry"in t)||t.foreignCountry==="IT")}function u(t){return"foreignCountry"in t&&typeof t.foreignCountry=="string"&&t.foreignCountry.length===2&&t.foreignCountry!=="IT"&&!("birthPlace"in t)}function d(t){if(!t)throw new Error("Person object is required");if(!t.firstName||t.firstName.trim()==="")throw new Error("First name is required");if(!t.lastName||t.lastName.trim()==="")throw new Error("Last name is required");if(!t.birthDate||!(t.birthDate instanceof Date)||Number.isNaN(t.birthDate.getTime()))throw new Error("Valid birth date is required");if(!t.gender||!["M","F"].includes(t.gender))throw new Error('Gender must be either "M" or "F"');if(!l(t)){if(!u(t))throw new Error('Either birthPlace must be provided with foreignCountry being undefined or "IT", or foreignCountry must be provided (not "IT") without birthPlace')}}async function P(t){let{municipalities:n}=await import("./chunks/municipalities-IZWKCUEP.mjs"),r=n.find(([e])=>e===t);if(!r)throw new Error(`Invalid municipality code: ${t}`);return r}function c(t){t=t.toUpperCase(),t=t.replace(/\s/g,"");let n={\u00C0:"A",\u00C1:"A",\u00C2:"A",\u00C3:"A",\u00C4:"A",\u00C5:"A",\u00C8:"E",\u00C9:"E",\u00CA:"E",\u00CB:"E",\u00CC:"I",\u00CD:"I",\u00CE:"I",\u00CF:"I",\u00D2:"O",\u00D3:"O",\u00D4:"O",\u00D5:"O",\u00D6:"O",\u00D9:"U",\u00DA:"U",\u00DB:"U",\u00DC:"U",\u00D1:"N",\u00C7:"C"};for(let[r,e]of Object.entries(n))t=t.replace(new RegExp(r,"g"),e);return t}function h(t){let n=t.toUpperCase().match(/[BCDFGHJKLMNPQRSTVWXYZ]/g);return n?n.join(""):""}function f(t){let n=t.toUpperCase().match(/[AEIOU]/g);return n?n.join(""):""}function b(t){t=c(t);let n=h(t),r=f(t),e="";for(e+=n.slice(0,3),e.length<3&&(e+=r.slice(0,3-e.length));e.length<3;)e+="X";return e}function w(t){t=c(t);let n=h(t),r=f(t),e="";if(n.length>=4)e=n[0]+n[2]+n[3];else for(e=n,e.length<3&&(e+=r.slice(0,3-e.length));e.length<3;)e+="X";return e}function x(t){return t.getFullYear().toString().slice(-2)}function M(t){let n=t.getMonth()+1;return{1:"A",2:"B",3:"C",4:"D",5:"E",6:"H",7:"L",8:"M",9:"P",10:"R",11:"S",12:"T"}[n]}function F(t,n){let r=t.getDate();return(n==="F"?r+40:r).toString().padStart(2,"0")}async function D(t){let{municipalities:n}=await import("./chunks/municipalities-IZWKCUEP.mjs"),r=c(t.toUpperCase());if(r.length===4){let i=n.find(([s])=>s===r);if(i)return i[0]}let e=n.find(([,i])=>c(i.toUpperCase())===r);if(!e)throw new Error(`Invalid birth place: ${t}`);return e[0]}async function I(t){let{countries:n}=await import("./chunks/countries-SZZXX6TJ.mjs"),r=n.find(([,e])=>e===t);if(!r)throw new Error(`Invalid country code: ${t}`);return r[0]}function p(t){let n={0:0,A:0,1:1,B:1,2:2,C:2,3:3,D:3,4:4,E:4,5:5,F:5,6:6,G:6,7:7,H:7,8:8,I:8,9:9,J:9,K:10,L:11,M:12,N:13,O:14,P:15,Q:16,R:17,S:18,T:19,U:20,V:21,W:22,X:23,Y:24,Z:25},r={0:1,A:1,1:0,B:0,2:5,C:5,3:7,D:7,4:9,E:9,5:13,F:13,6:15,G:15,7:17,H:17,8:19,I:19,9:21,J:21,K:2,L:4,M:18,N:20,O:11,P:3,Q:6,R:8,S:12,T:14,U:16,V:10,W:22,X:25,Y:24,Z:23},e=0;for(let o=0;o<t.length;o++){let a=t[o];o%2===0?e+=r[a]:e+=n[a]}return"ABCDEFGHIJKLMNOPQRSTUVWXYZ"[e%26]}function E(t){let n=Number.parseInt(t),r=new Date().getFullYear(),e=Math.floor(r/100)*100;return n>r%100?e-100+n:e+n}function N(t){return{A:1,B:2,C:3,D:4,E:5,H:6,L:7,M:8,P:9,R:10,S:11,T:12}[t]||1}function v(t){return t>40?t-40:t}async function V(t){d(t);let n=b(t.lastName),r=w(t.firstName),e=x(t.birthDate),i=M(t.birthDate),s=F(t.birthDate,t.gender),o;if(u(t))o=await I(t.foreignCountry);else if(l(t))o=await D(t.birthPlace);else throw new Error('Either birthPlace must be provided with foreignCountry being undefined or "IT", or foreignCountry must be provided (not "IT") without birthPlace');let a=n+r+e+i+s+o,g=p(a);return a+g}function A(t){if(t=t.toUpperCase().trim(),t.length!==16||!/^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/.test(t))return!1;let r=t.substring(0,15),e=t.substring(15,16),i=p(r);return e===i}async function k(t){if(!A(t))throw new Error("Invalid fiscal code format");let n=t.substring(6,8),r=t.substring(8,9),e=Number.parseInt(t.substring(9,11)),i=t.substring(11,15),s=E(n),o=N(r),a=v(e),g=e>40?"F":"M",m={birthDate:new Date(s,o-1,a),gender:g};if(i.startsWith("Z")){let{countries:B}=await import("./chunks/countries-SZZXX6TJ.mjs"),C=B.find(U=>U[0]===i),T=C?C[1]:i.substring(1);return{...m,foreignCountry:T}}let y=await P(i);return{...m,birthPlace:y[1],birthProvince:y[2]}}async function O(){let{municipalities:t}=await import("./chunks/municipalities-IZWKCUEP.mjs");return t}async function G(){let{countries:t}=await import("./chunks/countries-SZZXX6TJ.mjs");return t}export{p as calculateCheckCharacter,F as calculateDayGenderCode,w as calculateFirstNameCode,V as calculateFiscalCode,b as calculateLastNameCode,M as calculateMonthCode,x as calculateYearCode,v as decodeDay,k as decodeFiscalCode,N as decodeMonth,E as decodeYear,h as extractConsonants,f as extractVowels,I as getCountryCode,G as getForeignCountries,D as getMunicipalCodeFromPlace,O as getMunicipalities,P as getMunicipalityByCode,u as isForeignPerson,l as isItalianPerson,A as isValidFiscalCode,c as normalizeString,d as validatePerson}; //# sourceMappingURL=index.mjs.map