headless-datetimepicker
Version:
React headless datepicker
2 lines (1 loc) • 8.88 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=require("date-fns");var p={toJalaali:q,toGregorian:R,isValidJalaaliDate:B,isLeapJalaaliYear:Z,jalaaliMonthLength:$,jalCal:E,j2d:Y,d2j:j,g2d:H,d2g:A,jalaaliToDateObject:P,jalaaliWeek:Q},y=[-61,9,38,199,426,686,756,818,1111,1181,1210,1635,2060,2097,2192,2262,2324,2394,2456,3178];function q(e,t,r){return Object.prototype.toString.call(e)==="[object Date]"&&(r=e.getDate(),t=e.getMonth()+1,e=e.getFullYear()),j(H(e,t,r))}function R(e,t,r){return A(Y(e,t,r))}function B(e,t,r){return e>=-61&&e<=3177&&t>=1&&t<=12&&r>=1&&r<=$(e,t)}function Z(e){return K(e)===0}function $(e,t){return t<=6?31:t<=11||Z(e)?30:29}function K(e){var t=y.length,r=y[0],n,a,o,i,s;if(e<r||e>=y[t-1])throw new Error("Invalid Jalaali year "+e);for(s=1;s<t&&(n=y[s],a=n-r,!(e<n));s+=1)r=n;return i=e-r,a-i<6&&(i=i-a+u(a+4,33)*33),o=d(d(i+1,33)-1,4),o===-1&&(o=4),o}function E(e,t){var r=y.length,n=e+621,a=-14,o=y[0],i,s,m,D,h,c,g;if(e<o||e>=y[r-1])throw new Error("Invalid Jalaali year "+e);for(g=1;g<r&&(i=y[g],s=i-o,!(e<i));g+=1)a=a+u(s,33)*8+u(d(s,33),4),o=i;return c=e-o,a=a+u(c,33)*8+u(d(c,33)+3,4),d(s,33)===4&&s-c===4&&(a+=1),D=u(n,4)-u((u(n,100)+1)*3,4)-150,h=20+a-D,t?{gy:n,march:h}:(s-c<6&&(c=c-s+u(s+4,33)*33),m=d(d(c+1,33)-1,4),m===-1&&(m=4),{leap:m,gy:n,march:h})}function Y(e,t,r){var n=E(e,!0);return H(n.gy,3,n.march)+(t-1)*31-u(t,7)*(t-7)+r-1}function j(e){var t=A(e).gy,r=t-621,n=E(r,!1),a=H(t,3,n.march),o,i,s;if(s=e-a,s>=0){if(s<=185)return i=1+u(s,31),o=d(s,31)+1,{jy:r,jm:i,jd:o};s-=186}else r-=1,s+=179,n.leap===1&&(s+=1);return i=7+u(s,30),o=d(s,30)+1,{jy:r,jm:i,jd:o}}function H(e,t,r){var n=u((e+u(t-8,6)+100100)*1461,4)+u(153*d(t+9,12)+2,5)+r-34840408;return n=n-u(u(e+100100+u(t-8,6),100)*3,4)+752,n}function A(e){var t,r,n,a,o;return t=4*e+139361631,t=t+u(u(4*e+183187720,146097)*3,4)*4-3908,r=u(d(t,1461),4)*5+308,n=u(d(r,153),5)+1,a=d(u(r,153),12)+1,o=u(t,1461)-100100+u(8-a,6),{gy:o,gm:a,gd:n}}function Q(e,t,r){var n=P(e,t,r).getDay(),a=n==6?0:-(n+1),o=6+a;return{saturday:j(Y(e,t,r+a)),friday:j(Y(e,t,r+o))}}function P(e,t,r,n,a,o,i){var s=R(e,t,r);return new Date(s.gy,s.gm-1,s.gd,n||0,a||0,o||0,i||0)}function u(e,t){return~~(e/t)}function d(e,t){return e-~~(e/t)*t}const b=["فر","ارد","خر","تیر","مرد","شهر","مهر","آبا","آذر","دی","بهم","اسفن"],ee=["یک","دو","سه","چهار","پنج","جمعه","شنبه"],te=["یک","دو","سه","چه","پن","جم","شن"],re=/\[([^\]]+)]|YYYY|YY?|yyyy|yy?|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|m{1,2}|s{1,2}|Z{1,2}|S{1,3}|w{1,2}|x|X|a|A/g;function l(e,t=2){let r=`${Math.abs(e)}`;const n=e<0?"-":"";for(;r.length<t;)r=`0${r}`;return n+r}function N(e){return Math.round(e.timezoneOffset/15)*15}function F(e,t=""){const r=e>0?"-":"+",n=Math.abs(e),a=Math.floor(n/60),o=n%60;return r+l(a,2)+t+l(o,2)}const C=(e,t,r)=>{const n=e<12?"AM":"PM";return r?n.toLocaleLowerCase():n},O={Y(e){const t=e.year;return t<=9999?`${t}`:`+${t}`},yy(e){return l(e.year,4).substr(2)},yyyy(e){return l(e.year,4)},M(e){return e.month+1},MM(e){return l(e.month+1,2)},MMM(e){return b[e.month]},MMMM(e){return x.monthNames[e.month]},d(e){return e.day},dd(e){return l(e.day,2)},H(e){return e.hour},HH(e){return l(e.hour,2)},h(e){const t=e.hour;return t===0?12:t>12?t%12:t},hh(...e){const t=O.h(...e);return l(t,2)},m(e){return e.minute},mm(e){return l(e.minute,2)},s(e){return e.second},ss(e){return l(e.second,2)},S(e){return Math.floor(e.millisecond/100)},SS(e){return l(Math.floor(e.millisecond/10),2)},SSS(e){return l(e.millisecond,3)},D(e){return e.day},DD(e){return te[e.day]},ddd(e){return ee[e.day]},dddd(e){return x.dayNames[e.day]},A(e){return C(e.hour,e.minute,!1)},a(e){return C(e.hour,e.minute,!0)},Z(e){return F(N(e),":")},ZZ(e){return F(N(e))},X(e){return Math.floor(e.timestamp/1e3)},x(e){return e.timestamp},ww(e){return l(O.w(e),2)}};function ne(e,t){return(t?String(t):"YYYY-MM-DDTHH:mm:ss.SSSZ").replace(re,(n,a)=>a||(typeof O[n]=="function"?`${O[n](e)}`:n))}const L=["فروردین","اردیبهشت","خرداد","تیر","مرداد","شهریور","مهر","آبان","آذر","دی","بهمن","اسفند"],ae=/(\[[^[]*\])|(MM?M?M?|Do|DD?|ddd?d?|w[o|w]?|YYYY|YY|yyyy|yy|a|A|hh?|HH?|mm?|ss?|S{1,3}|x|X|ZZ?|.)/g,oe=/\d/,M=/\d\d/,se=/\d{3}/,ie=/\d{4}/,w=/\d\d?/,ue=/[+-]\d\d:?\d\d/,ce=/[+-]?\d+/,k="year",I="month",z="day",W="hour",G="minute",X="second",v="millisecond",_={},f=(e,t,r)=>{const n=Array.isArray(e)?e:[e];let a;typeof r=="string"?a=o=>{const i=parseInt(o,10);return{[r]:i}}:a=r,n.forEach(o=>{_[o]=[t,a]})},fe=e=>e.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&"),U=e=>()=>new RegExp(e.map(fe).join("|")),V=(e,t)=>r=>{const n=e.indexOf(r);if(n<0)throw new Error("Invalid Word");return{[t]:n}};f("Y",ce,k);f("YY",M,e=>{const t=new Date().getFullYear(),r=Math.floor(t/100);let n=parseInt(e,10);return n=(n>68?r-1:r)*100+n,{[k]:n}});f("yyyy",ie,k);f("M",w,e=>({[I]:parseInt(e,10)-1}));f("MM",M,e=>({[I]:parseInt(e,10)-1}));f("MMM",U(b),V(b,I));f("MMMM",U(L),V(L,I));f("d",w,z);f("dd",M,z);f(["H","h"],w,W);f(["HH","hh"],M,W);f("m",w,G);f("mm",M,G);f("s",w,X);f("ss",M,X);f("S",oe,e=>({[v]:parseInt(e,10)*100}));f("SS",M,e=>({[v]:parseInt(e,10)*10}));f("SSS",se,v);function le(){return/[ap]\.?m?\.?/i}function de(e){return`${e}`.toLowerCase().charAt(0)==="p"}f(["A","a"],le,e=>({isPM:de(e)}));function me(e){const[t,r,n]=e.match(/([+-]|\d\d)/g)||["-","0","0"],a=parseInt(r,10)*60+parseInt(n,10);return a===0?0:t==="+"?-a:+a}f(["Z","ZZ"],ue,e=>({offset:me(e)}));function he(e,t){if(e!==void 0&&t!==void 0){if(t){if(e<12)return e+12}else if(e===12)return 0}return e}function ye(e,t){const r=t.match(ae);if(!r)throw new Error;const{length:n}=r;let a={};for(let o=0;o<n;o+=1){const i=r[o],s=_[i];if(s){const m=typeof s[0]=="function"?s[0]():s[0],D=s[1],h=(m.exec(e)||[])[0],c=D(h||"");a={...a,...c},e=e.replace(h||"","")}else{const m=i.replace(/^\[|\]$/g,"");if(e.indexOf(m)===0)e=e.substr(m.length);else throw new Error("not match")}}return a}function ge(e,t,r=new Date){const n={year:r.getFullYear(),month:r.getMonth(),day:r.getDate(),hour:r.getHours(),minute:r.getMinutes(),second:r.getSeconds(),millisecond:r.getMilliseconds(),timezoneOffset:r.getTimezoneOffset()},a=ye(e,t);return["year","month","day","hour","minute","second","millisecond"].forEach(o=>{a[o]&&(n[o]=a[o]||0)}),n.hour=he(n.hour,a.isPM)||0,a.offset!==void 0&&(n.timezoneOffset=a.offset),n}const x={dayNames:["یکشنبه","دوشنبه","سهشنبه","چهارشنبه","پنجشنبه","جمعه","شنبه"],monthNames:["فروردین","اردیبهشت","خرداد","تیر","مرداد","شهریور","مهر","آبان","آذر","دی","بهمن","اسفند"],format:function(e,t){if(!e)return"";const r=p.toJalaali(e);return ne({year:r.jy,month:r.jm-1,day:r.jd,hour:e.getHours(),minute:e.getMinutes(),second:e.getSeconds(),millisecond:e.getMilliseconds(),timestamp:e.getTime(),timezoneOffset:e.getTimezoneOffset()},t)},parse:function(e,t,r){const n=ge(e,t,r||new Date),a=p.jalaaliToDateObject(n.year,n.month+1,n.day);return a.setHours(n.hour,n.minute,n.second,n.millisecond),a},toDateParts:function(e){return new Intl.DateTimeFormat("fa-IR-u-nu-latn",{year:"numeric",month:"numeric",day:"numeric"}).formatToParts(e).reduce((t,r)=>(r.type!=="literal"&&(t[r.type]=+r.value),t),{})},years:function({type:e,year:t}){const r=p.toJalaali(new Date).jy;return[...Array(200).keys()].map(n=>({type:e,key:e+n,isToday:r===n+1300,isSelected:t===n+1300,isHeader:!1,isDisabled:!1,value:n+1300,text:n+1300+""}))},months:function({type:e,month:t}){const r=p.toJalaali(new Date).jm;return[...this.monthNames.keys()].map(n=>({type:e,key:e+n,isToday:r===n+1,isSelected:t===n+1,isHeader:!1,isDisabled:!1,value:n+1,text:this.monthNames[n]}))},days:function({type:e,month:t,startOfWeek:r,year:n,value:a}){const o=p.jalaaliToDateObject(n,t,1),i=p.jalaaliToDateObject(n,t+1,1);i.setDate(i.getDate()-1);const s=T(r-1,7),m=new Intl.DateTimeFormat("fa-IR-u-nu-latn",{day:"numeric"}),D=S.startOfToday().getTime(),h=a?S.startOfDay(a).getTime():0;return this.dayNames.map((c,g)=>{const J=T(r+g,7);return{type:e,key:"weekday"+J,isToday:!1,isSelected:!1,isHeader:!0,isDisabled:!1,value:g,text:this.dayNames[J]}}).concat(S.eachDayOfInterval({start:S.addDays(o,-T(o.getDay()-r,7)),end:S.addDays(i,T(s-i.getDay(),7))}).map(c=>({type:e,key:c.toString(),isToday:D===c.getTime(),isSelected:h===c.getTime(),isHeader:!1,isDisabled:c<o||c>i,isInCurrentMonth:c>=o&&c<=i,value:c,text:m.format(c)})))},hours:function({type:e,hour:t}){return[...Array(24).keys()].map(r=>({type:e,key:r,value:r,text:r+"",isToday:!1,isSelected:t===r,isHeader:!1,isDisabled:!1}))},minutes:function({type:e,minute:t}){return[...Array(60).keys()].map(r=>({type:e,key:r,value:r,text:r+"",isToday:!1,isSelected:t===r,isHeader:!1,isDisabled:!1}))}};function T(e,t){return(e%t+t)%t}exports.config=x;