UNPKG

eamutils

Version:

Some reusable functions in some projects

61 lines (60 loc) 2.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.padLeadingZeros = exports.formatNumberCommas = exports.formatNumberToK = void 0; /* * Format number to use: * k for thousands, * m for millions, * b for billions, * t for trillions * @param num Number to format * @param replaces Array of strings to replace the default ones (k, m, b, t). It must have 4 elements. */ function formatNumberToK(num, precision = 1, replaces = ['K', 'M', 'B', 'T']) { if (replaces && replaces.length !== 4) throw new Error(`Replacements should be an array of length: 4`); if (num >= 1000000000000) { return (num / 1000000000000).toFixed(precision).replace(/\.0$/, '') + replaces[3]; } if (num >= 1000000000) { return (num / 1000000000).toFixed(precision).replace(/\.0$/, '') + replaces[2]; } if (num >= 1000000) { return (num / 1000000).toFixed(precision).replace(/\.0$/, '') + replaces[1]; } if (num >= 1000) { return (num / 1000).toFixed(precision).replace(/\.0$/, '') + replaces[0]; } return num.toString(); } exports.formatNumberToK = formatNumberToK; /* * Format a number with commas format * @param number to be formatted * Example: 1000000 => 1,000,000 */ const formatNumberCommas = (x) => { if (!x) return x; // Check if it's less than 0 if (parseFloat(x.toString().split(',').join('')) <= 1) return x; if (parseFloat(x.toString()) > 200) x = parseFloat(x).toFixed(0); return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); }; exports.formatNumberCommas = formatNumberCommas; /* * Pad a number adding leading zeros * @param number to format with leading zeros * @param length number of final string length (after adding zeros to the left) * Example: number = 5 and size = 2 should return 05. * Example 2: number = 5 and size = 3 should return 005 */ const padLeadingZeros = (num, finalLength) => { let s = `${num}`; while (s.length < finalLength) s = `0${s}`; return s; }; exports.padLeadingZeros = padLeadingZeros;