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