-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifo.js
55 lines (52 loc) · 1.15 KB
/
lifo.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
// Calculate cost basis in LIFO manner.
const fs = require('fs');
const csv = require('csv')
const Line = require('./line.js');
const argv = require('yargs')
.option('input', {
demandOption: true,
describe: 'The path of the input file'
})
.option('ignore_later_than', {
describe: 'ISO date string. Ignore transactions later than this date.'
})
.coerce('ignore_later_than', (arg) => new Date(arg))
.help()
.strict()
.argv
lines = []
fs.createReadStream(argv.input)
.pipe(csv.parse())
.on('data', (row) => {
if (row === null) {
return;
}
let l = new Line(...row);
if (l.date > argv.ignore_later_than) {
return;
}
l.mergeFee()
if (lines.length == 0) {
lines.push(l);
return;
}
let last = lines[lines.length-1];
if (l.date < last.date) {
throw 'going backwards!'
}
if (!last.canMerge(l)) {
lines.push(l);
return;
}
while (l.volume.gte(last.volume)) {
l.merge(last);
lines.pop();
last = lines[lines.length-1]
}
last.merge(l);
})
.on('end', () => {
lines.forEach(l => {
console.log(l.toString());
});
})