UNPKG

typescript-libraries

Version:
63 lines (62 loc) 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TSNumber = void 0; var TSNumber = /** @class */ (function () { function TSNumber() { } TSNumber.roundBy = function (n, d, type) { if (d === void 0) { d = 5; } if (type === void 0) { type = 'round'; } return Math[type](n / d) * d; }; TSNumber.random = function (from, to) { return Math.floor(from + (Math.random() * (to - from + 1))); }; TSNumber.humanize = function (number, options) { if (options === void 0) { options = {}; } options = options || {}; var result; if (options.hasOwnProperty('unit')) { result = parseInt(number.toString(), 10); var s = options.separator || '', cultures = { 'en': { '': ['', 0], 'Thousand': ['K', 3], 'Million': ['M', 6], 'Billion': ['B', 9], 'Trillion': ['T', 12], 'Quadrillion': ['q', 15], 'Quintillion': ['Q', 18] }, 'fr': { '': ['', 0], 'Mille': ['K', 3], 'Million': ['M', 6], 'Milliard': ['Md', 9], 'Mille Milliards': ['T', 12], 'Quadrillion': ['q', 15], 'Quintillion': ['Q', 18] } }[options.culture || 'en']; var zeroes = 0; var sM = {}, lM = {}; for (var c in cultures) { if (cultures.hasOwnProperty(c)) { sM[c] = cultures[c][0]; lM[cultures[c][1]] = c; } } while (parseInt((result / 1000).toString(), 10) !== 0) { result /= 1000; zeroes += 3; } return [!options.fixed ? Math.floor(result) : result.toFixed(options.fixed), options.unit !== 'long' ? sM[lM[zeroes]] : lM[zeroes]].join(s); } else { var d = options.delimiter || ','; var s = options.separator || '.'; var f = options.fixed || 3; result = number.toString().split('.'); result[0] = result[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1' + d); if (result[1]) { result[1] = result[1].substring(0, f); } if (d === s) { return result[0]; } return result.join(s); } }; return TSNumber; }()); exports.TSNumber = TSNumber;