@visulima/humanizer
Version:
Humanizer is a library for humanizing data in a human-readable form.
2 lines (1 loc) • 1.99 kB
JavaScript
var U=Object.defineProperty;var x=(t,s)=>U(t,"name",{value:s,configurable:!0});import{durationLanguage as k,englishUnitMap as D}from"../language/en.js";import P from"../language/util/validate-duration-language.js";var T=Object.defineProperty,_=x((t,s)=>T(t,"name",{value:s,configurable:!0}),"h");const I={d:864e5,h:36e5,m:6e4,mo:2629746e3,ms:1,s:1e3,w:6048e5,y:31556952e3},p=/[-/\\^$*+?.()|[\]{}]/g,C=/^PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?$/i,G=/^(?:(\d+):)?(?:(\d+):)?(\d+)$/,H=/^[+-]?\d+(?:\.\d+)?$/,B=_((t,s)=>{if(typeof t!="string"||t.length===0)return;const{defaultUnit:w="ms",language:l=k}=s??{};P(l);const g=l.decimal??".",S=l.groupSeparator??",",v=l.placeholderSeparator??"_",A=g.replaceAll(p,String.raw`\$&`),y=S.replaceAll(p,String.raw`\$&`),j=v.replaceAll(p,String.raw`\$&`),d=l.unitMap??D;let i=t.replaceAll(new RegExp(`(\\d)[${j}${y}](\\d)`,"g"),"$1$2");if(g!=="."&&(i=i.replace(A,".")),H.test(t)){const e=Number.parseFloat(i.trim());if(!Number.isNaN(e)){const r=d[w];if(r!==void 0)return e*I[r]}return}const o=C.exec(t);if(o){const e=Number.parseInt(o[1]??"0",10),r=Number.parseInt(o[2]??"0",10),n=Number.parseInt(o[3]??"0",10);return e*36e5+r*6e4+n*1e3}const a=G.exec(t);if(a){let e=0,r=0,n=0;return a[2]!==void 0?(e=Number.parseInt(a[1]??"0",10),r=Number.parseInt(a[2],10)):a[1]!==void 0&&(r=Number.parseInt(a[1],10)),n=Number.parseInt(a[3]??"0",10),e*36e5+r*6e4+n*1e3}const M=Object.keys(d).toSorted((e,r)=>r.length-e.length).map(e=>e.replaceAll(p,String.raw`\$&`)).join("|"),m=new RegExp(`(-?\\d*\\.?\\d+)\\s*(${M})`,"gi");let f=0,u,c=!1,N=-1,b=0;for(m.lastIndex=0;(u=m.exec(i))!==null;){c||(N=u.index),c=!0;const e=u[1],r=u[2];if(!e||!r)continue;const n=e.trim(),L=n.startsWith("-")?-1:1,O=n.replace(/^[-+]/,""),$=Number.parseFloat(O),h=d[r.toLowerCase()];if(h===void 0)continue;const R=I[h];if(Number.isNaN($))return;f+=L*$*R,b=m.lastIndex}const E=i.slice(0,N).trim(),F=i.slice(b).trim();if(!(c&&(E.length>0||F.length>0))&&c)return f},"parseDuration");export{B as default};