polish-utils
Version:
Lightweight library for extracting data and validating polish identification numbers such as PESEL or NIP
2 lines (1 loc) • 722 B
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});exports.PESEL=class{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,s]=this.pesel.match(/.{1,2}/g).map((e=>+e||0)),l=1900;return t>20&&t<40?(l=2e3,t-=20):t>40&&t<60?(l=2100,t-=40):t>60&&t<80?(l=2200,t-=60):t>80&&t<99&&(l=1800,t-=80),e&&t&&s?new Date(l+e,--t,s):null}validate(){if(!/^[0-9]{11}$/.test(this.pesel))return!1;const e=[1,3,7,9],t=`${this.pesel}`.split("").map((e=>+e)),s=t.splice(-1)[0],l=t.reduce(((t,s,l)=>t+s*e[l%4]))%10;return 10-(0===l?10:l)===s}};