-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (62 loc) · 2.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const fs = require('fs-extra');
const parse = require('csv-parse');
const async = require('async');
const ejs = require('ejs');
const opn = require('opn');
const path = require('path');
const inputFile = 'n26.csv';
const lines = [];
const processLine = (line) => {
lines.push(line);
return Promise.resolve();
};
const parser = parse({delimiter: ','}, (err, data) => {
async.eachSeries(data, (line, callback) => {
processLine(line).then(() => {
callback();
});
}, () => {
const rows = lines
.filter(line => !line[1].includes('Main Account'))
.filter(line => line[0] !== 'Date')
.reduce((acc, line) => {
const date = line[0].substring(0, 10);
const amount = +line[6];
if (acc.some(m => m.name === date)) {
const mo = acc.find(m => m.name === date);
mo.sum += amount
} else {
acc.push({
name: date,
sum: amount
})
}
return acc;
}, []);
const sum = `Money difference between start end date: ${rows.reduce((acc, curr) => curr.sum ? acc + curr.sum : acc, 0)}`;
const rowsAbs = rows.reduce((acc, row, idx) => {
if (idx === 0) {
acc.push(row);
} else {
acc.push({
...row,
sum: acc[idx - 1].sum + row.sum
});
}
return acc;
}, []);
const dates = rowsAbs.map(l => l.name);
const sums = rowsAbs.map(l => l.sum);
ejs.renderFile('./templates/index.html.dist', {dates, sums, sum}, null, (err, html) => {
if (err) {
console.error(err);
throw new Error(err);
}
fs.outputFileSync('./public/index.html', html);
opn('./public/index.html');
const indexRoute = path.resolve('./public/index.html');
console.log(`A browser tab should open with your graph. If not, please open directly file://${indexRoute} .`);
});
})
});
fs.createReadStream(inputFile).pipe(parser);