"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ComputeExponentForMagnitude = ComputeExponentForMagnitude; var tslib_1 = require("tslib"); var decimal_js_1 = tslib_1.__importDefault(require("decimal.js")); var utils_1 = require("../utils"); decimal_js_1.default.set({ toExpPos: 100, }); /** * The abstract operation ComputeExponentForMagnitude computes an exponent by which to scale a * number of the given magnitude (power of ten of the most significant digit) according to the * locale and the desired notation (scientific, engineering, or compact). */ function ComputeExponentForMagnitude(internalSlots, magnitude) { var notation = internalSlots.notation, dataLocaleData = internalSlots.dataLocaleData, numberingSystem = internalSlots.numberingSystem; switch (notation) { case 'standard': return 0; case 'scientific': return magnitude.toNumber(); case 'engineering': var thousands = magnitude.div(3).floor(); return thousands.times(3).toNumber(); default: { (0, utils_1.invariant)(notation === 'compact', 'Invalid notation'); // Let exponent be an implementation- and locale-dependent (ILD) integer by which to scale a // number of the given magnitude in compact notation for the current locale. var compactDisplay = internalSlots.compactDisplay, style = internalSlots.style, currencyDisplay = internalSlots.currencyDisplay; var thresholdMap = void 0; if (style === 'currency' && currencyDisplay !== 'name') { var currency = dataLocaleData.numbers.currency[numberingSystem] || dataLocaleData.numbers.currency[dataLocaleData.numbers.nu[0]]; thresholdMap = currency.short; } else { var decimal = dataLocaleData.numbers.decimal[numberingSystem] || dataLocaleData.numbers.decimal[dataLocaleData.numbers.nu[0]]; thresholdMap = compactDisplay === 'long' ? decimal.long : decimal.short; } if (!thresholdMap) { return 0; } var num = decimal_js_1.default.pow(10, magnitude).toString(); var thresholds = Object.keys(thresholdMap); // TODO: this can be pre-processed if (num < thresholds[0]) { return 0; } if (num > thresholds[thresholds.length - 1]) { return thresholds[thresholds.length - 1].length - 1; } var i = thresholds.indexOf(num); if (i === -1) { return 0; } // See https://unicode.org/reports/tr35/tr35-numbers.html#Compact_Number_Formats // Special handling if the pattern is precisely `0`. var magnitudeKey = thresholds[i]; // TODO: do we need to handle plural here? var compactPattern = thresholdMap[magnitudeKey].other; if (compactPattern === '0') { return 0; } // Example: in zh-TW, `10000000` maps to `0000萬`. So we need to return 8 - 4 = 4 here. return (magnitudeKey.length - thresholdMap[magnitudeKey].other.match(/0+/)[0].length); } } }