UNPKG

786 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.formatDecimal = void 0;
4const NUMBER_REGEX = new RegExp('(\\d+?)(?=(\\d{3})+(?!\\d)|$)', 'g');
5/**
6 * @name formatDecimal
7 * @description Formats a number into string format with thousand separators
8 */
9function formatDecimal(value, separator = ',') {
10 // We can do this by adjusting the regx, however for the sake of clarity
11 // we rather strip and re-add the negative sign in the output
12 const isNegative = value[0].startsWith('-');
13 const matched = isNegative
14 ? value.substring(1).match(NUMBER_REGEX)
15 : value.match(NUMBER_REGEX);
16 return matched
17 ? `${isNegative ? '-' : ''}${matched.join(separator)}`
18 : value;
19}
20exports.formatDecimal = formatDecimal;