UNPKG

@acrool/js-utils

Version:

Common javascript utils methods for project development

2 lines (1 loc) 9.25 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const ut=require("../common-50b3f529.cjs");var ct=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},P={},ft={get exports(){return P},set exports(o){P=o}};(function(o,d){(function($,M){o.exports=M()})(ct,function(){var $=1e3,M=6e4,w=36e5,v="millisecond",D="second",b="minute",T="hour",p="day",N="week",S="month",R="quarter",O="year",H="date",X="Invalid Date",st=/^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/,it=/\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g,at={name:"en",weekdays:"Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),months:"January_February_March_April_May_June_July_August_September_October_November_December".split("_"),ordinal:function(s){var n=["th","st","nd","rd"],t=s%100;return"["+s+(n[(t-20)%10]||n[t]||n[0])+"]"}},z=function(s,n,t){var r=String(s);return!r||r.length>=n?s:""+Array(n+1-r.length).join(t)+s},ot={s:z,z:function(s){var n=-s.utcOffset(),t=Math.abs(n),r=Math.floor(t/60),e=t%60;return(n<=0?"+":"-")+z(r,2,"0")+":"+z(e,2,"0")},m:function s(n,t){if(n.date()<t.date())return-s(t,n);var r=12*(t.year()-n.year())+(t.month()-n.month()),e=n.clone().add(r,S),i=t-e<0,a=n.clone().add(r+(i?-1:1),S);return+(-(r+(t-e)/(i?e-a:a-e))||0)},a:function(s){return s<0?Math.ceil(s)||0:Math.floor(s)},p:function(s){return{M:S,y:O,w:N,d:p,D:H,h:T,m:b,s:D,ms:v,Q:R}[s]||String(s||"").toLowerCase().replace(/s$/,"")},u:function(s){return s===void 0}},L="en",_={};_[L]=at;var E="$isDayjsObject",q=function(s){return s instanceof F||!(!s||!s[E])},V=function s(n,t,r){var e;if(!n)return L;if(typeof n=="string"){var i=n.toLowerCase();_[i]&&(e=i),t&&(_[i]=t,e=i);var a=n.split("-");if(!e&&a.length>1)return s(a[0])}else{var c=n.name;_[c]=n,e=c}return!r&&e&&(L=e),e||!r&&L},h=function(s,n){if(q(s))return s.clone();var t=typeof n=="object"?n:{};return t.date=s,t.args=arguments,new F(t)},u=ot;u.l=V,u.i=q,u.w=function(s,n){return h(s,{locale:n.$L,utc:n.$u,x:n.$x,$offset:n.$offset})};var F=function(){function s(t){this.$L=V(t.locale,null,!0),this.parse(t),this.$x=this.$x||t.x||{},this[E]=!0}var n=s.prototype;return n.parse=function(t){this.$d=function(r){var e=r.date,i=r.utc;if(e===null)return new Date(NaN);if(u.u(e))return new Date;if(e instanceof Date)return new Date(e);if(typeof e=="string"&&!/Z$/i.test(e)){var a=e.match(st);if(a){var c=a[2]-1||0,f=(a[7]||"0").substring(0,3);return i?new Date(Date.UTC(a[1],c,a[3]||1,a[4]||0,a[5]||0,a[6]||0,f)):new Date(a[1],c,a[3]||1,a[4]||0,a[5]||0,a[6]||0,f)}}return new Date(e)}(t),this.init()},n.init=function(){var t=this.$d;this.$y=t.getFullYear(),this.$M=t.getMonth(),this.$D=t.getDate(),this.$W=t.getDay(),this.$H=t.getHours(),this.$m=t.getMinutes(),this.$s=t.getSeconds(),this.$ms=t.getMilliseconds()},n.$utils=function(){return u},n.isValid=function(){return this.$d.toString()!==X},n.isSame=function(t,r){var e=h(t);return this.startOf(r)<=e&&e<=this.endOf(r)},n.isAfter=function(t,r){return h(t)<this.startOf(r)},n.isBefore=function(t,r){return this.endOf(r)<h(t)},n.$g=function(t,r,e){return u.u(t)?this[r]:this.set(e,t)},n.unix=function(){return Math.floor(this.valueOf()/1e3)},n.valueOf=function(){return this.$d.getTime()},n.startOf=function(t,r){var e=this,i=!!u.u(r)||r,a=u.p(t),c=function(j,y){var k=u.w(e.$u?Date.UTC(e.$y,y,j):new Date(e.$y,y,j),e);return i?k:k.endOf(p)},f=function(j,y){return u.w(e.toDate()[j].apply(e.toDate("s"),(i?[0,0,0,0]:[23,59,59,999]).slice(y)),e)},l=this.$W,m=this.$M,g=this.$D,x="set"+(this.$u?"UTC":"");switch(a){case O:return i?c(1,0):c(31,11);case S:return i?c(1,m):c(0,m+1);case N:var W=this.$locale().weekStart||0,C=(l<W?l+7:l)-W;return c(i?g-C:g+(6-C),m);case p:case H:return f(x+"Hours",0);case T:return f(x+"Minutes",1);case b:return f(x+"Seconds",2);case D:return f(x+"Milliseconds",3);default:return this.clone()}},n.endOf=function(t){return this.startOf(t,!1)},n.$set=function(t,r){var e,i=u.p(t),a="set"+(this.$u?"UTC":""),c=(e={},e[p]=a+"Date",e[H]=a+"Date",e[S]=a+"Month",e[O]=a+"FullYear",e[T]=a+"Hours",e[b]=a+"Minutes",e[D]=a+"Seconds",e[v]=a+"Milliseconds",e)[i],f=i===p?this.$D+(r-this.$W):r;if(i===S||i===O){var l=this.clone().set(H,1);l.$d[c](f),l.init(),this.$d=l.set(H,Math.min(this.$D,l.daysInMonth())).$d}else c&&this.$d[c](f);return this.init(),this},n.set=function(t,r){return this.clone().$set(t,r)},n.get=function(t){return this[u.p(t)]()},n.add=function(t,r){var e,i=this;t=Number(t);var a=u.p(r),c=function(m){var g=h(i);return u.w(g.date(g.date()+Math.round(m*t)),i)};if(a===S)return this.set(S,this.$M+t);if(a===O)return this.set(O,this.$y+t);if(a===p)return c(1);if(a===N)return c(7);var f=(e={},e[b]=M,e[T]=w,e[D]=$,e)[a]||1,l=this.$d.getTime()+t*f;return u.w(l,this)},n.subtract=function(t,r){return this.add(-1*t,r)},n.format=function(t){var r=this,e=this.$locale();if(!this.isValid())return e.invalidDate||X;var i=t||"YYYY-MM-DDTHH:mm:ssZ",a=u.z(this),c=this.$H,f=this.$m,l=this.$M,m=e.weekdays,g=e.months,x=e.meridiem,W=function(y,k,I,J){return y&&(y[k]||y(r,i))||I[k].slice(0,J)},C=function(y){return u.s(c%12||12,y,"0")},j=x||function(y,k,I){var J=y<12?"AM":"PM";return I?J.toLowerCase():J};return i.replace(it,function(y,k){return k||function(I){switch(I){case"YY":return String(r.$y).slice(-2);case"YYYY":return u.s(r.$y,4,"0");case"M":return l+1;case"MM":return u.s(l+1,2,"0");case"MMM":return W(e.monthsShort,l,g,3);case"MMMM":return W(g,l);case"D":return r.$D;case"DD":return u.s(r.$D,2,"0");case"d":return String(r.$W);case"dd":return W(e.weekdaysMin,r.$W,m,2);case"ddd":return W(e.weekdaysShort,r.$W,m,3);case"dddd":return m[r.$W];case"H":return String(c);case"HH":return u.s(c,2,"0");case"h":return C(1);case"hh":return C(2);case"a":return j(c,f,!0);case"A":return j(c,f,!1);case"m":return String(f);case"mm":return u.s(f,2,"0");case"s":return String(r.$s);case"ss":return u.s(r.$s,2,"0");case"SSS":return u.s(r.$ms,3,"0");case"Z":return a}return null}(y)||a.replace(":","")})},n.utcOffset=function(){return 15*-Math.round(this.$d.getTimezoneOffset()/15)},n.diff=function(t,r,e){var i,a=this,c=u.p(r),f=h(t),l=(f.utcOffset()-this.utcOffset())*M,m=this-f,g=function(){return u.m(a,f)};switch(c){case O:i=g()/12;break;case S:i=g();break;case R:i=g()/3;break;case N:i=(m-l)/6048e5;break;case p:i=(m-l)/864e5;break;case T:i=m/w;break;case b:i=m/M;break;case D:i=m/$;break;default:i=m}return e?i:u.a(i)},n.daysInMonth=function(){return this.endOf(S).$D},n.$locale=function(){return _[this.$L]},n.locale=function(t,r){if(!t)return this.$L;var e=this.clone(),i=V(t,r,!0);return i&&(e.$L=i),e},n.clone=function(){return u.w(this.$d,this)},n.toDate=function(){return new Date(this.valueOf())},n.toJSON=function(){return this.isValid()?this.toISOString():null},n.toISOString=function(){return this.$d.toISOString()},n.toString=function(){return this.$d.toUTCString()},s}(),K=F.prototype;return h.prototype=K,[["$ms",v],["$s",D],["$m",b],["$H",T],["$W",p],["$M",S],["$y",O],["$D",H]].forEach(function(s){K[s[1]]=function(n){return this.$g(n,s[0],s[1])}}),h.extend=function(s,n){return s.$i||(s(n,F,h),s.$i=!0),h},h.locale=V,h.isDayjs=q,h.unix=function(s){return h(1e3*s)},h.en=_[L],h.Ls=_,h.p={},h})})(ft);const A=P;function dt(o){if(!o||!o.trim())return"";const d=A(o);if(!d.isValid())return"";const $=Z(o),M=d.format("HH:mm");return`${$} ${M}`}function Z(o){if(!o||!o.trim())return"";const d=A(o);return d.isValid()?A().isSame(d,"year")?d.format("MM/DD"):d.format("YYYY/MM/DD"):""}function ht(o,d){const $=[o,d].filter(D=>D!==""&&D&&D!=="0000-00-00");if($.length===1||$[0]===$[1])return Z($[0]);const M=A(o),w=A(d),v={shortDate:"MM/DD",fullDate:"YYYY/MM/DD"};return M.isSame(d,"year")?[Z(o),w.format(v.shortDate)].join(" - "):[M.format(v.fullDate),w.format(v.fullDate)].join(" - ")}const tt=(o,d=!1)=>{const $=d?Math.floor(o/60/60/24):0,M=Math.floor(d?o/60/60%24:o/3600),w=Math.floor(o%3600/60),v=o%60;return{days:$,hours:M,minutes:w,seconds:v}},lt=(o,d=!1)=>{const{hours:$,minutes:M,seconds:w}=tt(o,!1),v=[$,M,w];let D=0;return v.reduce((b,T,p)=>D>=p&&d&&T===0?(D=p,b):[...b,ut.paddingLeft(T,2)],[]).join(":")},$t=(o,d=!1)=>{const $=o/1e3;return d?Math.floor($):$},et=o=>o*60*1e3,nt=o=>et(o*60),mt=o=>nt(o*24),Y=A(),U=Y.day(0),B=U.subtract(7,"day"),G=U.subtract(14,"day"),Q=Y.set("date",1),Mt=Y.subtract(6,"month"),yt={today:Y,thisWeek:U,lastWeek:B,twoWeekAgo:G,thisMonth:Q,halfYearAgo:Mt};var rt=(o=>(o.today="today",o.yesterday="yesterday",o.thisWeek="thisWeek",o.lastWeek="lastWeek",o.twoWeekAgo="twoWeekAgo",o.thisMonth="thisMonth",o))(rt||{});const Dt={today:{startDate:Y,endDate:Y},yesterday:{startDate:Y.subtract(1,"day"),endDate:Y.subtract(1,"day")},thisWeek:{startDate:U,endDate:U.add(6,"day")},lastWeek:{startDate:B,endDate:B.add(6,"day")},twoWeekAgo:{startDate:G,endDate:G.add(6,"day")},thisMonth:{startDate:Q,endDate:Q.endOf("month")}};exports.EDayKey=rt;exports.dateUtils=yt;exports.dayList=Dt;exports.dayToMillisecond=mt;exports.formatSecondToString=lt;exports.formatTotalSeconds=tt;exports.hourToMillisecond=nt;exports.millisecondToSeconds=$t;exports.minuteToMillisecond=et;exports.rangeSimpleDate=ht;exports.simpleDate=Z;exports.simpleDateTime=dt;