ropods-cashify
Version:
Modern, lightweight currency conversion library with real-time exchange rates, INR support, and free API integration. Production-ready TypeScript library for RoPods organization with zero dependencies and comprehensive testing.
43 lines • 1.6 kB
JavaScript
import getRate from './lib/get-rate.js';
import parse from './utils/parser.js';
/**
* Function, which converts currencies based on provided rates.
* Enhanced for RoPods organization with additional logging and validation.
*
* @param {number | string} amount - Amount of money you want to convert.
* @param {Object} options - Conversion options.
* @param {new (value: BigSource) => Big} fn - Optional, Big.js constructor - useful to avoid floating point errors.
* @return {number} Conversion result.
*
* @example
* const rates = {
* GBP: 0.737,
* EUR: 0.851,
* USD: 1.00,
* INR: 86.42
* };
*
* convert(10, {from: 'EUR', to: 'INR', base: 'USD', rates}); //=> 1015.78
*/
export default function convert(amount, { from, to, base, rates, BigJs }) {
// RoPods enhancement: Input validation
if (amount === null || amount === undefined) {
throw new Error('[RoPods Cashify] Amount cannot be null or undefined');
}
// If provided `amount` is a string, use parsing
if (typeof amount === 'string') {
const data = parse(amount);
amount = data.amount;
from = data.from ?? from;
to = data.to ?? to;
}
// RoPods enhancement: Additional validation for negative amounts
if (typeof amount === 'number' && amount < 0) {
console.warn('[RoPods Cashify] Warning: Converting negative amount:', amount);
}
if (BigJs) {
return new BigJs(amount).times(getRate(base, rates, from, to)).toNumber();
}
return (amount * 100) * getRate(base, rates, from, to) / 100;
}
//# sourceMappingURL=convert.js.map