UNPKG

@sildeswj/common-libraries

Version:

This is common libraries used for all refeed projects

86 lines 3.87 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.calculateTotalPrice = exports.errorConvertor = exports.currencyFormat = exports.encryptedPassword = void 0; const bcrypt = __importStar(require("bcryptjs")); const constants_1 = require("./constants"); const encryptedPassword = async (password) => { const hashedPassword = await bcrypt.hash(password.toString(), 10); return hashedPassword; }; exports.encryptedPassword = encryptedPassword; 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 localeMap = { ko: { locale: 'ko-KR', unit: '원' }, en: { locale: 'en-US', unit: '$' }, vn: { locale: 'vi-VN', unit: '₫' }, jp: { locale: 'ja-JP', unit: '円' }, }; const formattedNumber = new Intl.NumberFormat(localeMap[locale].locale, { minimumFractionDigits: fixedNumber, maximumFractionDigits: fixedNumber, }).format(num); if (locale === 'en') { return `${includeCurrencySymbol ? localeMap[locale].unit : ''}${formattedNumber}`; } return `${formattedNumber}${includeCurrencySymbol ? localeMap[locale].unit : ''}`; }; exports.currencyFormat = currencyFormat; 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, kgPerCan, }) => { if (measureOptions === constants_1.COLLECTION_ORDER_MEASURE_OPTIONS.CAN) { return Number((Number(actualCollectionVolume) * Number(pricePerKg) * Number(kgPerCan)).toFixed(0)); } return Number((Number(actualCollectionKg) * Number(pricePerKg)).toFixed(0)); }; exports.calculateTotalPrice = calculateTotalPrice; //# sourceMappingURL=utils.js.map