UNPKG

make-currency

Version:

[![npm](https://img.shields.io/npm/v/make-currency)](https://www.npmjs.com/package/make-currency) [![NPM](https://img.shields.io/npm/l/make-currency)](https://www.npmjs.com/package/make-currency)

192 lines (177 loc) 4.93 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var USD = { lang: 'en-US', currency: 'USD', removePrefix: function (value) { return value.slice(1); } }; var index$5 = { lang: 'pt-BR', currency: 'BRL', removePrefix: function (value) { return value.slice(3); } }; var index$4 = { lang: 'pt-PT', currency: 'EUR', removePrefix: function (value) { return value.slice(0, -2); } }; var index$3 = { lang: 'en-US', currency: 'USD', removePrefix: function (value) { return value.slice(1); }, replaceSymbol: function (value) { return value.replace('$', '¥'); } }; var index$2 = { lang: 'es-MX', currency: 'MXN', removePrefix: function (value) { return value.slice(1); } }; var index$1 = { lang: 'ru-RU', currency: 'RUB', removePrefix: function (value) { return value.slice(0, -2); } }; var index = { lang: 'en-GB', currency: 'GBP', removePrefix: function (value) { return value.slice(1); } }; var TYPES = /*#__PURE__*/Object.freeze({ __proto__: null, BRL: index$5, EUR: index$4, GBP: index, JPY: index$3, MXN: index$2, RUB: index$1, USD: USD }); //locale var Configure = /** @class */function () { function Configure() { this.CURRENT = USD; } Configure.prototype.setMoney = function (money) { this.CURRENT = money; }; return Configure; }(); var configure = new Configure(); // modules function toNumber(numero) { var partes = numero.toString().split('.'); var numeroFormatado = partes[0]; if (partes[1]) { numeroFormatado += '.' + partes[1].substring(0, 2); } else { numeroFormatado += '.00'; } return parseFloat(numeroFormatado); } /** * Returns a formatted value of currency. * * @example * Here's a simple example: * ``` * currency(2.99) // Prints "$2.99": * ``` * @param {number} floatValue * @param {object} options - optional * @version 0.0.4 * @see https://github.com/holasoycael/make-currency * @returns {string} */ var currency = function (floatValue, options) { var _a, _b, _c, _d; var symbol, isEmpty, money; if (options === 'INPUT') { symbol = false; isEmpty = true; money = configure.CURRENT; } else { symbol = (_a = options === null || options === void 0 ? void 0 : options.symbol) !== null && _a !== void 0 ? _a : true; isEmpty = (_b = options === null || options === void 0 ? void 0 : options.isEmpty) !== null && _b !== void 0 ? _b : false; money = (_c = options === null || options === void 0 ? void 0 : options.money) !== null && _c !== void 0 ? _c : configure.CURRENT; } var replaceSymbolFn = (_d = money === null || money === void 0 ? void 0 : money.replaceSymbol) !== null && _d !== void 0 ? _d : function (value) { return value; }; var current = toNumber(floatValue); var defaultPrice = new Intl.NumberFormat(money.lang, { style: 'currency', currency: money.currency, useGrouping: true }).format(current); var primaryPrice = replaceSymbolFn(defaultPrice.replace(/[\u00A0]/g, ' ')); var formatValue = money.removePrefix(primaryPrice); var value = symbol ? primaryPrice : formatValue; return floatValue ? value : isEmpty ? '' : value; }; // models /** * Returns string and float value of currency. * * @example * Here's a simple example: * ``` * currencyFn('29,99') // Prints { floatValue: 29.99, stringValue: '29,99' }: * ``` * @param {string} value * @param {object} options * @returns {string} */ var currencyFn = function (value, options) { var _a, _b; var currenctValue = value.padStart(3, '0'); currenctValue = currenctValue.replace(/\D/g, ''); currenctValue = currenctValue.replace(/(\d)(\d{2})$/, '$1,$2'); currenctValue = currenctValue.replace(/(?=(\d{3})+(\D))\B/g, '.'); var _c = currenctValue.padStart(3, '0').replace(/(\d)(\d{2})$/, '$1,$2').split(','), thousand = _c[0], decimal = _c[1]; var parseThousand = thousand.replace(/\D/g, ''); var parseFloatValue = "".concat(parseThousand, ".").concat(decimal); var floatValue = parseFloat(parseFloatValue); var __options = { symbol: (_a = options === null || options === void 0 ? void 0 : options.symbol) !== null && _a !== void 0 ? _a : false, isEmpty: (_b = options === null || options === void 0 ? void 0 : options.isEmpty) !== null && _b !== void 0 ? _b : true, money: options === null || options === void 0 ? void 0 : options.money }; return { floatValue: floatValue, stringValue: currency(floatValue, __options) }; }; var CONFIGURE = function (_a) { var money = _a.money; configure.setMoney(money); }; var Make = { currency: currency, currencyFn: currencyFn, TYPES: TYPES, CONFIGURE: CONFIGURE }; exports.CONFIGURE = CONFIGURE; exports.TYPES = TYPES; exports.currency = currency; exports.currencyFn = currencyFn; exports.default = Make; //# sourceMappingURL=index.js.map