Skip to content

Instantly share code, notes, and snippets.

@graffic
Last active June 20, 2018 06:59
Show Gist options
  • Save graffic/522d5e958bb5aabe9bbb2fb03a042024 to your computer and use it in GitHub Desktop.
Save graffic/522d5e958bb5aabe9bbb2fb03a042024 to your computer and use it in GitHub Desktop.
Sum Steam purchase history from https://store.steampowered.com/account/history
// 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());
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment