-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopen-csv-input-stream.js
39 lines (29 loc) · 1.3 KB
/
open-csv-input-stream.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
"use strict";
const stream = require('stream');
const fs = require('fs');
const papaparse = require('papaparse');
//
// Open a streaming CSV file for input.
//
function openCsvInputStream (inputFilePath) {
const csvInputStream = new stream.Readable({ objectMode: true }); // Create a stream that we can read data records from, note that 'object mode' is enabled.
csvInputStream._read = () => {}; // Must include, otherwise we get an error.
const fileInputStream = fs.createReadStream(inputFilePath); // Create stream for reading the input file.
papaparse.parse(fileInputStream, {
header: true,
dynamicTyping: true,
// We may not need this, but don't want to get halfway through the massive file before realising it is needed.
skipEmptyLines: true,
step: (results) => { // Handles incoming rows of CSV data.
csvInputStream.push(results.data); // Push results as they are streamed from the file.
},
complete: () => { // File read operation has completed.
csvInputStream.push(null); // Signify end of stream.
},
error: (err) => { // An error has occurred.
csvInputStream.emit("error", err); // Pass on errors.
}
});
return csvInputStream;
};
module.exports = openCsvInputStream;