isikukood
Version:
Estonian personal ID (isikukood) JavaScript module
4 lines (3 loc) • 2.96 kB
JavaScript
// isikukood.js, https://github.com/dknight/Isikukood-js
;var Isikukood=(()=>{var l=Object.defineProperty,f=Object.getOwnPropertyDescriptor,b=Object.getOwnPropertyNames,m=Object.prototype.hasOwnProperty,y=(i,t)=>{for(var e in t)l(i,e,{get:t[e],enumerable:!0})},p=(i,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of b(t))!m.call(i,r)&&r!==e&&l(i,r,{get:()=>t[r],enumerable:!(o=f(t,r))||o.enumerable});return i},M=i=>p(l({},"__esModule",{value:!0}),i),c={};y(c,{Gender:()=>g,default:()=>u});class u{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 e=[1,2,3,4,5,6,7,8,9,1],o=[3,4,5,6,7,8,9,1,2,3];let r=0,a=0;for(let n=0;n<10;++n)a+=Number(t.charAt(n))*e[n];if(r=a%11,a=0,r===10){for(let n=0;n<10;++n)a+=Number(t.charAt(n))*o[n];r=a%11,r===10&&(r=0)}return r}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)),e=Number(this.code.substring(3,5)),o=Number(this.code.substring(5,7)),r=this.getBirthday();return t===r.getFullYear()%100&&r.getMonth()+1===e&&o===r.getDate()}getGender(){const t=this.code.charAt(0),e=["1","3","5"],o=["2","4","6"];return e.includes(t)?"male":o.includes(t)?"female":"unknown"}getAge(){const t=this.getBirthday(),e=new Date;let o=e.getFullYear()-t.getFullYear();const r=e.getMonth()-t.getMonth();return(r<0||r===0&&e.getDate()<t.getDate())&&o--,o}getBirthday(){let t=Number(this.code.substring(1,3));const e=Number(this.code.substring(3,5).replace(/^0/,""))-1,o=Number(this.code.substring(5,7).replace(/^0/,"")),r=this.code.charAt(0);for(let a=1,n=1800;a<=8;a+=2,n+=100)[a,a+1].map(String).includes(r)&&(t+=n);return new Date(t,e,o)}parse(){return u.parse(this.code)}static parse(t){const e=new this(t);return{gender:e.getGender(),birthDay:e.getBirthday(),age:e.getAge()}}static generate(t={}){let e,o,r;const a=t.gender||(Math.round(Math.random())===0?"male":"female");let n="";const d=["00","01","02","22","27","37","42","47","49","52","57","60","65","70","95"];if(!["male","female"].includes(a))return"";if(t.birthYear?e=t.birthYear:e=Math.round(Math.random()*100+1900+(new Date().getFullYear()-2e3)),t.birthMonth?o=t.birthMonth:o=Math.floor(Math.random()*12)+1,t.birthDay)r=t.birthDay;else{const s=new Date(e,o,0).getDate();r=Math.floor(Math.random()*s)+1}for(let s=1800,h=2;s<=2100;s+=100,h+=2)if(e>=s&&e<s+100)switch(a){case"male":n+=String(h-1);break;case"female":n+=String(h);break;default:return""}return n+=String(e).substring(2,4),n+=String(o).padStart(2,"0"),n+=String(r).padStart(2,"0"),n+=d[Math.floor(Math.random()*d.length)],n+=String(Math.floor(Math.random()*10)),n+=String(this.prototype.getControlNumber(n)),n}}var g=(i=>(i.MALE="male",i.FEMALE="female",i.UNKNOWN="unknown",i))(g||{});return M(c)})();window.Isikukood=Isikukood.default;
//# sourceMappingURL=isikukood.min.js.map