@activecollab/components
Version:
ActiveCollab Components
152 lines (149 loc) • 8.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.numberWithSeparator = exports.getNumberFromString = exports.formatNumber = exports.formatCurrency = exports.fixedDecimalSpaces = exports.currencyMultiplier = void 0;
var currencyMultiplier = exports.currencyMultiplier = {
k: 1000,
m: 1000000,
b: 1000000000,
t: 1000000000000
};
var fixedDecimalSpaces = exports.fixedDecimalSpaces = function fixedDecimalSpaces(num, fixed) {
fixed = fixed || 0;
fixed = Math.pow(10, fixed);
var parts = num.toString().split(".");
if (parts.length > 1 && parseInt(parts[1]) > fixed) {
return Math.floor(num * fixed) / fixed;
}
return Math.round(num * fixed) / fixed;
};
var getNumberFromString = exports.getNumberFromString = function getNumberFromString(number) {
var thousandSeparator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ",";
var decimalSeparator = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ".";
if (typeof number === "string" && number === "") {
return NaN;
}
if (!isNaN(Number(number)) || typeof number === "number") {
return Number(number);
}
if (decimalSeparator === ",") {
var parts = number.split(decimalSeparator);
var result = 0;
if (parts.length === 2) {
result = parts[0] ? parseFloat(String(parts[0]).replaceAll(thousandSeparator, "")) : 0;
if (result < 0) {
result -= parts[1] ? parseFloat(String(parts[1])) / Math.pow(10, parts[1].length) : 0;
} else {
result += parts[1] ? parseFloat(String(parts[1])) / Math.pow(10, parts[1].length) : 0;
}
return result;
}
if (parts.length === 1) {
result = parseFloat(String(parts[0]).replaceAll(thousandSeparator, ""));
return result;
}
return 0;
} else {
return parseFloat(String(number).replaceAll(thousandSeparator, ""));
}
};
/**
* @function formatNumber
* @description
* Formats a number or string into a human-readable format with options for thousand separators,
* decimal precision, and shortening the number using suffixes (K, M, B, T).
* It can handle negative numbers and optionally trim decimals and set number of decimal places based on the configuration.
*
* @param {string | number} n - The number or string to be formatted.
* @param {"," | "." | " " | ""} [thousandSeparator=","] - The character to use as a thousand separator.
* @param {"," | "."} [decimalSeperator="."] - The character to use as a decimal separator.
* @param {boolean} [trimDecimals=true] - Whether to trim decimals to the specified number of decimal spaces.
* @param {number} [decimalSpaces=2] - The number of decimal spaces to keep in the formatted output.
* @param {"long" | "short"} [format="long"] - The format type, either "long" for full numbers or "short" for shortened numbers with suffixes.
* @param {number} [shortenThreshold=1000] - The minimum numeric value at which the number should be shortened
* using suffixes. Numbers below this threshold will always be shown in long format.
*
* @returns {string} - The formatted number as a string with separators and potentially with decimals.
*
* @example
* formatNumber(1500) // "1,500.00"
* formatNumber(1500000, ",", ".", true, 2, "short") // "1.5M"
*/
var formatNumber = exports.formatNumber = function formatNumber(n) {
var thousandSeparator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ",";
var decimalSeperator = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ".";
var trimDecimals = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
var decimalSpaces = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 2;
var format = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : "long";
var shortenThreshold = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : 1000;
if (n === undefined || n === null) return "";
var number = getNumberFromString(n, thousandSeparator, decimalSeperator);
if (isNaN(number)) {
return "";
}
var isNegative = number < 0;
var absoluteNumber = Math.abs(number);
var suffixes = ["", "K", "M", "B", "T"];
var scale = 0;
var shouldShorten = absoluteNumber >= shortenThreshold && format === "short";
while (shouldShorten && scale < suffixes.length - 1 && absoluteNumber >= 1000) {
absoluteNumber /= 1000;
scale++;
}
var formattedNum = scale === 0 ? fixedDecimalSpaces(absoluteNumber, decimalSpaces) : parseFloat(absoluteNumber.toFixed(1));
if (formattedNum === 1000 && scale > 0 && scale < 4) {
formattedNum = 1;
scale++;
}
var result = numberWithSeparator(trimDecimals ? formattedNum : formattedNum.toFixed(decimalSpaces), thousandSeparator, decimalSeperator);
return isNegative ? "-".concat(result).concat(suffixes[scale]) : "".concat(result).concat(suffixes[scale]);
};
var numberWithSeparator = exports.numberWithSeparator = function numberWithSeparator(x) {
var thousandSeparator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ",";
var decimalSeparator = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ".";
var format = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
if (!format) {
return x;
}
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, thousandSeparator);
return parts.join(decimalSeparator);
};
/**
* @function formatCurrency
* @description
* Formats a number or string as a currency string with options for thousand separators,
* decimal precision, currency code, currency code position, and the use of suffixes (K, M, B, T) if needed.
* It can handle negative values and supports customization of the currency code and its position.
*
* @param {string | number} n - The number or string to be formatted as currency.
* @param {"," | "." | " " | ""} [thousandSeparator=","] - The character to use as a thousand separator.
* @param {"," | "."} [decimalSeperator="."] - The character to use as a decimal separator.
* @param {boolean} [trimDecimals=false] - Whether to trim decimals to the specified number of decimal spaces.
* @param {number} [decimalSpaces=2] - The number of decimal spaces to keep in the formatted output.
* @param {"long" | "short"} [format="long"] - The format type, either "long" for full numbers or "short" for shortened numbers with suffixes.
* @param {string} [currencyCode=""] - The currency code to append or prepend to the formatted number.
* @param {"right" | "left"} [currencyCodePosition="right"] - The position of the currency code relative to the formatted number.
*
* @returns {string} - The formatted currency string, including the currency code if provided.
*
* @example
* formatCurrency(1500, ",", ".", false, 2, "long", "USD", "right") // "1,500.00 USD"
* formatCurrency(1500000, ",", ".", true, 2, "short", "JPY", "left") // "JPY 1.5M"
*/
var formatCurrency = exports.formatCurrency = function formatCurrency(n) {
var thousandSeparator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ",";
var decimalSeperator = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ".";
var trimDecimals = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var decimalSpaces = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 2;
var format = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : "long";
var currencyCode = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : "";
var currencyCodePosition = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : "right";
var formattedNum = formatNumber(n, thousandSeparator, decimalSeperator, trimDecimals, decimalSpaces, format, 10000);
if (!currencyCode) {
return formattedNum;
}
return currencyCodePosition === "right" ? "".concat(formattedNum, " ").concat(currencyCode) : "".concat(currencyCode, " ").concat(formattedNum);
};
//# sourceMappingURL=currencyUtils.js.map