-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseTXTs.js
45 lines (39 loc) · 1.07 KB
/
parseTXTs.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
'use strict'
// Parse TXT and export a JSON file
let fs = require('fs'),
stream = fs.createWriteStream('compiled.json'),
windows1252 = require('windows-1252')
for (let filename of fs.readdirSync('txts')) {
let data = windows1252.decode(fs.readFileSync('txts/' + filename, 'binary')),
match = filename.match(/^(\d{4})-(\d\d).txt$/),
ano = Number(match[1]),
mes = Number(match[2]),
lines = data.split(/\r?\n/)
console.log('Got %d lines for %s', lines.length, filename)
for (let line of lines) {
if (!line) {
continue
}
let fields = line.split(';').map(e => e.trim())
stream.write(JSON.stringify({
ano,
mes,
registro: fields[0],
nome: fields[1],
codigoArea: fields[2],
area: fields[3],
areas: Object.assign({}, fields[3].split('/')),
cargo: fields[4],
referencia: fields[5],
bruto: readMoney(fields[6]),
indenizacoes: readMoney(fields[7]),
redutor: readMoney(fields[8]),
descontos: readMoney(fields[9]),
liquido: readMoney(fields[10])
}) + '\n')
}
}
stream.end()
function readMoney(str) {
return Number(str.replace(',', '.'))
}