UNPKG

pomeranian-durations

Version:

An immutable duration library based on the ISO-8601 format for durations.

2 lines (1 loc) 10.7 kB
var Go=Object.defineProperty;var Mo=(o,t)=>{for(var s in t)Go(o,s,{get:t[s],enumerable:!0})};var xo={};Mo(xo,{add:()=>u,addDays:()=>io,addHours:()=>ao,addMicroseconds:()=>eo,addMilliseconds:()=>ro,addMinutes:()=>co,addMonths:()=>po,addSeconds:()=>no,addToDate:()=>uo,addWeeks:()=>mo,addYears:()=>fo,sum:()=>nt});var n=o=>{let t=o.length;return function s(){let e=Array.prototype.slice.call(arguments);return function(){let c=e.slice();return Array.prototype.push.apply(c,arguments),(c.length>=t?o:s).apply(null,c)}}()},N=(o,...t)=>t.reduce((s,e)=>e(s),o),U=(o="")=>{let t=I=>I.isBuilder?I.toValue():I,s=I=>Bo=>U(I(t(Bo))),e=s(I=>`(${I})?`),c=s(I=>`${o}${I}`),a=(...I)=>U([o,...I].join("")),d=()=>c("^"),w=()=>c("$"),V=()=>o;return{join:a,startOfLine:d,endOfLine:w,maybe:e,and:c,toValue:V,test:I=>new RegExp(V()).test(I),isBuilder:!0}},Oo=n((o,t)=>o.reduce((s,e)=>(e in t&&(s[e]=t[e]),s),{})),K=n((o,t)=>Object.keys(t).reduce((s,e)=>(s[e]=o(t[e]),s),{})),Do=n((o,t)=>Object.keys(t).reduce((s,e)=>(o.includes(e)||(s[e]=t[e]),s),{})),Z=(o,t,...s)=>s.filter(o).join(t),B=n((o,t,s)=>{let e=`${s||t}`,[c,a]=e.split(".");if(c.length>=o)return e;let w=`${Array.from({length:o}).reduce(V=>`${V}${t}`)}${c}`.substr(-o);return a?`${w}.${a}`:w}),_o=o=>Object.keys(o).map(t=>o[t]),h=o=>o*-1;var qo=1,E=qo*1e3,y=E*1e3,O=y*60,T=O*60,Y=T*24,J="P",A="T",v="P",f="Invalid Duration",Q=["microseconds","milliseconds","seconds","minutes","hours","days","weeks","months","years"],r=Q.reduce((o,t)=>(o[t]=t,o),{}),X=[r.microseconds,r.milliseconds,r.seconds,r.minutes,r.hours,r.days,r.weeks,r.months,r.years],oo={[r.seconds]:"S",[r.minutes]:"M",[r.hours]:"H"},to={[r.days]:"D",[r.weeks]:"W",[r.months]:"M",[r.years]:"Y"},M={...oo,...to};var zo=(o,t,s)=>o.substring(o.lastIndexOf(t)+1,o.lastIndexOf(s)),Ko=o=>o.lastIndexOf(A)===-1,Zo=o=>o.split(A)[1]||"",Jo=o=>Ko(o)?o.replace(v,""):zo(o,v,A),Eo=(o,t)=>{let s=o.toUpperCase().match(new RegExp(`[+,-]?[0-9]+(\\.[0-9]+)?${t}`));if(!!s)return parseFloat(s[0].slice(0,-1))},so=n((o,t)=>{if(!t)return;let s=Zo(t);return Eo(s,o)}),G=n((o,t)=>{if(!t)return;let s=Jo(t);return Eo(s,o)}),$=so(M.seconds),R=so(M.minutes),b=so(M.hours),F=G(M.days),j=G(M.weeks),S=G(M.months),C=G(M.years);var Qo=(o,t)=>o.indexOf(t)!==-1,Xo=(o,t)=>Qo(Object.keys(o),t),Ao=(o,t,s)=>Object.keys(o).filter(e=>Xo(t,e)).sort((e,c)=>{let a=Object.keys(M);return a.indexOf(c)-a.indexOf(e)}).reduce((e,c)=>{if(!s&&o[c]===0)return e;let a=t[c],d=parseFloat(o[c]);return`${e}${d}${a}`},""),ot=(o,t)=>{let s=Ao(o,to,t);return`${v}${s}`},tt=(o,t)=>{let s=Ao(o,oo,t);return s?`${A}${s}`:""},i=(o,{includeZeroValues:t=!1}={})=>{let s=ot(o,t),e=tt(o,t);return`${s}${e}`},m=(o,{defaultValue:t=0}={})=>({seconds:$(o)||t,minutes:R(o)||t,hours:b(o)||t,days:F(o)||t,weeks:j(o)||t,months:S(o)||t,years:C(o)||t}),st=o=>{let t=Object.entries(m(o)).filter(s=>s[1]!==0).map(([s])=>s);return Q.filter(s=>t.includes(s))},St=o=>st(o).reverse();var H=/[+-]?\d+(\.\d+)?/.source,et=U().and(`(${H}Y)?`).and(`(${H}M)?`).and(`(${H}W)?`).and(`(${H}D)?`),Uo=U().maybe(U().and(A).and(`(${H}H)?`).and(`(${H}M)?`).and(`(${H}S)?`)),rt=U().startOfLine().and(v).and(Uo).and(et).and(Uo).endOfLine(),vo=o=>typeof o=="string"&&rt.test(o.toUpperCase()),p=o=>!vo(o),kt=n((o,t)=>vo(t)?t:typeof o=="function"?o(t):o),wt=n((o,t)=>t!==f?t:typeof o=="function"?o(t):o);var u=n((o,t)=>{if(p(o)||p(t))return f;let s=m(o),e=m(t),c=Object.keys(s).reduce((a,d)=>(a[d]=s[d]+e[d],a),{});return i(c)}),nt=o=>o.reduce((t,s)=>u(t,s)),eo=n((o,t)=>u(t,`PT${o/10**6}S`)),ro=n((o,t)=>u(t,`PT${o/10**3}S`)),no=n((o,t)=>u(t,`PT${o}S`)),co=n((o,t)=>u(t,`PT${o}M`)),ao=n((o,t)=>u(t,`PT${o}H`)),io=n((o,t)=>u(t,`P${o}D`)),mo=n((o,t)=>u(t,`P${o}W`)),po=n((o,t)=>u(t,`P${o}M`)),fo=n((o,t)=>u(t,`P${o}Y`)),uo=n((o,t)=>{if(p(o))return f;let s=new Date(t),e=m(o);return s.setSeconds(s.getSeconds()+e.seconds),s.setMinutes(s.getMinutes()+e.minutes),s.setHours(s.getHours()+e.hours),s.setDate(s.getDate()+e.days),s.setDate(s.getDate()+e.weeks*7),s.setMonth(s.getMonth()+e.months),s.setFullYear(s.getFullYear()+e.years),s});var Io={};Mo(Io,{floor:()=>$o,floorDays:()=>mt,floorHours:()=>it,floorMinutes:()=>at,floorMonths:()=>pt,floorSeconds:()=>ct,floorWeeks:()=>dt,floorYears:()=>ft});var lo=[r.seconds,r.minutes,r.hours,r.days,r.weeks,r.months,r.years],P=o=>t=>{if(p(t))return f;let s=m(t),c=lo.slice(0,lo.indexOf(o)).reduce((a,d)=>(a[d]=0,a),s);return c[o]=Math.floor(s[o]),i(c)},$o=(o,t)=>{let s=m(t),e=m(o),c=lo.reduce((a,d)=>(!s[d]||!e[d]||(a[d]=s[d]-s[d]%e[d]),a),{});return i(c)},ct=P(r.seconds),at=P(r.minutes),it=P(r.hours),mt=P(r.days),dt=P(r.weeks),pt=P(r.months),ft=P(r.years);var k=o=>t=>{let s=Io[`floor${o}`],e=xo[`add${o}`];return s(e(1,t))},Kt=n((o,t)=>$o(o,u(o,t))),Zt=k("Seconds"),Jt=k("Minutes"),Qt=k("Hours"),Xt=k("Days"),os=k("Weeks"),ts=k("Months"),ss=k("Years");var q=(o,t)=>L(o)/t,ut=o=>[F(o),j(o),S(o),C(o)].some(t=>t),L=o=>{if(ut(o))throw new Error("Can't convert from date units.");return[($(o)||0)*y,(R(o)||0)*O,(b(o)||0)*T].reduce((t,s)=>t+s)},cs=o=>q(o,E),W=o=>q(o,y),Ro=o=>q(o,O),bo=o=>q(o,T),as=o=>W(o)/E/E,is=o=>W(o)/E,xt=o=>W(o),lt=o=>xt(o)/60,ms=o=>lt(o)/60;var g=o=>function t(...s){let e=s[0],c=s[1];if(s.length!==2)return w=>t(w,e);if(p(e)||p(c))return!1;let a=L(e),d=L(c);return o(a,d)},us=g((o,t)=>o>=t),xs=g((o,t)=>o>t),Fo=g((o,t)=>o<t),ls=g((o,t)=>o<=t),jo=g((o,t)=>o===t);var x=(...o)=>t=>o.reduce((s,e)=>e(s),t),l=n((o,t)=>`${B(o,"0",t)}`),So={"%Y":x(C,l(1)),"%YY":x(C,l(2)),"%M":x(S,l(1)),"%MM":x(S,l(2)),"%W":x(j,l(1)),"%WW":x(j,l(2)),"%D":x(F,l(1)),"%DD":x(F,l(2)),"%h":x(b,l(1)),"%hh":x(b,l(2)),"%m":x(R,l(1)),"%mm":x(R,l(2)),"%s":x($,l(1)),"%ss":x($,l(2))},It=Object.keys(So).sort((o,t)=>t.length-o.length),ys=n((o,t)=>It.reduce((s,e)=>s.replace(new RegExp(e,"g"),So[e](t)),o));var D=(o,t)=>{let s=new RegExp(t.map(a=>`[+,-]?[0-9] ?${a} `).join("|")),e=new RegExp(t.join("|").toLowerCase()),c=`${o} `.toLowerCase().match(s);return c?parseFloat(c[0].replace(e,"")):0},Co=n((o,t)=>parseFloat(`${o}${t.replace(o,"")}`)),Nt=Co("+"),ht=Co("-"),yt=o=>{let t=new RegExp("[+-]?[0-9]+:[0-9]+:[0-9]+"),s=o.toLowerCase().match(t);if(!s)return 0;let e=s[0][0]==="-"?ht:Nt,[c,a,d]=s[0].split(":").map(e);return{hours:c,minutes:a,seconds:d}},Ds=o=>i({years:D(o,"years year y".split(" ")),months:D(o,"months month mons mon".split(" ")),days:D(o,"days day d".split(" ")),...yt(o)}),_s=o=>i({years:D(o,"years year y".split(" ")),months:D(o,"months month mons mon".split(" ")),days:D(o,"days day d".split(" ")),hours:D(o,"hours hour h".split(" ")),minutes:D(o,"minutes minute mins min m".split(" ")),seconds:D(o,"seconds second secs sec s".split(" "))});var vs=o=>z(o/y),$s=o=>z(o/E),z=o=>i({seconds:o}),Ho=o=>i({minutes:o}),Po=o=>i({hours:o}),Rs=o=>i({days:o}),bs=o=>i({weeks:o}),Fs=o=>i({months:o}),js=o=>i({years:o});var No=(o,t)=>s=>p(s)?f:o(t(s)),ws=No(z,W),Ls=No(Ho,Ro),Ys=No(Po,bo);var ko=o=>N(o,m,K(Math.abs),i),wo=o=>N(o,m,K(h),i);var Tt=[r.hours,r.minutes,r.seconds],Lo=o=>{if(p(o))return f;let t=m(o),s=N(t,Oo(Tt),i,L),e=Math.floor(s/T),c=Math.floor((s-e*T)/O),a=(s-e*T-c*O)/y;return i({...t,hours:e,minutes:c,seconds:a})};var _=n((o,t)=>p(t)?f:N(t,m,Do(o),i)),se=_([r.seconds]),ee=_([r.minutes]),re=_([r.hours]),ne=_([r.days]),ce=_([r.weeks]),ae=_([r.months]),ie=_([r.years]),ho=_([r.seconds,r.minutes,r.hours]),yo=_([r.days,r.weeks,r.months,r.years]);var Yo=o=>{let t=m(o);return[t.seconds*y,t.minutes*O,t.hours*T,t.days*Y,t.weeks*Y*7,t.months*Y*30,t.years*Y*364.25].reduce((s,e)=>s+e,0)},Mt=o=>(t,s)=>o(s,t),Wo=(o,t)=>Yo(o)-Yo(t),ue=n((o,t,s)=>Wo(t[o],s[o])),Ot=Mt(Wo),xe=n((o,t,s)=>Ot(t[o],s[o]));var Oe=n((o,t)=>{let s=m(t),e=Object.keys(s).reduce((c,a)=>(c[a]=h(s[a]),c),{});return u(o,i(e))}),De=n((o,t)=>ro(h(o),t)),_e=n((o,t)=>eo(h(o),t)),Ee=n((o,t)=>no(h(o),t)),Ae=n((o,t)=>co(h(o),t)),Ue=n((o,t)=>ao(h(o),t)),ve=n((o,t)=>io(h(o),t)),$e=n((o,t)=>mo(h(o),t)),Re=n((o,t)=>po(h(o),t)),be=n((o,t)=>fo(h(o),t)),Fe=n((o,t)=>p(o)?f:uo(wo(o),t));var Dt={years:"years",months:"mons",days:"days",hours:"hours",minutes:"mins",seconds:"seconds"},To=o=>B(2,"0",o),go=o=>{let t=m(o);return Object.keys(t).sort((s,e)=>X.indexOf(e)-X.indexOf(s)).filter(s=>t[s]!==0).map(s=>`${t[s]} ${Dt[s]}`).join(" ")},Vo=n((o,t)=>{if(jo(t,J))return"";let s=N(t,ko,Lo,m);return(Fo(t,J)?"-":"")+[o?To(s.hours):s.hours,To(s.minutes),To(s.seconds)].join(":")}),_t=o=>{let t=m(o);return _o(t).every(s=>s===0)?"":`${t.years}-${t.months} ${t.days}`},We=o=>p(o)?f:`@ ${go(o)}`.trim(),ge=o=>{if(p(o))return f;let t=N(o,yo,Vo(!0)),s=N(o,ho,go);return Z(e=>e!==""," ",s,t)},Ve=o=>{if(p(o))return f;let t=N(o,yo,Vo(!1)),s=N(o,ho,_t);return Z(e=>e!==""," ",s,t)};export{to as DATE_UNITS,v as DURATION_DESIGNATOR,f as INVALID_DURATION,Y as ONE_DAY,T as ONE_HOUR,qo as ONE_MICROSECOND,E as ONE_MILLISECOND,O as ONE_MINUTE,y as ONE_SECOND,A as TIME_DESIGNATOR,oo as TIME_UNITS,M as UNITS,r as UNIT_NAMES,Q as UNIT_NAMES_LIST,X as UNIT_ORDER,J as ZERO,ko as absolute,u as add,io as addDays,ao as addHours,eo as addMicroseconds,ro as addMilliseconds,co as addMinutes,po as addMonths,no as addSeconds,uo as addToDate,mo as addWeeks,fo as addYears,ms as asDecimalHours,as as asDecimalMicroseconds,is as asDecimalMilliseconds,lt as asDecimalMinutes,xt as asDecimalSeconds,bo as asHours,L as asMicroseconds,cs as asMilliseconds,Ro as asMinutes,W as asSeconds,Kt as ceil,Xt as ceilDays,Qt as ceilHours,Jt as ceilMinutes,ts as ceilMonths,Zt as ceilSeconds,os as ceilWeeks,ss as ceilYears,jo as eq,F as findDays,b as findHours,R as findMinutes,S as findMonths,$ as findSeconds,j as findWeeks,C as findYears,$o as floor,mt as floorDays,it as floorHours,at as floorMinutes,pt as floorMonths,ct as floorSeconds,dt as floorWeeks,ft as floorYears,ys as format,Rs as fromDays,Po as fromHours,vs as fromMicroseconds,$s as fromMilliseconds,Ho as fromMinutes,Fs as fromMonths,Ds as fromPostgres,_s as fromPostgresVerbose,z as fromSeconds,bs as fromWeeks,js as fromYears,xs as gt,us as gte,Ys as inHours,Ls as inMinutes,ws as inSeconds,wo as invert,p as isInvalid,vo as isValid,Fo as lt,ls as lte,Lo as normalizeTime,yo as removeDateUnits,ne as removeDays,re as removeHours,ee as removeMinutes,ae as removeMonths,se as removeSeconds,ho as removeTimeUnits,ce as removeWeeks,ie as removeYears,Wo as sortAsc,ue as sortAscBy,Ot as sortDesc,xe as sortDescBy,Oe as subtract,ve as subtractDays,Fe as subtractFromDate,Ue as subtractHours,_e as subtractMicroseconds,De as subtractMilliseconds,Ae as subtractMinutes,Re as subtractMonths,Ee as subtractSeconds,$e as subtractWeeks,be as subtractYears,nt as sum,m as toFragments,i as toIso,ge as toPostgres,We as toPostgresVerbose,Ve as toSql,st as unitNamesAsc,St as unitNamesDesc,kt as whenInvalid,wt as whenInvalidDuration};