UNPKG

polish-utils

Version:

Lightweight library for extracting data and validating polish identification numbers such as PESEL or NIP

2 lines (1 loc) 660 B
class e{constructor(e){this.pesel=e,this.valid=this.validate()}get isValid(){return this.valid}getGender(){if(this.pesel.length<10)return null;return+this.pesel[9]%2==0?"female":"male"}getDateOfBirth(){if(this.pesel.length<6)return null;let[e,t,l]=this.pesel.match(/.{1,2}/g).map((e=>+e||0)),s=1900;return t>20&&t<40?(s=2e3,t-=20):t>40&&t<60?(s=2100,t-=40):t>60&&t<80?(s=2200,t-=60):t>80&&t<99&&(s=1800,t-=80),e&&t&&l?new Date(s+e,--t,l):null}validate(){if(!/^[0-9]{11}$/.test(this.pesel))return!1;const e=[1,3,7,9],t=`${this.pesel}`.split("").map((e=>+e)),l=t.splice(-1)[0],s=t.reduce(((t,l,s)=>t+l*e[s%4]))%10;return 10-(0===s?10:s)===l}}export{e as PESEL};