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.61 kB
/*! * @standards/duration v1.1.3 | ISC @ Richard King <richrdkng@gmail.com> (www.richrdkng.com) */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).duration=t()}(this,(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,o={};function s(e){return"string"==typeof e&&e.length>0||"number"==typeof e&&e>-1/0&&e<1/0&&!isNaN(e)}function i(i,r,f){f=f||(r&&"object"==typeof r?r:{});const u=s(r)?r:0,c="string"==typeof f.unit?f.unit.toLowerCase():"ms",m="boolean"!=typeof f.round||f.round,p=i+""+u+c+m,d=p in o;if(d)return o[p];if("string"==typeof(i=s(i)?i:u)){let o=0;if(i.length>0)for(let s,r,f;s=t.exec(i);)r=parseFloat(s[2].replace(n,"")),s[1]&&(r=-r),isNaN(r)||(f=s[3].toLowerCase()||"ms",f in e&&(o+=r*e[f]));i=o}return c in e?i/=e[c]:i=0,0!==i&&m&&0===(i=Math.round(i))&&(i=Math.abs(i)),d||(o[p]=i),i}return i.createCustom=function(e,t,n){return function(o,s,r){let f="string"==typeof t||"number"==typeof t?t:0;"string"!=typeof s&&"number"!=typeof s||(f=s);let u=t&&"object"==typeof t?t:{};return n&&"object"==typeof n&&(u=n),r=r||(s&&"object"==typeof s?s:{}),i(o="string"==typeof o||"number"==typeof o?o:e,f,r=Object.assign(u,r))}},i})); //# sourceMappingURL=index.umd.min.js.map