UNPKG

@sildeswj/common-libraries

Version:

This is common libraries used for all refeed projects

85 lines 3.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.roundToTwoDecimals = exports.calculateTotalPriceSubtractedNewOilTotalPrice = exports.calculateTotalPrice = exports.errorConvertor = exports.numberFormat = exports.currencyFormat = void 0; const constants_1 = require("./constants"); const localeMap = { ko: { locale: 'ko-KR', unit: '원' }, en: { locale: 'en-US', unit: '$' }, vn: { locale: 'vi-VN', unit: '₫' }, jp: { locale: 'ja-JP', unit: '円' }, }; const currencyFormat = (num, locale = null, fixedNumber = 0, includeCurrencySymbol = true) => { if (typeof num === 'string') { // Check if the string is a valid number if (!/^\d+(\.\d+)?$/.test(num)) { return undefined; } num = parseFloat(num); } if (typeof num !== 'number' || isNaN(num) || num === 0) { return undefined; } const defaultFormatted = num.toFixed(fixedNumber).replace(/\B(?=(\d{3})+(?!\d))/g, ','); if (!locale) return defaultFormatted; const formattedNumber = (0, exports.numberFormat)(num, locale, fixedNumber); if (locale === 'en') { return `${includeCurrencySymbol ? localeMap[locale].unit : ''}${formattedNumber}`; } return `${formattedNumber}${includeCurrencySymbol ? localeMap[locale].unit : ''}`; }; exports.currencyFormat = currencyFormat; const numberFormat = (num, locale = null, fixedNumber = 0) => { if (typeof num === 'string') { num = parseFloat(num); } return new Intl.NumberFormat(localeMap[locale].locale, { minimumFractionDigits: fixedNumber, maximumFractionDigits: fixedNumber, }).format(num); }; exports.numberFormat = numberFormat; const errorConvertor = (err) => { var _a; let errorStatus = 500; let errorMessage = 'Server error'; if (((_a = err === null || err === void 0 ? void 0 : err.errorResponse) === null || _a === void 0 ? void 0 : _a.code) === 11000) { errorStatus = 412; errorMessage = 'The unique value is already registered'; } if ((err === null || err === void 0 ? void 0 : err.status) === 413 || (err === null || err === void 0 ? void 0 : err.status) === 415) { errorStatus = err.status; errorMessage = err === null || err === void 0 ? void 0 : err.response; } return { errorStatus, errorMessage }; }; exports.errorConvertor = errorConvertor; const calculateTotalPrice = ({ measureOptions, actualCollectionKg, actualCollectionVolume, pricePerKg, pricePerCan, }) => { let totalPrice = 0; if (measureOptions === constants_1.COLLECTION_ORDER_MEASURE_OPTIONS.CAN) { totalPrice = Number((Number(actualCollectionVolume) * Number(pricePerCan)).toFixed(0)) || 0; } else { totalPrice = Number((Number(actualCollectionKg) * Number(pricePerKg)).toFixed(0)) || 0; } return totalPrice; }; exports.calculateTotalPrice = calculateTotalPrice; const calculateTotalPriceSubtractedNewOilTotalPrice = (totalPrice, newOilTotalPrice) => { return Number((Number(totalPrice) - Number(newOilTotalPrice)).toFixed(0)) || 0; }; exports.calculateTotalPriceSubtractedNewOilTotalPrice = calculateTotalPriceSubtractedNewOilTotalPrice; /** * Rounds a number to 2 decimal places * Examples: 23.3423423 -> 23.34, 4.5 -> 4.5, 4 -> 4, 94.889 -> 94.89 */ const roundToTwoDecimals = (value) => { if (value === undefined || value === null || isNaN(value)) { return 0; } return Math.round(value * 100) / 100; }; exports.roundToTwoDecimals = roundToTwoDecimals; //# sourceMappingURL=utils.js.map