-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor fixes and improvements for csv2yaml and transactions scripts
- Loading branch information
Showing
13 changed files
with
227 additions
and
57 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const fse = require('fs-extra') | ||
const yaml = require('js-yaml') | ||
const csvnorm = require('csvnorm') | ||
const converter = require('converter') | ||
|
||
const { | ||
rmEmptyString, | ||
keysToEnglish, | ||
noteToAccount, | ||
sanitizeYaml, | ||
} = require('../helpers.js') | ||
|
||
|
||
function normalizeAndPrint (filePathTemp) { | ||
const csv2json = converter({ | ||
from: 'csv', | ||
to: 'json', | ||
}) | ||
|
||
let jsonTemp = '' | ||
csv2json.on('data', chunk => { | ||
jsonTemp += chunk | ||
}) | ||
csv2json.on('end', () => { | ||
const transactions = JSON | ||
.parse(jsonTemp) | ||
.map(keysToEnglish) | ||
.map(transaction => { | ||
const note = transaction.note | ||
.replace(/<br\s+\/>/g, '\n') | ||
const amount = transaction.amount + ' €' | ||
const sortedTransaction = { | ||
utc: transaction['value-utc'] < transaction['entry-utc'] | ||
? transaction['value-utc'] | ||
: transaction['entry-utc'], | ||
} | ||
|
||
if (transaction['value-utc'] !== sortedTransaction.utc) { | ||
sortedTransaction['value-utc'] = transaction['value-utc'] | ||
} | ||
if (transaction['entry-utc'] !== sortedTransaction.utc) { | ||
sortedTransaction['entry-utc'] = transaction['entry-utc'] | ||
} | ||
|
||
sortedTransaction.type = transaction.type | ||
sortedTransaction.note = note | ||
|
||
const transfersObj = transaction.amount.startsWith('-') | ||
? { | ||
transfers: [{ | ||
from: 'dkb:visa', | ||
to: noteToAccount(note), | ||
amount: amount.slice(1), | ||
'original-amount': transaction['original-amount'], | ||
}], | ||
} | ||
: { | ||
transfers: [{ | ||
from: noteToAccount(note), | ||
to: 'dkb:visa', | ||
// TODO: Remove when github.com/adius/csvnorm/issues/1 is solved | ||
amount: transaction.amount === '0,00' ? '0 €' : amount, | ||
'original-amount': transaction['original-amount'], | ||
}], | ||
} | ||
const newTransaction = Object.assign(sortedTransaction, transfersObj) | ||
|
||
delete newTransaction.amount | ||
|
||
return JSON.parse(JSON.stringify(newTransaction, rmEmptyString)) | ||
}) | ||
.sort((transA, transB) => | ||
// Oldest first | ||
String(transA.utc) | ||
.localeCompare(String(transB.utc), 'en'), | ||
) | ||
|
||
const yamlString = sanitizeYaml(yaml.dump({transactions})) | ||
|
||
console.info(yamlString) | ||
}) | ||
|
||
csvnorm.default({ | ||
encoding: 'latin1', | ||
readableStream: fse.createReadStream(filePathTemp), | ||
skipLinesStart: 6, | ||
writableStream: csv2json, | ||
}) | ||
} | ||
|
||
normalizeAndPrint(process.argv[2]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const fse = require('fs-extra') | ||
const yaml = require('js-yaml') | ||
const csvnorm = require('csvnorm') | ||
const converter = require('converter') | ||
|
||
const { | ||
rmEmptyString, | ||
keysToEnglish, | ||
noteToAccount, | ||
sanitizeYaml, | ||
} = require('../helpers.js') | ||
|
||
|
||
function normalizeAndPrint (filePathTemp) { | ||
const csv2json = converter({ | ||
from: 'csv', | ||
to: 'json', | ||
}) | ||
|
||
let jsonTemp = '' | ||
csv2json.on('data', chunk => { | ||
jsonTemp += chunk | ||
}) | ||
csv2json.on('end', () => { | ||
const transactions = JSON | ||
.parse(jsonTemp) | ||
.map(keysToEnglish) | ||
.reverse() // Now sorted ascending by value date | ||
.map(transaction => { | ||
const currency = ' €' | ||
const sortedTransaction = { | ||
utc: transaction.date, | ||
note: transaction.note + '\n' + transaction.note2, | ||
} | ||
const account = noteToAccount(transaction.note) || '_todo_' | ||
const transfersObj = transaction.amount.startsWith('-') | ||
? { | ||
transfers: [{ | ||
from: 'fidor:giro', | ||
to: account, | ||
amount: transaction.amount.slice(1) + currency, | ||
}], | ||
} | ||
: { | ||
transfers: [{ | ||
from: account, | ||
to: 'fidor:giro', | ||
// TODO: Remove when https://github.com/adius/csvnorm/issues/1 | ||
// is solved | ||
amount: transaction.amount === '0,00' | ||
? 0 | ||
: transaction.amount + currency, | ||
}], | ||
} | ||
const newTransaction = Object.assign(sortedTransaction, transfersObj) | ||
|
||
delete newTransaction.amount | ||
|
||
return JSON.parse(JSON.stringify(newTransaction, rmEmptyString)) | ||
}) | ||
|
||
const yamlString = sanitizeYaml(yaml.dump({transactions})) | ||
|
||
console.info(yamlString) | ||
}) | ||
|
||
csvnorm.default({ | ||
encoding: 'utf-8', | ||
readableStream: fse.createReadStream(filePathTemp), | ||
writableStream: csv2json, | ||
}) | ||
} | ||
|
||
normalizeAndPrint(process.argv[2]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.