make-currency
Version:
[](https://www.npmjs.com/package/make-currency) [](https://www.npmjs.com/package/make-currency)
140 lines (130 loc) • 3.91 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var USD = {
lang: 'en-US',
currency: 'USD',
removePrefix: function (value) {
return value.slice(1);
}
};
var BRL = {
lang: 'pt-BR',
currency: 'BRL',
removePrefix: function (value) {
return value.slice(3);
}
};
var TYPES = /*#__PURE__*/Object.freeze({
__proto__: null,
BRL: BRL,
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 convert(numero) {
var partes = numero.toString().split('.');
var numeroFormatado = partes[0];
if (partes[1]) {
numeroFormatado += '.' + partes[1].substring(0, 2);
} else {
numeroFormatado += '.00';
}
return 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;
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 current = parseFloat(convert(floatValue));
var defaultPrice = current.toLocaleString(money.lang, {
style: 'currency',
currency: money.currency
});
var primaryPrice = 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