UNPKG

nums2persian

Version:
60 lines (59 loc) 1.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getRandomNumber = exports.sum = exports.currencyFormatDE = exports.currencyFormat = exports.formatNumberShowZero = exports.formatNumber = void 0; const utilityKeys_1 = require("./utilityKeys"); const formatNumber = (num, convertToPersian) => { if (typeof num === 'number') { return exports.formatNumberShowZero(num, convertToPersian); } else if (!num) { return ''; } else if (typeof num === 'string') { return utilityKeys_1.englishStringNumberToPersianString(num); } else { return ''; } }; exports.formatNumber = formatNumber; const formatNumberShowZero = (num, convertToPersian) => { if (typeof num === 'number') { const result = num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,'); if (convertToPersian) { return utilityKeys_1.englishStringNumberToPersianString(result); } return result; } return ''; }; exports.formatNumberShowZero = formatNumberShowZero; const currencyFormat = (num) => { if (!num) { return ''; } return '$' + num.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,'); }; exports.currencyFormat = currencyFormat; const currencyFormatDE = (num) => { if (!num) { return ''; } return (num .toFixed(2) // always two decimal digits .replace('.', ',') // replace decimal point character with , .replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1.') + ' €'); }; exports.currencyFormatDE = currencyFormatDE; const sum = (array, key) => { if (array && array.length > 0 && typeof array[0][key] === 'number') { const result = array.reduce((a, b) => a + (b[key] || 0), 0); return result; } return 0; }; exports.sum = sum; const getRandomNumber = (min, max) => { return Math.floor(Math.random() * (max - min + 1) + min); }; exports.getRandomNumber = getRandomNumber;