Skip to content

Instantly share code, notes, and snippets.

@henryoswald
Created October 9, 2014 10:15
Show Gist options
  • Save henryoswald/77879ff08632a3dfff8e to your computer and use it in GitHub Desktop.
Save henryoswald/77879ff08632a3dfff8e to your computer and use it in GitHub Desktop.
this can be used to total together transactions expored by recutly by groups such as the EU and USA
euCountries = ["AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", "DE", "EL", "HU", "IE", "IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT", "RO", "SK", "SI", "ES", "SE", "GB"]
_ = require("underscore")
fs = require("fs")
file = fs.readFileSync("./transactions.csv", "utf-8")
lines = file.split("\n")
groupedTransactions = _.groupBy lines, (line)->
vals = line.split(",")
countryCode = vals[20]
if _.contains(euCountries, countryCode)
return "EU"
else
return countryCode
countryInfo = {}
_.map groupedTransactions, (transactionGroup, countryCode)->
totalValue = 0
countryInfo[countryCode] = {}
_.each transactionGroup, (transaction)->
vals = transaction.split(",")
message = vals[8]
ammount = vals[4]
type = vals[3]
if message == "success" and type == "purchase"
totalValue += parseInt(ammount)
if message == "success" and type == "refund"
totalValue -= parseInt(ammount)
console.log totalValue:totalValue, countryCode:countryCode
return totalValue:totalValue, countryCode:countryCode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment