Skip to content

Instantly share code, notes, and snippets.

@blacksheep557
Last active May 21, 2020 07:48
Show Gist options
  • Select an option

  • Save blacksheep557/a6660056abc54e9b7bda2dffcdc19a0f to your computer and use it in GitHub Desktop.

Select an option

Save blacksheep557/a6660056abc54e9b7bda2dffcdc19a0f to your computer and use it in GitHub Desktop.
const axios = require('axios');
async function getTotalPriceInBaseCurrency(prices, baseCurrency, date) {
prices = prices.replace(/\$/gm, 'USD').replace(/€/gm, 'EUR').replace(/₹/gm, 'INR').replace('£', 'GBP').replace(/,/gm, '');
let itemList = {};
prices.replace(/(([A-Z]{3}) {0,}([\d*\.{0,1}\d*,]*))/gm, (m, g1, g2, g3) => {
if (itemList[g2]!==undefined) {
itemList[g2] += Number(g3)
} else {
itemList[g2] = Number(g3);
}
});
let currencyData = await axios.get('https://openexchangerates.org/api/historical/' + date + '.json?app_id=f66d5b8ebf9140dd948fe5aa7d564624');
let rates = (await currencyData.data)['rates'];
let amountInUSD = 0;
for (let currency in itemList) {
amountInUSD += (1 / rates[currency]) * (itemList[currency])
}
return parseFloat((rates[baseCurrency] * amountInUSD).toFixed(2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment