Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function formatShortNumber(n: number, format: Pattern, bundle: FluentBundle) {
const lastIndexOf0 = format.lastIndexOf("0");
const unit = format.substr(lastIndexOf0 + 1);
const rest = format.substr(0, lastIndexOf0 + 1);
const splitted = rest.split(".");
const digits = splitted[0].length;
const fractalDigits = (splitted.length > 1 && splitted[1].length) || 0;
const threshold = Math.pow(10, digits);
while (n > threshold) {
n /= 10;
}
const formattedNumber = new FluentNumber(n, {
maximumFractionDigits: fractalDigits,
}).toString(bundle);
return `${formattedNumber}${unit}`;
}