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