-
Notifications
You must be signed in to change notification settings - Fork 3
/
myResultParser.js
51 lines (45 loc) · 2.65 KB
/
myResultParser.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
const fs = require('fs')
const transactions = JSON.parse(fs.readFileSync(process.argv[2]).toString())
const desiredTransactionDataByType = {
'dividend reinvested': { finalType: 'Investment Buy' },
'dividend': { finalType: 'Dividend' },
'recurring buy': { finalType: 'Investment Buy' },
'fractional buy': { finalType: 'Investment Buy' },
'market sell': { finalType: 'Investment Sell' },
'limit buy': { finalType: 'Investment Buy' },
'limit sell': { finalType: 'Investment Sell' },
'invested cash': { finalType: 'Investment Buy' },
'sold asset': { finalType: 'Investment Sell' }
}
const desiredTransactionDataByDescription = {
'earnings': { finalType: 'Other Money In' },
'interest': { finalType: 'Interest' }
}
const positiveFinalTypes = ['Investment Sell', 'Dividend']
const filteredParsedTransactions = []
const additionalIgnoreFilters = {
'Investment Buy': new Set(['CASH']),
'Investment Sell': new Set(['CASH'])
}
for (let i = 0; i < transactions.length; i++) {
const desiredTypeData = desiredTransactionDataByType[transactions[i].type.toLowerCase()] || desiredTransactionDataByDescription[transactions[i].description.toLowerCase()]
const swapDescriptionAndType = !!desiredTransactionDataByDescription[transactions[i].description.toLowerCase()]
if (desiredTypeData) {
if (desiredTypeData.alsoMatchDescription && desiredTypeData.alsoMatchDescription != transactions[i].description) {
continue
}
if (!!additionalIgnoreFilters[desiredTypeData.finalType] && additionalIgnoreFilters[desiredTypeData.finalType].has(transactions[i].description)) {
continue
}
filteredParsedTransactions.push({
date: transactions[i].date.slice(0,10),
description: !swapDescriptionAndType ? `${transactions[i].description} ${transactions[i].type}` : transactions[i].type,
amount: parseFloat(transactions[i].amount.replace('$', '').split(',').join('')) * (positiveFinalTypes.indexOf(desiredTypeData.finalType) >= 0 ? 1 : -1),
currency: RegExp(' [A-Z]+$').exec(transactions[i].amount)[0].trim(),
type: desiredTypeData.finalType || (!swapDescriptionAndType ? transactions[i].type : transactions[i].description)
})
}
}
for (let i = filteredParsedTransactions.length - 1; i >= 0; i--) {
console.log(`${filteredParsedTransactions[i].date}\t${filteredParsedTransactions[i].description}${filteredParsedTransactions[i].amount < 0 ? '\t\t' : '\t'}${Math.abs(filteredParsedTransactions[i].amount)}${filteredParsedTransactions[i].amount < 0 ? '\t' : '\t\t'}${filteredParsedTransactions[i].currency}\t${filteredParsedTransactions[i].type}`)
}