js-duration-parser
Version:
Converter between human readable duration and time units
3 lines (2 loc) • 3.49 kB
JavaScript
"use strict";var D=(n,t,e)=>{if(!t.has(n))throw TypeError("Cannot "+e)};var s=(n,t,e)=>(D(n,t,"read from private field"),e?e.call(n):t.get(n)),u=(n,t,e)=>{if(t.has(n))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(n):t.set(n,e)},l=(n,t,e,r)=>(D(n,t,"write to private field"),r?r.call(n,e):t.set(n,e),e);var P=(n,t,e)=>(D(n,t,"access private method"),e);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});var T;class y{constructor(){u(this,T,{"s=>s":1,"m=>s":60,"h=>s":60*60,"d=>s":24*60*60,"w=>s":7*24*60*60,"s=>m":1/60,"m=>m":1,"h=>m":60,"d=>m":24*60,"w=>m":7*24*60,"s=>h":1/60/60,"m=>h":1/60,"h=>h":1,"d=>h":24,"w=>h":7*24,"s=>d":1/24/60/60,"m=>d":1/24/60,"h=>d":1/24,"d=>d":1,"w=>d":7,"s=>w":1/7/24/60/60,"m=>w":1/7/24/60,"h=>w":1/7/24,"d=>w":1/7,"w=>w":1})}convert(t,e,r){const i=`${e}=>${r}`,o=s(this,T)[i]??null;if(o===null)throw new Error(`Conversion ${i} is not supported!`);return t*o}}T=new WeakMap;var f;class I{constructor(){u(this,f,{en:{s:"s",m:"m",h:"h",d:"d",w:"w"}})}add(t,e){return s(this,f)[t]=e,this}get(t){return s(this,f)[t]??s(this,f).en}getInternalUnit(t,e){const r=this.get(e);for(const i in r)if(r[i]===t)return i;return null}}f=new WeakMap;var w;class E{constructor(t=null){u(this,w,void 0);l(this,w,t??new I)}extractTimeGroups(t,e){const r=s(this,w).get(e),i=`^(\\d+${r.w})?\\s*(\\d+${r.d})?\\s*(\\d+${r.h})?\\s*(\\d+${r.m})?\\s*(\\d+${r.s})?$`,o=new RegExp(i),c=t.trim().toLowerCase().match(o);return c===null?[]:[...c].slice(1).filter(Boolean)}parseTimeGroup(t,e){const r=s(this,w).get(e),i=`^(\\d+)\\s*([${r.w}${r.d}${r.h}${r.m}${r.s}])$`,o=new RegExp(i),c=t.trim().toLowerCase().match(o);if(c===null)return null;const g=[...c].slice(1);return{time:parseInt(g[0]),unit:g[1]}}buildTimeGroup(t){if(typeof t.time>"u"||typeof t.unit>"u")throw new Error("Wrong input object is passed");return`${t.time}${t.unit.trim()}`}composeDuration(t,e=" "){return t.join(e)}}w=new WeakMap;var a,h,d,m,v,R;class b{constructor(t="en",e=null){u(this,v);u(this,a,void 0);u(this,h,void 0);u(this,d,void 0);u(this,m,void 0);l(this,a,t),l(this,h,new y),l(this,d,e??new I),l(this,m,new E(s(this,d)))}parse(t,e="m"){if(!t)return 0;if(t.trim().match(/^\d+$/)!==null)return parseInt(t.trim());const r=s(this,m).extractTimeGroups(t,s(this,a));return r.length===0?null:r.reduce((i,o)=>i+P(this,v,R).call(this,o,e),0)}compose(t,e="m",r=" "){if(!t)return`0${e}`;const i=s(this,d).get(s(this,a)),o=["w","d","h","m","s"],c=[];let g=t;for(const G of o){const x=parseInt(s(this,h).convert(g,e,G)),C=i[G]??null;if(x>=1&&C!==null){c.push(s(this,m).buildTimeGroup({time:x,unit:C}));const j=s(this,h).convert(x,G,e);g-=j}}return s(this,m).composeDuration(c,r)}}a=new WeakMap,h=new WeakMap,d=new WeakMap,m=new WeakMap,v=new WeakSet,R=function(t,e){const r=s(this,m).parseTimeGroup(t,s(this,a));return s(this,h).convert(r.time,s(this,d).getInternalUnit(r.unit,s(this,a)),e)};var p;class M{constructor(t=new I){u(this,p,void 0);l(this,p,t)}translate(t,e,r){const i=new b(e,s(this,p)),o=new b(r,s(this,p)),c=i.parse(t,"s");return o.compose(c,"s")}}p=new WeakMap;var $;class S{constructor(t=null){u(this,$,void 0);l(this,$,new E(t??new I))}validate(t,e="en"){return!!(t.length>0&&(t.trim().match(/^\d+$/)!==null||s(this,$).extractTimeGroups(t,e).length))}}$=new WeakMap;exports.DurationTranslator=M;exports.DurationValidator=S;exports.TimeIdentifiers=I;exports.default=b;
//# sourceMappingURL=index.cjs.js.map