UNPKG

@technobuddha/library

Version:
30 lines (29 loc) 1.15 kB
import { empty } from '../constants'; import { formatNumber } from '../formatNumber'; import isNil from 'lodash/isNil'; import round from 'lodash/round'; /** * Abbreviate a number by adding a suffix for metric units (i.e. 1000 => 1K, .0001 = 1m) * * @param input The number to abbreviate * @param __namedParameters {@link MetricUnitOptions} */ export function metricUnits(input, { format, pad, macro = ['K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'], micro = ['m', 'µ', 'n', 'p', 'f', 'a', 'z', 'y'], unit = 1000, precision = 2, } = {}) { let number = Math.abs(input); let suffix = empty; let index = 0; if (number < 1) { while ((number + Number.EPSILON) < 1 && index < micro.length) { suffix = micro[index++]; number = number * unit; } } else { while ((number + Number.EPSILON) >= unit && index < macro.length) { suffix = macro[index++]; number = number / unit; } } return (isNil(format) ? round(number, precision).toString() : formatNumber(round(number, precision), format)).padStart(pad ?? 0) + suffix; } export default metricUnits;