@marketto/codice-fiscale-utils
Version:
TS & JS utilities to handle Italian Codice Fiscale
9 lines (7 loc) • 22.1 kB
JavaScript
/**
* @marketto/codice-fiscale-utils 3.1.3
* Copyright (c) 2019-2024, Marco Ricupero <marco.ricupero@gmail.com>
* License: MIT
*/
import t from"dayjs";import e from"dayjs/plugin/utc";import i from"@marketto/diacritic-remover";const r=15,s="[12][0-9]{3}",a="0[1-9]|1[0-2]",n="0[469]|11",o="0[13578]|1[02]",c=`(?:${a})-(?:0[1-9]|[12]\\d)|(?:${n})-30|(?:${o})-3[01]`,h="[01]\\d|2[0-3]",l="[0-5]\\d",d=l,T="\\d{3}",f=`Z|[-+](?:${h})(?::?${l})?`,$=`(?:${h})(?::${l}(?::${d}(?:\\.${T})?)?(?:${f})?)?`,u=`${s}-(?:${c})(?:T${$})?`,A=`${s}(?:-(?:(?:${a})|(?:${c})(?:T${$})?))?`;var E=Object.freeze({__proto__:null,DAY:"0[1-9]|[12][0-9]|3[01]",DAYS_30_MONTHS:n,DAYS_31_MONTHS:o,HOURS:h,ISO8601_DATE_TIME:A,ISO8601_SHORT_DATE:u,LEAP_MONTH:"02",MILLISECONDS:T,MINUTES:l,MONTH:a,MONTH_DAY:c,SECONDS:d,TIME:$,TIMEZONE:f,YEAR:s});t.extend(e);class _{static parseDate(e){if(!(e instanceof Date||"string"==typeof e&&new RegExp(`^(?:${A})$`).test(e)||Array.isArray(e)&&e.length&&!e.some((t=>"number"!=typeof t||isNaN(t)))))return null;try{let i;if(Array.isArray(e)){const[r,s=0,a=1]=e;if(!(s>=0&&s<=11&&a>0&&a<=31))return null;i=t.utc(Date.UTC(r,s||0,a||1))}else i=t.utc(e);return i.isValid()?i.toDate():null}catch(t){return null}}static ymdToDate(t,e,i){return this.parseDate([t,e,i])}}const p="B-DF-HJ-NP-TV-Z",C="AEIOU",m="\\dLMNP-V",R="1-9MNP-V",D="0L",N="A-EHLMPR-T",M="DHPS",I="ACELMRT",S="A-M",O="Z",y=`[A-Z][${C}][${C}X]|[${C}]X{2}|[${p}]{2}[A-Z]`,g=y,L=`(?:${y}){2}`,P=`[${m}]{2}`,F="[02468LNQSU][048LQU]|[13579MPRTV][26NS]",w=`[${N}]`,b="[26NS]",B="[37PT]",v=`[${D}4Q][${R}]|[1256MNRS][${m}]`,H=`[${B}][${D}]`,Y=`[${B}][${D}1M]`,V=`(?:${v}|${B}[${D}1M])`,G=`(?:[${D}][${R}]|[12MN][${m}]|[3P][${D}1M])`,U=`(?:[4Q][${R}]|[56RS][${m}]|[7T][${D}1M])`,j=`${w}(?:${v})|[${M}]${H}|[${I}]${Y}`,x=`${P}(?:${w}(?:[${D}4Q][${R}]|[15MR][${m}]|${b}[0-8LMNP-U])|[${M}]${B}[${D}]|[${I}]${B}[${D}1M]|[${M}${I}]${b}[9V])|(?:${F})B${b}[9V]`,Z=`${P}(?:${w}(?:[${D}][${R}]|[1M][${m}]|[2N][0-8LMNP-U])|[${M}][3P][${D}]|[${I}][3P][${D}1M]|[${M}${I}][2N][9V])|(?:${F})B[2N][9V]`,k=`${P}(?:${w}(?:[4Q][${R}]|[5R][${m}]|[6S][0-8LMNP-U])|[${M}][7T][${D}]|[${I}][7T][${D}1M]|[${M}${I}][6S][9V])|(?:${F})B[6S][9V]`,X=`[${S}](?:[${R}][${m}]{2}|[${D}](?:[${R}][${m}]|[${D}][${R}]))`,J=`${O}[${R}][${m}]{2}`,Q=`(?:[${S}${O}][${R}][${m}]{2})|(?:[${S}][${D}](?:[${R}][${m}]|[${D}][${R}]))`,z="[A-Z]",K=`${L}(?:${x})(?:${Q})${z}`,W=`[A-Z][${C}]?|[${p}]{1,2}`,q=`(?:${W})|(?:(?:${y})(?:${W})?)`,tt=`[${m}]`,et=`${w}[${D}12456MNQRS]?|[${M}${I}]${B}`,it=`${tt}|(?:${P}(?:${et})?)`,rt=`[${S}${O}](?:[${R}][${m}]?)?|[${O}](?:[${D}][${m}]?)?`,st=`${q}|(?:${L}(?:(?:${it})|(?:${x})(?:(?:${rt})|(?:${Q})${z}?)?)?)?`;var at,nt=Object.freeze({__proto__:null,BELFIORE_CODE_MATCHER:Q,CF_FULL_NAME_MATCHER:L,CF_NAME_MATCHER:y,CF_SURNAME_MATCHER:g,CHECK_DIGIT:z,CITY_CODE_LIST:S,CITY_CODE_MATCHER:X,CODICE_FISCALE:K,CONSONANT_LIST:p,COUNTRY_CODE_LIST:O,COUNTRY_CODE_MATCHER:J,DAY_29_MATCHER:v,DAY_2X_MATCHER:b,DAY_30_MATCHER:H,DAY_31_MATCHER:Y,DAY_3X_MATCHER:B,DAY_MATCHER:V,FEMALE_DAY_MATCHER:U,FEMALE_FULL_DATE_MATCHER:k,FULL_DATE_MATCHER:x,LEAP_YEAR_MATCHER:F,MALE_DAY_MATCHER:G,MALE_FULL_DATE_MATCHER:Z,MONTH_30DAYS_LIST:M,MONTH_31DAYS_LIST:I,MONTH_DAY_MATCHER:j,MONTH_LIST:N,MONTH_MATCHER:w,OMOCODE_NON_ZERO_NUMBER_LIST:R,OMOCODE_NUMBER_LIST:m,OMOCODE_ZERO_LIST:D,PARTIAL_BELFIORE_CODE_MATCHER:rt,PARTIAL_CF:st,PARTIAL_CF_FULL_NAME:q,PARTIAL_CF_NAME_MATCHER:W,PARTIAL_FULL_DATE:it,PARTIAL_MONTH_DAY:et,PARTIAL_YEAR:tt,VOWEL_LIST:C,YEAR_MATCHER:P});!function(t){t[t.B=0]="B",t[t.A=1]="A",t[t.K=2]="K",t[t.P=3]="P",t[t.L=4]="L",t[t.C=5]="C",t[t.Q=6]="Q",t[t.D=7]="D",t[t.R=8]="R",t[t.E=9]="E",t[t.V=10]="V",t[t.O=11]="O",t[t.S=12]="S",t[t.F=13]="F",t[t.T=14]="T",t[t.G=15]="G",t[t.U=16]="U",t[t.H=17]="H",t[t.M=18]="M",t[t.I=19]="I",t[t.N=20]="N",t[t.J=21]="J",t[t.W=22]="W",t[t.Z=23]="Z",t[t.Y=24]="Y",t[t.X=25]="X"}(at||(at={}));var ot,ct=at;class ht{static checkDigit(t){if("string"==typeof t&&new RegExp(st).test(t)){const e=t.substr(0,r);let i=0;for(const t of this.evaluateChar(e))i+=t;return String.fromCharCode(i%this.CRC_MOD+this.CHAR_OFFSET)}return null}static evaluateChar(t=""){return(e=this.evaluateCharGenerator(t))[Symbol.iterator]=()=>e,e;var e}static CHAR_OFFSET=65;static CRC_MOD=26;static*evaluateCharGenerator(t=""){if("string"==typeof t&&t.length)for(let e=0;e<t.length;e++){let i=t[e].toUpperCase();/^\d$/u.test(i)&&(i=String.fromCharCode(parseInt(i,10)+this.CHAR_OFFSET));!(e%2)?yield parseInt(ct[i],10):yield i.charCodeAt(0)-this.CHAR_OFFSET}return 0}}!function(t){t[t.A=0]="A",t[t.B=1]="B",t[t.C=2]="C",t[t.D=3]="D",t[t.E=4]="E",t[t.H=5]="H",t[t.L=6]="L",t[t.M=7]="M",t[t.P=8]="P",t[t.R=9]="R",t[t.S=10]="S",t[t.T=11]="T"}(ot||(ot={}));var lt,dt=ot;!function(t){t[t.M=0]="M",t[t.F=40]="F"}(lt||(lt={}));var Tt,ft=lt;!function(t){t[t.L=0]="L",t[t.M=1]="M",t[t.N=2]="N",t[t.P=3]="P",t[t.Q=4]="Q",t[t.R=5]="R",t[t.S=6]="S",t[t.T=7]="T",t[t.U=8]="U",t[t.V=9]="V"}(Tt||(Tt={}));var $t=Tt;class ut{static getDay(t){const e=t%ft.F;return e>0&&e<=this.MAX_MONTH_DAY?e:null}static getGender(t){return this.toArray().find((e=>t>=ft[e]&&t<=ft[e]+this.MAX_MONTH_DAY))||null}static genderizeDay(t,e){return t+ft[e]}static toArray(){return["M","F"]}static MAX_MONTH_DAY=31}const At=new Date("1973-09-29"),Et=new i;class _t{belfioreConnector;constructor(t){this.belfioreConnector=t}OMOCODE_BITMAP=30400;cfDeomocode(t){if(t&&t.length<=6)return t;const e=this.partialCfDeomocode(t);if(e.length<r)return e;return e.substring(0,r)+this.appyCaseToChar(ht.checkDigit(e)||"",e.substring(r,16))}cfOmocode(t,e){if(!e)return this.cfDeomocode(t);const i=t.split("");for(let r=t.length-1,s=0;r>=0;r--)if(2**r&this.OMOCODE_BITMAP){if(!!(e&2**s)!==isNaN(parseInt(i[r],10))){const t=i[r].toUpperCase();i[r]=$t[t]}s++}const s=i[15];if(s){const t=i.slice(0,r).join("");i[15]=this.appyCaseToChar(ht.checkDigit(t)||"",s)}return i.join("")}cfOmocodeId(t){const e=t.split("").filter(((t,e)=>2**e&this.OMOCODE_BITMAP)).map((t=>/^[a-z]$/i.test(Et[t])?1:0)).join("");return parseInt(e,2)}cfToLastName(t){const e=t?.substring(0,3);if("string"!=typeof t||3!==e.length||!new RegExp(`^(?:${g})`,"iu").test(e))return null;const i=t.substring(0,3),[r=""]=i.match(new RegExp(`^[${p}]{1,3}`,"ig"))||[],[s=""]=i.match(new RegExp(`[${C}]{1,3}`,"ig"))||[],a=r.length+s.length;if(a<2||a<3&&"X"!==i[2].toUpperCase())return null;switch(r.length){case 3:return(r+s).split("").join(this.JOLLY_CHAR)+this.JOLLY_CHAR;case 2:return`${r[0]}${s[0]}*${r[1]}${this.JOLLY_CHAR}`;case 1:return`${r[0]}${s}${this.JOLLY_CHAR}`;default:return`${s}${3===s.length?this.JOLLY_CHAR:""}`}}cfToFirstName(t){const e=t?.substring(3,6);return"string"==typeof t&&3===e?.length&&new RegExp(`^(${y})$`,"iu").test(e)?this.cfToLastName(e):null}cfToGender(t){if("string"!=typeof t||t.length<10)return null;const e=t.substring(9,10),i=10*parseInt(this.partialCfDeomocode(e,9),10);return ut.getGender(i)}cfToBirthYear(e){if("string"!=typeof e||e.length<8)return null;const i=e.substring(6,8),r=parseInt(this.partialCfDeomocode(i,6),10);if(isNaN(r))return null;const s=parseInt(t().format("YY"),10),a=100*(r>s?1:0);return t().subtract(s-r+a,"years").year()}cfToBirthMonth(t){if("string"!=typeof t||t.length<9)return null;const e=t.substring(8,9).toUpperCase(),i=dt[e];return"number"!=typeof i||i<0||i>11?null:i}cfToBirthDay(t){if("string"!=typeof t||t.length<11)return null;const e=t.substring(9,11),i=parseInt(this.partialCfDeomocode(e,9),10);return isNaN(i)?null:ut.getDay(i)}cfToBirthDate(t){const e=this.cfToBirthDay(t);if(!e)return null;const i=this.cfToBirthMonth(t);if("number"!=typeof i)return null;const r=this.cfToBirthYear(t);return _.ymdToDate(r,i,e)}async cfToBirthPlace(e,i=!0){if("string"!=typeof e||e.length<15)return null;const r=e.substring(11,15),s=this.partialCfDeomocode(r,11),a=await this.belfioreConnector.findByCode(s);if(!a)return null;const{creationDate:n,expirationDate:o}=a;if((n||o)&&i){const i=this.cfToBirthDate(e),r=t(At).add(5,"years").isBefore(i,"day");if(i&&r){if(!((!o||t(i).isBefore(o,"day"))&&(!n||t(i).isAfter(n,"day"))))return null}}return a}async cfDecode(t){const e=this.cfToBirthYear(t)||void 0,i=this.cfToBirthMonth(t)??void 0,r=this.cfToBirthDay(t)||void 0,s=_.ymdToDate(e,i,r)||void 0,a=await this.cfToBirthPlace(t),n={firstName:this.cfToFirstName(t)||void 0,lastName:this.cfToLastName(t)||void 0,day:r,month:i,year:e,date:s,gender:this.cfToGender(t)||void 0,place:a||void 0,omocodeId:this.cfOmocodeId(t)};return e&&i&&r&&(n.date=new Date(Date.UTC(e,i,r))),n}lastNameToCf(t){if(!t||(t||"").trim().length<2)return null;if(!/^[A-Z ']{1,32}$/iu.test(Et.replace(t)))return null;const e=(this.charExtractor(t,p)+this.charExtractor(t,C)).padEnd(3,"X").substring(0,3);return e.length<3?null:e.toUpperCase()}firstNameToCf(t){if(!t||(t||"").trim().length<2)return null;const e=this.charExtractor(t,p);return e.length>=4?(e[0]+e.substring(2,4)).toUpperCase():this.lastNameToCf(t)}yearToCf(t){let e;return e="string"==typeof t?parseInt(t,10):t,"number"!=typeof e||isNaN(e)||!(e>=1900||e<100)?null:`0${e}`.substr(-2)}monthToCf(t){return t<0||t>11?null:dt[t]||null}dayGenderToCf(t,e){if(t<1||t>31)return null;const i=ft[e];return"number"!=typeof i?null:`0${t+i}`.substr(-2)}yearMonthDayToDate(e,i=0,r=1){if(!e||e<1861||[i,r].some((t=>"number"!=typeof t)))return null;const s=t(Date.UTC(e,i||0,r||1));return s.isValid()&&s.year()===e&&s.month()===i&&s.date()===r?s.toDate():null}async parsePlace(t){let e;return t?("object"==typeof t&&t.belfioreCode?e=await this.belfioreConnector.findByCode(t.belfioreCode):"string"==typeof t&&(e=await this.belfioreConnector.findByCode(t)||await this.belfioreConnector.findByName(t)),e||null):null}dateGenderToCf(t,e){const i=_.parseDate(t);if(!i)return null;return`${this.yearToCf(i.getFullYear())}${this.monthToCf(i.getMonth())}${this.dayGenderToCf(i.getDate(),e)}`}async placeToCf(t,e,i){const r=_.parseDate(t);let s,a;if(r||"string"!=typeof t){if(!e)return null;s=e,a=i}else s=t,a=e;let n=this.belfioreConnector;if(a&&(n=n.byProvince(a)),r&&n&&(n=n.from(r)),n){const t=await new _t(n).parsePlace(s);if(t)return t.belfioreCode}return null}async encodeCf({lastName:t,firstName:e,year:i,month:r,day:s,date:a,gender:n,place:o,omocodeId:c=0}){const h=_.parseDate(a)||this.yearMonthDayToDate(i,r,s);if(!(h&&t&&e&&n&&o))return null;const l=[async()=>this.lastNameToCf(t),async()=>this.firstNameToCf(e),async()=>this.dateGenderToCf(h,n),async()=>await this.placeToCf(h,o?.belfioreCode||o)];let d="";for(const t of l){const e=await t();if(!e)return null;d+=e}return this.cfOmocode(d,c)}JOLLY_CHAR="*";checkBitmap(t){return!!(2**t&this.OMOCODE_BITMAP)}charOmocode(t,e){return/^[A-Z]$/giu.test(t)&&this.checkBitmap(e)?$t[t.toUpperCase()]:t}charExtractor(t,e){const i=new RegExp(`[${e}]{1,24}`,"ig");return(Et.replace(t).trim().match(i)||[]).join("")}partialCfDeomocode(t,e=0){return t.replace(/[\dA-Z]/giu,((t,i)=>this.charOmocode(t,i+e)))}appyCaseToChar(t,e){if(t&&e){const i=e[0]===e[0].toUpperCase(),r=e[0]===e[0].toLowerCase();if(i&&!r)return t[0].toUpperCase();if(!i&&r)return t[0].toLowerCase()}return t[0]}}const pt="Provided lastName is not valid, only letters, diacritics and apostrophe allowed",Ct="Provided name is not valid, only letters, diacritics and apostrophe allowed",mt="Provided day is not valid",Rt="Provided gender is not valid",Dt="Provided day and/or gender are not valid",Nt="Provided year is not valid, only 2 or 4 digit numbers are allowed",Mt="Provided date is not valid";var It=Object.freeze({__proto__:null,INVALID_DATE:Mt,INVALID_DAY:mt,INVALID_DAY_OR_GENDER:Dt,INVALID_GENDER:Rt,INVALID_NAME:Ct,INVALID_PLACE_NAME:"Proviced City/Country name is not valid",INVALID_SURNAME:pt,INVALID_YEAR:Nt});class St extends Error{constructor(t){super((Object.entries(It).find((([e])=>e===t))||[])[1]||t)}}const Ot=new i;class yt{belfioreConnector;parser;constructor(t){this.belfioreConnector=t,this.parser=new _t(t)}cfLastName(t){let e=g;if(t){if(!this.lastName().test(t))throw new St(pt);e=this.parser.lastNameToCf(t)||e}return this.isolatedInsensitiveTailor(e)}cfFirstName(t){let e=y;if(t){if(!this.lastName().test(t))throw new St(Ct);e=this.parser.firstNameToCf(t)||e}return this.isolatedInsensitiveTailor(e)}cfYear(t){let e=P;if(t){const i=this.parser.yearToCf(t);if(!i)throw new St(Nt);e=this.deomocode(i)}return this.isolatedInsensitiveTailor(e)}cfMonth(t){let e=w;return t&&(e=this.parser.monthToCf(t)||e),this.isolatedInsensitiveTailor(e)}cfDay(t){let e=V;if(t){const i=this.parser.dayGenderToCf(t,"M"),r=this.parser.dayGenderToCf(t,"F");if(!i||!r)throw new St(mt);e=`(?:${this.deomocode(i)})|(?:${this.deomocode(r)})`}return this.isolatedInsensitiveTailor(e)}cfDayGender(t,e){if(!e)return this.cfDay(t);let i;if(t){const r=this.parser.dayGenderToCf(t,e);if(!r)throw new St(Dt);i=this.deomocode(r)}else switch(e){case"M":i=G;break;case"F":i=U;break;default:throw new St(Rt)}return this.isolatedInsensitiveTailor(i)}cfDateGender(t,e){if(t&&!_.parseDate(t))throw new St(Mt);if(e&&!ut.toArray().includes(e))throw new St(Rt);let i=x;if(t){const r=e&&this.parser.dateGenderToCf(t,e);if(r)i=this.deomocode(r);else{const e=e=>{const i=this.parser.dateGenderToCf(t,e);if(!i)throw new St(Mt);return i&&this.deomocode(i)};i=`(?:${ut.toArray().map(e).join("|")})`}}else"M"===e?i=Z:"F"===e&&(i=k);return this.isolatedInsensitiveTailor(i)}async cfPlace(t,e){let i=Q;if(t){const r=_.parseDate(t);if(r&&e){const t=e,s=await this.parser.placeToCf(r,t);i=this.deomocode(s||"")}else if(!r&&"string"==typeof t){const e=t,r=await this.parser.placeToCf(e);i=this.deomocode(r||"")}}return this.isolatedInsensitiveTailor(i)}async codiceFiscale(t){let e=K;if(t){const i=await this.parser.encodeCf(t);if(i)e=this.deomocode(i);else{const{lastName:i,firstName:r,year:s,month:a,day:n,date:o,gender:c,place:h}=t;if(i||r||s||a||n||o||c||h){let t=null;o?t=_.parseDate(o):s&&(t=this.parser.yearMonthDayToDate(s,a,n));const l=[async()=>this.cfLastName(i),async()=>this.cfFirstName(r),async()=>this.cfDateGender(t,c),async()=>await this.cfPlace(t,h?.belfioreCode||h)];e="";for(const t of l){const i=(await t()).toString(),r=i.match(/\^(.{1,256})\$/),s=r&&r[1];if(!s)throw new Error(`Unable to handle [${i}]`);e+=`(?:${s})`}e+=z}}}return this.isolatedInsensitiveTailor(e)}LETTER_SET=`[A-Z${Ot.matcherBy(/^[A-Z]$/iu)}]`;SEPARATOR_SET="(?:'?\\s{0,4})";lastName(t){let e=`${this.LETTER_SET}{1,24}`;if(t&&/^[A-Z]{1,3}/iu.test(t)){const i=t.substr(0,3),r=t=>t.split("").map((t=>`[${Ot.insensitiveMatcher[t]}]`)),[s,a]=[`^[${p}]{1,3}`,`[${C}]{1,3}`].map((t=>r((i.match(new RegExp(t,"ig"))||[])[0]||""))),n=`[${C+Ot.matcherBy(new RegExp(`^[${C}]$`,"ui"))}]`,o=`(?:${n}${this.SEPARATOR_SET}){0,24}`,c=`(?:${this.SEPARATOR_SET}${o}${n})?`;switch(s.length){case 3:{const t=o;e=t+s.join(`${this.SEPARATOR_SET}${t}`)+`(?:${this.SEPARATOR_SET}${this.LETTER_SET}{0,24}${this.LETTER_SET})?`;break}case 2:e=`(?:${[`${a[0]}${o}${this.SEPARATOR_SET}${s[0]}${o}${s[1]}`,`${s[0]}${this.SEPARATOR_SET}`+a.join(`${this.SEPARATOR_SET}`)+`${this.SEPARATOR_SET}${o}${s[1]}`,s.join(`${this.SEPARATOR_SET}`)+`${this.SEPARATOR_SET}${a[0]}`].join("|")})${c}`;break;case 1:e=`(?:${[a.slice(0,2).join(`${this.SEPARATOR_SET}`)+o+s.join(`${this.SEPARATOR_SET}`),`${a[0]}${this.SEPARATOR_SET}`+s.join(`${this.SEPARATOR_SET}`)+a[1],[s[0],...a.slice(0,2)].join(`${this.SEPARATOR_SET}`)].join("|")})${c}`;break;default:e=`${a.join(`${this.SEPARATOR_SET}`)}${c}`}if(a?.length+s?.length<3)return this.isolatedInsensitiveTailor(`\\s{0,4}(${e})\\s{0,4}`)}return this.isolatedInsensitiveTailor(`\\s{0,4}((?:${e})(?:${this.SEPARATOR_SET}${this.LETTER_SET}{1,24}){0,24})\\s{0,4}`)}firstName(t){if(t&&new RegExp(`^[A-Z]{3}[${p}]{3}`,"iu").test(t)){const e=((t.substr(3,3).match(new RegExp(`^[${p}]{1,3}`,"ig"))||[])[0]||"").split("").map((t=>`[${Ot.insensitiveMatcher[t]}]`)),[i,r]=[C,p].map((t=>t+Ot.matcherBy(new RegExp(`^[${t}]$`,"ui")))),s=`(?:[${i}]{1,24}${this.SEPARATOR_SET}){0,24}${e[0]}${this.SEPARATOR_SET}(?:[${i}]{1,24}${this.SEPARATOR_SET}){0,24}(?:[${r}]${this.SEPARATOR_SET}(?:[${i}]{1,24}${this.SEPARATOR_SET}){0,24})?`+e.slice(1,3).join(`${this.SEPARATOR_SET}(?:[${i}]{1,24}${this.SEPARATOR_SET}){0,24}`)+`(?:${this.SEPARATOR_SET}${this.LETTER_SET}{1,24}){0,24}`;return this.isolatedInsensitiveTailor(s)}return this.lastName((t||"").substr(3,3))}date(e){let i=A;if(e){const r=this.parser.cfToBirthDate(e);if(r){const e=r.toJSON();if(t().diff(t(r),"y")<50){const t=parseInt(e.substr(0,2),10);i=`(?:${[t-1,t].map((t=>t.toString().padStart(2,"0"))).join("|")})`+e.substr(2,8)}else i=e.substr(0,10)}}return this.isolatedInsensitiveTailor(`${i}(?:T${$}(?:${f})?)?`)}gender(t){const e=t&&this.parser.cfToGender(t)||`[${ut.toArray().join("")}]`;return this.isolatedInsensitiveTailor(e)}async place(t){let e=".{1,32}";const i=t&&await this.parser.cfToBirthPlace(t);if(i){e=`(?:(?:${i.name.replace(/./gu,(t=>Ot[t]===t?t:`[${t}${Ot[t]}]`))})|${i.belfioreCode})`}return this.isolatedInsensitiveTailor(e)}deomocode(t){return t.replace(/\d/gu,(t=>`[${t}${$t[t]}]`))}isolatedInsensitiveTailor(t){return new RegExp(`^(?:${t})$`,"iu")}}class gt{codiceFiscale;pattern;parser;constructor(t,e){this.codiceFiscale=e,this.pattern=new yt(t),this.parser=new _t(t)}get hasLastName(){return this.codiceFiscale?.length>=3}get hasFirstName(){return this.codiceFiscale?.length>=6}get hasBirthYear(){return this.codiceFiscale?.length>=8}get hasBirthDate(){return this.codiceFiscale?.length>=11}get hasGender(){return this.codiceFiscale?.length>=10}get hasBirthPlace(){return this.codiceFiscale?.length>=15}get hasCRC(){return this.codiceFiscale?.length>=16}async matchPersonalInfo(t){return(await this.pattern.codiceFiscale(t)).test(this.codiceFiscale)}async mismatchPersonalInfo(t){return!(!(this.codiceFiscale&&t&&t.lastName&&t.firstName&&(t.date||t.day&&"number"==typeof t.month&&t.year)&&t.gender&&t.place)||await this.matchPersonalInfo(t))}matchLastName(t){return this.hasLastName&&this.pattern.lastName(this.codiceFiscale).test(t||"")}mismatchLastName(t){return this.hasLastName&&!!t&&!this.matchLastName(t)}matchFirstName(t){return this.hasFirstName&&this.pattern.firstName(this.codiceFiscale).test(t||"")}mismatchFirstName(t){return this.hasFirstName&&!!t&&!this.matchFirstName(t)}matchBirthDate(t){if(this.hasBirthDate){const e=_.parseDate(t),i=this.pattern.date(this.codiceFiscale);return!!e&&i.test(e.toJSON())}return!1}mismatchBirthDate(t){return this.hasBirthYear&&!!_.parseDate(t)&&!this.matchBirthDate(t)}matchGender(t){return this.hasGender&&this.pattern.gender(this.codiceFiscale).test(t||"")}mismatchGender(t){return this.hasGender&&!!t&&!this.matchGender(t)}async matchBirthPlace(t){if(this.hasBirthPlace&&t){const e=await this.pattern.place(this.codiceFiscale),i=await this.parser.parsePlace(t);return!!i&&e.test(i?.belfioreCode)}return!1}async mismatchBirthPlace(t){return this.hasBirthPlace&&!!t&&!await this.matchBirthPlace(t)}get errors(){return Promise.all([this.parser.cfToBirthPlace(this.codiceFiscale,!1),this.parser.cfToBirthPlace(this.codiceFiscale,!0)]).then((([t,e])=>({...this.parser.cfToLastName(this.codiceFiscale)?{}:{lastName:"MISSING_OR_INVALID_LAST_NAME"},...this.parser.cfToFirstName(this.codiceFiscale)?{}:{firstName:"MISSING_OR_INVALID_FIRST_NAME"},...this.parser.cfToBirthDate(this.codiceFiscale)?{}:{date:"MISSING_OR_INVALID_DATE"},...this.parser.cfToBirthDay(this.codiceFiscale)?{}:{date:"MISSING_OR_INVALID_DAY"},..."number"==typeof this.parser.cfToBirthMonth(this.codiceFiscale)?{}:{date:"MISSING_OR_INVALID_MONTH"},...this.parser.cfToBirthYear(this.codiceFiscale)?{}:{date:"MISSING_OR_INVALID_YEAR"},...this.parser.cfToGender(this.codiceFiscale)?{}:{gender:"MISSING_DAY"},...t?{}:{place:"MISSING_OR_INVALID_PLACE"},...e?{}:{place:"PLACE_EXPIRED_OR_NOT_YET_CREATED_ON_BIRTDATE",date:"BIRTHDATE_OUT_OF_BIRTH_PLACE_LIFE_RANGE"},...this.codiceFiscale?.substring(r,16)?.toUpperCase()===ht.checkDigit(this.codiceFiscale)?{}:{crc:"INVALID_CRC_CODE"},...this.hasCRC?{}:{crc:"MISSING_CRC_CODE"}}))).then((t=>Object.keys(t).length?t:null))}get valid(){return Promise.all([this.pattern.codiceFiscale(),this.parser.cfToBirthPlace(this.codiceFiscale)]).then((([t,e])=>!(!this.hasCRC||!t.test(this.codiceFiscale)||this.codiceFiscale?.substring(r,16)?.toUpperCase()!==ht.checkDigit(this.codiceFiscale)||!e)))}get invalid(){return this.valid.then((t=>!!this.codiceFiscale&&!t))}}class Lt{belfioreConnector;parser;pattern;constructor(t){this.belfioreConnector=t,this.parser=new _t(t),this.pattern=new yt(t)}codiceFiscale(t){return new gt(this.belfioreConnector,t)}isLastNameValid(t){return this.pattern.lastName().test(t)}isLastNameInvalid(t){return!!t&&!this.isLastNameValid(t)}isFirstNameValid(t){return this.pattern.firstName().test(t)}isFirstNameInvalid(t){return!!t&&!this.isFirstNameValid(t)}isBirthDateValid(t){return!!_.parseDate(t)}isBirthDateInvalid(t){return!!t&&!this.isBirthDateValid(t)}isGenderValid(t){return this.pattern.gender().test(t)}isGenderInvalid(t){return!!t&&!this.isGenderValid(t)}async isBirthPlaceValid(t){const e=await this.parser.parsePlace(t);return!!e&&!!await this.belfioreConnector.findByCode(e.belfioreCode)}async isBirthPlaceInvalid(t){return!!t&&!await this.isBirthPlaceValid(t)}async birthDatePlaceMatch(e,i){const r=await this.parser.parsePlace(i);return this.isBirthDateValid(e)&&!!r&&(!!await this.belfioreConnector.from(e).findByCode(r.belfioreCode)||!t(_.parseDate(e)).isAfter(At,"day"))}async birthDatePlaceMismatch(t,e){const i=await this.parser.parsePlace(e);return this.isBirthDateValid(t)&&!!i&&!await this.birthDatePlaceMatch(t,e)}async birthPlaceDateMatch(t,e){return await this.birthDatePlaceMatch(e,t)}async birthPlaceDateMismatch(t,e){return await this.birthDatePlaceMismatch(e,t)}}class Pt{belfioreConnector;validator;parser;pattern;constructor(t){this.belfioreConnector=t,this.validator=new Lt(t),this.parser=new _t(t),this.pattern=new yt(t)}}export{dt as BirthMonth,gt as CFMismatchValidator,ct as CRC,ht as CheckDigitizer,Pt as CodiceFiscaleUtils,E as DATE_MATCHER,_ as DateUtils,ut as Gender,nt as Matcher,$t as Omocodes,_t as Parser,yt as Pattern,nt as VALIDATOR,Lt as Validator,Pt as default};
//# sourceMappingURL=index.mjs.map