UNPKG

@activecollab/components

Version:

ActiveCollab Components

166 lines (163 loc) • 9.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseValueToNumber = exports.numberWithSeparator = exports.getNumberFromString = exports.formatNumber = exports.formatCurrency = exports.fixedDecimalSpaces = exports.currencyMultiplier = void 0; function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } 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. * * @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 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; while (absoluteNumber >= 1000 && scale < suffixes.length - 1 && format === "short") { 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); if (!currencyCode) { return formattedNum; } return currencyCodePosition === "right" ? "".concat(formattedNum, " ").concat(currencyCode) : "".concat(currencyCode, " ").concat(formattedNum); }; var parseValueToNumber = exports.parseValueToNumber = function parseValueToNumber(value, thousandSeparator, decimalSeparator) { var stringValue = String(value).trim(); if (stringValue.includes(":")) { var _stringValue$split = stringValue.split(":"), _stringValue$split2 = _slicedToArray(_stringValue$split, 2), hoursPart = _stringValue$split2[0], minutesPart = _stringValue$split2[1]; var hours = parseFloat(hoursPart) || 0; var minutes = parseFloat(minutesPart) || 0; return hours + minutes / 60; } return +stringValue.replaceAll(thousandSeparator, "").replace(decimalSeparator, "."); }; //# sourceMappingURL=currencyUtils.js.map