@ppasmik/birth-number-utils
Version:
A TypeScript/JavaScript library for generating, validating, and parsing Czech and Slovak birth numbers (rodné číslo).
3 lines (2 loc) • 8.78 kB
JavaScript
;var t=require("dayjs");"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self&&self;function e(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var r,s,i,n={exports:{}},a=e(n.exports=(r="minute",s=/[+-]\d\d(?::?\d\d)?/g,i=/([+-]|\d\d)/g,function(t,e,n){var a=e.prototype;n.utc=function(t){return new e({date:t,utc:!0,args:arguments})},a.utc=function(t){var e=n(this.toDate(),{locale:this.$L,utc:!0});return t?e.add(this.utcOffset(),r):e},a.local=function(){return n(this.toDate(),{locale:this.$L,utc:!1})};var o=a.parse;a.parse=function(t){t.utc&&(this.$u=!0),this.$utils().u(t.$offset)||(this.$offset=t.$offset),o.call(this,t)};var u=a.init;a.init=function(){if(this.$u){var t=this.$d;this.$y=t.getUTCFullYear(),this.$M=t.getUTCMonth(),this.$D=t.getUTCDate(),this.$W=t.getUTCDay(),this.$H=t.getUTCHours(),this.$m=t.getUTCMinutes(),this.$s=t.getUTCSeconds(),this.$ms=t.getUTCMilliseconds()}else u.call(this)};var h=a.utcOffset;a.utcOffset=function(t,e){var n=this.$utils().u;if(n(t))return this.$u?0:n(this.$offset)?h.call(this):this.$offset;if("string"==typeof t&&(t=function(t){void 0===t&&(t="");var e=t.match(s);if(!e)return null;var r=(""+e[0]).match(i)||["-",0,0],n=r[0],a=60*+r[1]+ +r[2];return 0===a?0:"+"===n?a:-a}(t),null===t))return this;var a=Math.abs(t)<=16?60*t:t,o=this;if(e)return o.$offset=a,o.$u=0===t,o;if(0!==t){var u=this.$u?this.toDate().getTimezoneOffset():-1*this.utcOffset();(o=this.local().add(a+u,r)).$offset=a,o.$x.$localOffset=u}else o=this.utc();return o};var l=a.format;a.format=function(t){var e=t||(this.$u?"YYYY-MM-DDTHH:mm:ss[Z]":"");return l.call(this,e)},a.valueOf=function(){var t=this.$utils().u(this.$offset)?0:this.$offset+(this.$x.$localOffset||this.$d.getTimezoneOffset());return this.$d.valueOf()-6e4*t},a.isUTC=function(){return!!this.$u},a.toISOString=function(){return this.toDate().toISOString()},a.toString=function(){return this.toDate().toUTCString()};var f=a.toDate;a.toDate=function(t){return"s"===t&&this.$offset?n(this.format("YYYY-MM-DD HH:mm:ss:SSS")).toDate():f.call(this)};var d=a.diff;a.diff=function(t,e,r){if(t&&this.$u===t.$u)return d.call(this,t,e,r);var s=this.local(),i=n(t).local();return d.call(s,i,e,r)}}));t.extend(a);const o=t=>t.replace(/\//g,""),u=18,h=53,l=1900,f=2e3,d=2004,c=50,p=20,m=11,g=0,M=10,D=0,$=/^(\d\d)(\d\d)(\d\d)\/?(\d\d\d\d?)$/,v=e=>{const r=o(e),s=$.exec(r);if(!s)return!1;const[i,n,a,v,Y]=s,V=4===Y.length;if(V&&!((t,e,r,s)=>{const i=+(t+e+r+s),n=parseInt(i.toString().slice(0,-1)),a=parseInt(i.toString().slice(-1));return i%m===g||n%m===M&&a===D})(n,a,v,Y))return!1;const w=((t,e)=>{const r=parseInt(t,10);return!e&&r<=h||e&&r>h?r+l:!!(e&&r<=h)&&r+f})(n,V);if(!1===w)return!1;const S=parseInt(a,10),{month:b,gender:y,isValid:x}=((t,e)=>{if(0===t)return{month:t,gender:"MALE",isValid:!1};let r="MALE";if(t>c&&(r="FEMALE",t-=c),t>p){if(!(e>=d))return{month:t,gender:r,isValid:!1};t=t%p||p}return{month:t,gender:r,isValid:!0}})(S,w);if(!x)return!1;const L=parseInt(v,10),T=t.utc(`${w}-${b.toString().padStart(2,"0")}-${L}`,"YYYY-MM-DD",!0);if(!T.isValid())return!1;const O=t().diff(T,"year");return{birthDate:T.toDate(),gender:y,age:O,isAdult:O>=u,isMale:"MALE"===y,isFemale:"FEMALE"===y,birthDateAsString:(A=T,A.format("DD.MM.YYYY")),error:null};var A};class Y{constructor(t){this.rawValue=o(t),this.parsedValue=v(this.rawValue)}year(){return this.parsedValue?this.parsedValue.birthDate.getFullYear():null}month(){return this.parsedValue?this.parsedValue.birthDate.getMonth():null}day(){return this.parsedValue?this.parsedValue.birthDate.getDate():null}isBorn(){return!!this.parsedValue&&this.parsedValue.birthDate<=new Date}isMale(){return!!this.parsedValue&&this.parsedValue.isMale}isFemale(){return!!this.parsedValue&&this.parsedValue.isFemale}birthDate(){return this.parsedValue?new Date(this.parsedValue.birthDate):null}birthDateAsString(){return this.parsedValue?this.parsedValue.birthDateAsString:null}isValid(){return!1!==this.parsedValue&&this.isBorn()}isPossible(){return!1!==this.parsedValue}age(){return this.parsedValue?this.parsedValue.age:null}isAdult(t=18){return!!this.parsedValue&&this.parsedValue.age>=t}error(){return this.parsedValue?this.isBorn()?this.parsedValue.error:"Birth date is in future":"Invalid birth number format"}}var V={exports:{}};V.exports=function(){var t={LTS:"h:mm:ss A",LT:"h:mm A",L:"MM/DD/YYYY",LL:"MMMM D, YYYY",LLL:"MMMM D, YYYY h:mm A",LLLL:"dddd, MMMM D, YYYY h:mm A"},e=/(\[[^[]*\])|([-_:/.,()\s]+)|(A|a|Q|YYYY|YY?|ww?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g,r=/\d/,s=/\d\d/,i=/\d\d?/,n=/\d*[^-_:/,()\s\d]+/,a={},o=function(t){return(t=+t)+(t>68?1900:2e3)},u=function(t){return function(e){this[t]=+e}},h=[/[+-]\d\d:?(\d\d)?|Z/,function(t){(this.zone||(this.zone={})).offset=function(t){if(!t)return 0;if("Z"===t)return 0;var e=t.match(/([+-]|\d\d)/g),r=60*e[1]+(+e[2]||0);return 0===r?0:"+"===e[0]?-r:r}(t)}],l=function(t){var e=a[t];return e&&(e.indexOf?e:e.s.concat(e.f))},f=function(t,e){var r,s=a.meridiem;if(s){for(var i=1;i<=24;i+=1)if(t.indexOf(s(i,0,e))>-1){r=i>12;break}}else r=t===(e?"pm":"PM");return r},d={A:[n,function(t){this.afternoon=f(t,!1)}],a:[n,function(t){this.afternoon=f(t,!0)}],Q:[r,function(t){this.month=3*(t-1)+1}],S:[r,function(t){this.milliseconds=100*+t}],SS:[s,function(t){this.milliseconds=10*+t}],SSS:[/\d{3}/,function(t){this.milliseconds=+t}],s:[i,u("seconds")],ss:[i,u("seconds")],m:[i,u("minutes")],mm:[i,u("minutes")],H:[i,u("hours")],h:[i,u("hours")],HH:[i,u("hours")],hh:[i,u("hours")],D:[i,u("day")],DD:[s,u("day")],Do:[n,function(t){var e=a.ordinal,r=t.match(/\d+/);if(this.day=r[0],e)for(var s=1;s<=31;s+=1)e(s).replace(/\[|\]/g,"")===t&&(this.day=s)}],w:[i,u("week")],ww:[s,u("week")],M:[i,u("month")],MM:[s,u("month")],MMM:[n,function(t){var e=l("months"),r=(l("monthsShort")||e.map((function(t){return t.slice(0,3)}))).indexOf(t)+1;if(r<1)throw new Error;this.month=r%12||r}],MMMM:[n,function(t){var e=l("months").indexOf(t)+1;if(e<1)throw new Error;this.month=e%12||e}],Y:[/[+-]?\d+/,u("year")],YY:[s,function(t){this.year=o(t)}],YYYY:[/\d{4}/,u("year")],Z:h,ZZ:h};function c(r){var s,i;s=r,i=a&&a.formats;for(var n=(r=s.replace(/(\[[^\]]+])|(LTS?|l{1,4}|L{1,4})/g,(function(e,r,s){var n=s&&s.toUpperCase();return r||i[s]||t[s]||i[n].replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g,(function(t,e,r){return e||r.slice(1)}))}))).match(e),o=n.length,u=0;u<o;u+=1){var h=n[u],l=d[h],f=l&&l[0],c=l&&l[1];n[u]=c?{regex:f,parser:c}:h.replace(/^\[|\]$/g,"")}return function(t){for(var e={},r=0,s=0;r<o;r+=1){var i=n[r];if("string"==typeof i)s+=i.length;else{var a=i.regex,u=i.parser,h=t.slice(s),l=a.exec(h)[0];u.call(e,l),t=t.replace(l,"")}}return function(t){var e=t.afternoon;if(void 0!==e){var r=t.hours;e?r<12&&(t.hours+=12):12===r&&(t.hours=0),delete t.afternoon}}(e),e}}return function(t,e,r){r.p.customParseFormat=!0,t&&t.parseTwoDigitYear&&(o=t.parseTwoDigitYear);var s=e.prototype,i=s.parse;s.parse=function(t){var e=t.date,s=t.utc,n=t.args;this.$u=s;var o=n[1];if("string"==typeof o){var u=!0===n[2],h=!0===n[3],l=u||h,f=n[2];h&&(f=n[2]),a=this.$locale(),!u&&f&&(a=r.Ls[f]),this.$d=function(t,e,r,s){try{if(["x","X"].indexOf(e)>-1)return new Date(("X"===e?1e3:1)*t);var i=c(e)(t),n=i.year,a=i.month,o=i.day,u=i.hours,h=i.minutes,l=i.seconds,f=i.milliseconds,d=i.zone,p=i.week,m=new Date,g=o||(n||a?1:m.getDate()),M=n||m.getFullYear(),D=0;n&&!a||(D=a>0?a-1:m.getMonth());var $,v=u||0,Y=h||0,V=l||0,w=f||0;return d?new Date(Date.UTC(M,D,g,v,Y,V,w+60*d.offset*1e3)):r?new Date(Date.UTC(M,D,g,v,Y,V,w)):($=new Date(M,D,g,v,Y,V,w),p&&($=s($).week(p).toDate()),$)}catch(t){return new Date("")}}(e,o,s,r),this.init(),f&&!0!==f&&(this.$L=this.locale(f).$L),l&&e!=this.format(o)&&(this.$d=new Date("")),a={}}else if(o instanceof Array)for(var d=o.length,p=1;p<=d;p+=1){n[1]=o[p-1];var m=r.apply(this,n);if(m.isValid()){this.$d=m.$d,this.$L=m.$L,this.init();break}p===d&&(this.$d=new Date(""))}else i.call(this,t)}}}();var w=e(V.exports);t.extend(a),t.extend(w);const S=(e={})=>{const r=e.birthDate?t(e.birthDate):t().subtract(Math.floor(72*Math.random()+18),"years"),s=r.format("YY"),i=r.format("DD");let n=r.month()+1;"FEMALE"===e.gender&&(n+=50);const a=s+n.toString().padStart(2,"0")+i;let o=Math.floor(1e4*Math.random()).toString().padStart(4,"0"),u=parseInt(a+o,10);for(;u%11!=0;)o=Math.floor(1e4*Math.random()).toString().padStart(4,"0"),u=parseInt(a+o,10);return`${a}/${o}`},b=S,y=S;exports.BirthNumber=Y,exports.generateBirthNumber=S,exports.generateCzechBirthNumber=b,exports.generateSlovakBirthNumber=y,exports.isValidBirthNumber=t=>{const e=o(t);return!1!==v(e)},exports.parseBirthNumber=v,exports.rodnecislo=t=>new Y(t);
//# sourceMappingURL=index.js.map