Source

number/shortenNumberWithUnit.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var divideDeep_1 = __importDefault(require("../math/divideDeep"));
/**
 * Get a shorten number with unit (e.g. 1K, 1M)
 *
 * @param num The number to parse. E.g. 300000
 * @param divisor The divisor for each depth. E.g. 1000
 * @param unitsMap The mapping of depth to unit. E.g. ['K', 'M', 'B']
 *
 * @returns A tuple type of [<Displayed number>, <Displayed units>] format. E.g. [300, 'K']
 * @category number
 * @module shortenNumberWithUnit
 */
var shortenNumberWithUnit = function (num, divisor, unitsMap) {
    var _a = (0, divideDeep_1.default)(num, divisor, unitsMap.length - 1), normalizedNum = _a[0], depth = _a[1];
    var fixedNum = Number(normalizedNum.toFixed(2));
    var unit = unitsMap[depth];
    return [fixedNum, unit];
};
exports.default = shortenNumberWithUnit;