-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbf.js
43 lines (31 loc) · 1.04 KB
/
dbf.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
const path = require("path");
if (!(process.argv[2] && process.argv[2].match(/\.dbf$/i))) {
console.error(`${path.basename(process.argv[2])} takes an input file (*.dbf)`);
process.exit(1);
}
/**
* Replacement for the node-dbf executable, as the packaged one does not properly escape quotes
*/
const fs = require("fs"),
csv = require("fast-csv"),
Parser = require("node-dbf");
let infile = process.argv[2],
outfile = infile.replace(/\.dbf$/i, ".csv"),
stream = csv.createWriteStream({headers: true});
let timeStart = process.hrtime(), fields, parser = new Parser(infile);
stream.pipe(fs.createWriteStream(outfile));
parser.on("header", function(header) {
fields = header.fields.map(function(f) { return f.name; });
stream.write(fields);
});
parser.on("record", function(record) {
let len = fields.length, data = new Array(len);
for (let i = 0; i < len; ++i) {
data[i] = (record[fields[i]]);
}
stream.write(data);
});
parser.on("end", function() {
console.error(`Completed file conversion`);
});
parser.parse();