Guest User

Untitled

a guest
Jun 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. // Run in console or as a snippet in Chrome
  2. (function() {
  3. const parseUs = x => parseFloat(x.replace(",",""));
  4. const parseEu = x => parseFloat(x.replace(/[,.]/g, y => y == "," ? "." : ","))
  5. const parsers = new Map([
  6. [",", parseEu],
  7. [".", parseUs]
  8. ]);
  9. const matcher = /^([^\d]?)([\d\,\.]+\d\d)([^\n\d]?)(\nCredit|)/m;
  10. const matchNode = x => matcher.exec(x.innerText);
  11. const mapMatch = (mt) => {
  12. const [_,c1,amount,c2, credit] = mt;
  13. const currency = c1 || c2;
  14. const parser = parsers.get(amount[amount.length -3]);
  15. return {currency, amount: parser(amount), credit: !!credit};
  16. };
  17. const reducePrices = (acc, {currency, amount, credit}) => {
  18. const previous = acc.get(currency) || 0;
  19. if(credit) amount = -amount;
  20. acc.set(currency, previous + amount);
  21. return acc;
  22. };
  23.  
  24. return Array.from(document.querySelectorAll('td.wht_total'))
  25. .map(matchNode)
  26. .map(mapMatch)
  27. .reduce(reducePrices, new Map());
  28. })();
Add Comment
Please, Sign In to add comment