UNPKG

isikukood

Version:

Estonian personal ID (isikukood) JavaScript module

4 lines (3 loc) 2.5 kB
// isikukood.esm.js, https://github.com/dknight/Isikukood-js "use strict";class l{constructor(t){this._code=String(t)}get code(){return this._code}set code(t){this._code=String(t)}getControlNumber(t=""){t||(t=this.code);const r=[1,2,3,4,5,6,7,8,9,1],a=[3,4,5,6,7,8,9,1,2,3];let n=0,i=0;for(let e=0;e<10;++e)i+=Number(t.charAt(e))*r[e];if(n=i%11,i=0,n===10){for(let e=0;e<10;++e)i+=Number(t.charAt(e))*a[e];n=i%11,n===10&&(n=0)}return n}validate(){if(this.code.charAt(0)==="0"||this.code.length!==11||this.getControlNumber()!==Number(this.code.charAt(10)))return!1;const t=Number(this.code.substring(1,3)),r=Number(this.code.substring(3,5)),a=Number(this.code.substring(5,7)),n=this.getBirthday();return t===n.getFullYear()%100&&n.getMonth()+1===r&&a===n.getDate()}getGender(){const t=this.code.charAt(0),r=["1","3","5"],a=["2","4","6"];return r.includes(t)?"male":a.includes(t)?"female":"unknown"}getAge(){const t=this.getBirthday(),r=new Date;let a=r.getFullYear()-t.getFullYear();const n=r.getMonth()-t.getMonth();return(n<0||n===0&&r.getDate()<t.getDate())&&a--,a}getBirthday(){let t=Number(this.code.substring(1,3));const r=Number(this.code.substring(3,5).replace(/^0/,""))-1,a=Number(this.code.substring(5,7).replace(/^0/,"")),n=this.code.charAt(0);for(let i=1,e=1800;i<=8;i+=2,e+=100)[i,i+1].map(String).includes(n)&&(t+=e);return new Date(t,r,a)}parse(){return l.parse(this.code)}static parse(t){const r=new this(t);return{gender:r.getGender(),birthDay:r.getBirthday(),age:r.getAge()}}static generate(t={}){let r,a,n;const i=t.gender||(Math.round(Math.random())===0?"male":"female");let e="";const c=["00","01","02","22","27","37","42","47","49","52","57","60","65","70","95"];if(!["male","female"].includes(i))return"";if(t.birthYear?r=t.birthYear:r=Math.round(Math.random()*100+1900+(new Date().getFullYear()-2e3)),t.birthMonth?a=t.birthMonth:a=Math.floor(Math.random()*12)+1,t.birthDay)n=t.birthDay;else{const o=new Date(r,a,0).getDate();n=Math.floor(Math.random()*o)+1}for(let o=1800,h=2;o<=2100;o+=100,h+=2)if(r>=o&&r<o+100)switch(i){case"male":e+=String(h-1);break;case"female":e+=String(h);break;default:return""}return e+=String(r).substring(2,4),e+=String(a).padStart(2,"0"),e+=String(n).padStart(2,"0"),e+=c[Math.floor(Math.random()*c.length)],e+=String(Math.floor(Math.random()*10)),e+=String(this.prototype.getControlNumber(e)),e}}var u=(s=>(s.MALE="male",s.FEMALE="female",s.UNKNOWN="unknown",s))(u||{});export{u as Gender,l as default}; //# sourceMappingURL=isikukood.esm.min.js.map