UNPKG

numify

Version:

Shortern long numbers to Human Readable K M B format

2 lines (1 loc) 1.55 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e={en:{decimal:".",thousand:",",system:"international"},de:{decimal:",",thousand:".",system:"international"},fr:{decimal:",",thousand:" ",system:"international"},es:{decimal:",",thousand:".",system:"international"},in:{decimal:".",thousand:",",system:"indian"},it:{decimal:",",thousand:".",system:"international"},ch:{decimal:".",thousand:"'",system:"international"},se:{decimal:",",thousand:" ",system:"international"}},t={k:"thousand",M:"million",B:"billion",T:"trillion",E:"quintillion",P:"quadrillion",Cr:"crore",L:"lakh"},n=t=>t?"international"===t||"indian"===t?{decimal:".",thousand:",",system:t}:e[t]||e.en:e.en;exports.formatNumber=(e,t={})=>{const{formatType:i="en"}=t,a=n(i),s=e.toString().split(".");return s[0]=s[0].replace(/\B(?=(\d{3})+(?!\d))/g,a.thousand),s.length>1?s.join(a.decimal):s[0]},exports.numify=(e,i={})=>{const{formatType:a="en",precise:s=!1,style:o="short",units:r}=i;e=Number(e.toString().replace(/[^0-9.]/g,""));const l=n(a);if(e<1e3)return s?e.toFixed(2).replace(".",l.decimal):e.toString()+(r?1===e?` ${r[0]}`:` ${r[1]}`:"");let d;d=r?[[1,1===e?r[0]:r[1]]]:"indian"===l.system?[[1e7,"Cr"],[1e5,"L"],[1e3,"K"]]:[[1e18,"E"],[1e15,"P"],[1e12,"T"],[1e9,"B"],[1e6,"M"],[1e3,"k"]];const m=d.find((([t])=>e>=t));if(!m)return e.toString()+(r?1===e?` ${r[0]}`:` ${r[1]}`:"");const c=(e/m[0]).toFixed(2).replace(/\.0+$|(\.[0-9]*[1-9])0+$/,"$1").replace(".",l.decimal),u="long"!==o||r?m[1]:` ${t[m[1]]}`;return c+(r?1===e?` ${r[0]}`:` ${r[1]}`:u)};