Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Run in console or as a snippet in Chrome
- (function() {
- const parseUs = x => parseFloat(x.replace(",",""));
- const parseEu = x => parseFloat(x.replace(/[,.]/g, y => y == "," ? "." : ","))
- const parsers = new Map([
- [",", parseEu],
- [".", parseUs]
- ]);
- const matcher = /^([^\d]?)([\d\,\.]+\d\d)([^\n\d]?)(\nCredit|)/m;
- const matchNode = x => matcher.exec(x.innerText);
- const mapMatch = (mt) => {
- const [_,c1,amount,c2, credit] = mt;
- const currency = c1 || c2;
- const parser = parsers.get(amount[amount.length -3]);
- return {currency, amount: parser(amount), credit: !!credit};
- };
- const reducePrices = (acc, {currency, amount, credit}) => {
- const previous = acc.get(currency) || 0;
- if(credit) amount = -amount;
- acc.set(currency, previous + amount);
- return acc;
- };
- return Array.from(document.querySelectorAll('td.wht_total'))
- .map(matchNode)
- .map(mapMatch)
- .reduce(reducePrices, new Map());
- })();
Add Comment
Please, Sign In to add comment