num-beauty
Version:
An ultra lightweight module for formatting numbers into human-friendly strings
1 lines • 2.97 kB
JavaScript
import{locales}from"../locales/index.js";import{round}from"./rounding.service.js";import{formatNumber}from"./formatting.service.js";import{abbreviateNumber}from"./abbreviation.service.js";import{formatCurrency}from"./currency.service.js";import{formatBytes}from"./bytes.service.js";import{formatPercentage}from"./percentage.service.js";export function beautifyToParts(e,t={}){const{locale:r="en-US",decimals:s=2,abbreviated:i=!1,stripZeros:a=!1,roundingMode:o="HALF_UP",currency:n,showSymbol:c=!0,showCode:l=!1,bytes:u=!1,bytesBinary:m=!0,bytesLongFormat:y=!1,percentage:p=!1,percentageMultiply:b=!0,percentageAddSpace:h}=t;let f;if(n)f=formatCurrency(e,r,{currency:n,decimals:s,showSymbol:c,showCode:l,stripZeros:a});else if(u)f=formatBytes(e,{binary:m,decimals:s,stripZeros:a,locale:r,longFormat:y});else if(p)f=formatPercentage(e,{multiply:b,decimals:s,stripZeros:a,locale:r,addSpace:h});else{const t=round(e,s,o);f=formatNumber(t,s,r,a),i&&(f=abbreviateNumber(e,f,r))}return parseFormattedString(f,r,{hasCurrency:!!n,hasPercentage:p,hasUnit:i||u})}function parseFormattedString(e,t,r){const s=[],i=locales[t],a=new Intl.NumberFormat(t).format(1.1).charAt(1),o=new Intl.NumberFormat(t).format(1e3).charAt(1);let n=e.trim();if(n.startsWith("-")?(s.push({type:"minusSign",value:"-"}),n=n.slice(1).trim()):n.startsWith("+")&&(s.push({type:"plusSign",value:"+"}),n=n.slice(1).trim()),r.hasCurrency){const e={...i.currencies};for(const t in e){const r=e[t];if(r&&r.symbol&&n.startsWith(r.symbol)){s.push({type:"currency",value:r.symbol}),n=n.slice(r.symbol.length).trim();break}if(n.startsWith(t)){s.push({type:"currency",value:t}),n=n.slice(t.length).trim();break}}}let c=n.match(new RegExp(`^([\\d${"."===o?"\\.":o}]+)(${a}([\\d]+))?`));if(c){const e=c[1],t=c[3];s.push({type:"integer",value:e}),c[2]&&(s.push({type:"decimal",value:a}),s.push({type:"fraction",value:t})),n=n.slice(c[0].length).trim()}if(r.hasUnit&&n){const e=/^(B|KB|MB|GB|TB|PB|EB|KiB|MiB|GiB|TiB|PiB|EiB|Bytes?|Kilobytes?|Megabytes?|Gigabytes?|Terabytes?|Petabytes?|Exabytes?|Kibibytes?|Mebibytes?|Gibibytes?|Tebibytes?|Pebibytes?|Exbibytes?)/i,t=n.match(e);if(t)s.push({type:"unit",value:t[1]}),n=n.slice(t[0].length).trim();else{const e=i.units||[];let t=!1;for(let r=e.length-1;r>=0;r--){const i=e[r];if(i){for(const e of i)if(e&&n.toLowerCase().startsWith(e.toLowerCase())){s.push({type:"unit",value:e}),n=n.slice(e.length).trim(),t=!0;break}if(t)break}}if(!t&&/^[kMBT]/.test(n)){const e=n.charAt(0);s.push({type:"unit",value:e}),n=n.slice(1).trim()}}}if(r.hasPercentage&&n.includes("%")&&(n.startsWith(" ")&&(s.push({type:"literal",value:" "}),n=n.slice(1)),s.push({type:"percentSign",value:"%"}),n=n.replace("%","").trim()),r.hasCurrency&&n){const e={...i.currencies};for(const t in e){const r=e[t];if(r&&r.symbol&&n.includes(r.symbol)){s.push({type:"literal",value:" "}),s.push({type:"currency",value:r.symbol}),n=n.replace(r.symbol,"").trim();break}}}return n&&s.push({type:"literal",value:n}),s}