-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseCSV.js
54 lines (50 loc) · 1.51 KB
/
parseCSV.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
const fs = require('fs')
const parse = require('csv-parse')
const async = require('async')
const textParser = require('./textParser')
const transform = require('./transform')
const keywords = {
absender: 'ABSENDER: ',
auftraggeber: 'AUFTRAGGEBER: ',
mitteilungen: 'MITTEILUNGEN: ',
referenznr: 'REFERENZ-NR: ',
postomat: 'POSTOMAT: ',
uebrige: 'UEBRIGE: ',
zahlungsempfaenger: 'ZAHLUNGSEMPFÄNGER: ',
idnr: 'ID-NR. DES ZAHLUNGSEMPFÄNGERS: ',
von: 'GUTSCHRIFT VON ',
}
const splitLine = ([date, text, plus, minus, valuta, saldo], type) =>
({ date, plus, minus, valuta, saldo, type, text, ...transform(text, keywords) })
const parser = parse({delimiter: ';', from: 6, relax: true, relax_column_count: true }, function (err, data) {
if(err) {
console.error(err)
} else {
async.eachSeries(data, function (line, next) {
next()
})
}
})
module.exports = function (inputFile, options, cb) {
let transactions = []
let stats = {}
const p = fs.createReadStream(inputFile, options).pipe(parser)
p.on('data', chunk => {
if(chunk) {
let transaction = null
const line = splitLine(chunk)
const text = line.text
const first = text && text.split(' ')[0]
if (first) {
// count how many types there are
stats[first] = Object.keys(stats).includes(first) ? stats[first] + 1 : 1
// enrich transaction data with extracted text
transaction = textParser[first](line, first)
if(transaction) { transactions.push(transaction) }
}
}
})
p.on('end', () => {
cb(transactions, stats)
})
}