UNPKG

@standards/duration

Version:

Human-readable, convenient, friendly durations. Converts durations given as strings to milliseconds or to custom units from milliseconds to weeks.

6 lines (5 loc) 1.46 kB
/*! * @standards/duration v1.1.3 | ISC @ Richard King <richrdkng@gmail.com> (www.richrdkng.com) */ var duration=function(){"use strict";const e={w:6048e5,wk:6048e5,wks:6048e5,week:6048e5,weeks:6048e5,d:864e5,dy:864e5,day:864e5,days:864e5,h:36e5,hr:36e5,hrs:36e5,hour:36e5,hours:36e5,m:6e4,mn:6e4,min:6e4,mins:6e4,minute:6e4,minutes:6e4,s:1e3,sec:1e3,secs:1e3,second:1e3,seconds:1e3,ms:1,msec:1,msecs:1,millisec:1,millisecond:1,milliseconds:1},t=/(-?)([\d\s\-_,.]+)\s*([a-zA-Z]*)/g,n=/[\s\-_,]/g,s={};function o(e){return"string"==typeof e&&e.length>0||"number"==typeof e&&e>-1/0&&e<1/0&&!isNaN(e)}function r(r,i,u){u=u||(i&&"object"==typeof i?i:{});const c=o(i)?i:0,f="string"==typeof u.unit?u.unit.toLowerCase():"ms",m="boolean"!=typeof u.round||u.round,a=r+""+c+f+m,y=a in s;if(y)return s[a];if("string"==typeof(r=o(r)?r:c)){let s=0;if(r.length>0)for(let o,i,u;o=t.exec(r);)i=parseFloat(o[2].replace(n,"")),o[1]&&(i=-i),isNaN(i)||(u=o[3].toLowerCase()||"ms",u in e&&(s+=i*e[u]));r=s}return f in e?r/=e[f]:r=0,0!==r&&m&&0===(r=Math.round(r))&&(r=Math.abs(r)),y||(s[a]=r),r}return r.createCustom=function(e,t,n){return function(s,o,i){let u="string"==typeof t||"number"==typeof t?t:0;"string"!=typeof o&&"number"!=typeof o||(u=o);let c=t&&"object"==typeof t?t:{};return n&&"object"==typeof n&&(c=n),i=i||(o&&"object"==typeof o?o:{}),r(s="string"==typeof s||"number"==typeof s?s:e,u,i=Object.assign(c,i))}},r}(); //# sourceMappingURL=index.iife.min.js.map