Skip to content

Instantly share code, notes, and snippets.

@weiland
Created June 20, 2014 14:36
Show Gist options
  • Save weiland/dbf912b9aeacd6465d76 to your computer and use it in GitHub Desktop.
Save weiland/dbf912b9aeacd6465d76 to your computer and use it in GitHub Desktop.
Calculate with nodejs and with your Spendee exported .csv how much money you spent on certain things.
var fs = require('fs');
var Money = function(value) {
this.rawMoney = value;
this.parsedMoney = "0";
};
Money.prototype.parse = function() {
return this.parsedMoney = parseInt(this.rawMoney.toString().replace(/€/g, '').replace(/ /g, '').replace(/,/g, '.').replace(/\"/g, ''));
};
var Spending = function(SpendingArr) {
this.spedingArray = SpendingArr;
this.finalSpending = (function(self) {
var obj = {
money: (new Money(self.spedingArray[self.spedingArray.length - 2])).parse(),
note: self.spedingArray[self.spedingArray.length - 1]
}
return obj;
})(this);
};
Spending.prototype.get = function() {
return this.finalSpending;
};
var CSVparser = function(csvContent) {
this.__csvRowSplitter = "\r\r\n";
this.__csvColSplitter = ";";
this.rawContent = csvContent;
this.rows = [];
this.cols = [];
this.headlines = [];
this.rowsCount = 0;
this.colsCount = 0;
this.currentRow = [];
};
CSVparser.prototype.splitRows = function() {
this.rows = this.rawContent.split(this.__csvRowSplitter);
this.rowsCount = this.rows.length;
this.colsCount = this.rows[0].length;
this.headlines = this.rows[0];
var row, cols;
for(var i = 0; this.rows.length > i; i++) {
row = this.rows[i];
cols = row.split(this.__csvColSplitter);
if(cols.length > 1) {
this.cols[i] = cols;
}
}
return this;
};
CSVparser.prototype.getRows = function() {
return this.rows;
};
CSVparser.prototype.getCols = function() {
return this.cols;
};
CSVparser.prototype.splitCols = function() {
return this.currentRow.split(';');
};
CSVparser.prototype.getRow = function(number) {
return this.rows[(number || -1) + 1];
};
var SpendeeCSVParser = function(pFile) {
// public vars
this.notes = [];
this.money = [];
// private vars
var isValid = false;
//this.
var self = this;
this.callback = function(err, content) {
if(err) return console.error(new Error(content));
content = content.toString(); // validate it
var parsedCSV = new CSVparser(content);
parsedCSV.splitRows();
var cols = parsedCSV.getCols();
var self = this;
var spending;
for(var i = 0; cols.length > i; i++) {
spending = (new Spending(cols[i])).get();
this.notes[i] = spending.note;
this.money[i] = spending.money;
}
};
var __constructor = (function() { // IIFE constructor
this.file = pFile;
if(typeof this.file !== 'undefined' && fs.existsSync(this.file)) {
var contents = fs.readFileSync(this.file);
this.callback(false, contents);
}
}).call(this);
};
SpendeeCSVParser.prototype.filterMoney = function() {
var self = this;
var filter = Array.prototype.slice.call(arguments);
if(filter && filter.length === 1) filter = filter[0];
var applyFilter = function(filter) {
//console.log('Filter: ', filter);
var collection = [], money = 0;
for(var i = 0; self.notes.length > i; i++) {
if(self.notes[i].toLowerCase().indexOf(filter.toLowerCase()) !== -1) {
collection.push(self.notes[i]);
money = money + self.money[i];
}
}
//console.log(collection.length + ';');
// return collection;
return money;
};
if(Array.isArray(filter)) {
var coll = [];
var mon = 0;
filter.forEach(function(el, index, ar) {
// coll = coll.concat(applyFilter(el));
mon = mon + applyFilter(el);
});
//console.log('found: ', coll.length);
return mon;
return coll;
}
return applyFilter(filter);
};
var s = new SpendeeCSVParser('spendee-transactions-2014-06-20_01.51 pm.csv'); // your csv filename
console.log('I spent €', s.filterMoney('tinto', 'trinto', 'türk k', 'bäck', 'alte pizza')); // your notes serach filters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment