-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_stream_method.js
59 lines (47 loc) · 1.92 KB
/
convert_stream_method.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
55
56
57
58
59
'use strict';
const fs = require('fs');
const path = require('path');
const stream = require('stream');
const readline = require('readline'); //native node module https://nodejs.org/docs/latest-v8.x/api/readline.html
const common = require('./common');
const convertCSVtoJSON = (csvFilepath, csvSeparator=',', pathBenchmarkOutputFile) => {
common.init();
csvSeparator = common.normalizeCSVSeparator(csvSeparator);
const fp = path.normalize(csvFilepath);
const resultJsonFilepath = common.buildJsonFilepath(fp);
let jsonResult = [];
let isMoreOneObject = false;
let headerFields = [];
const inStream = fs.createReadStream(fp);
const outStream = new stream;
const rl = readline.createInterface(inStream, outStream);
const streamResultJson = fs.createWriteStream(resultJsonFilepath);
rl.on('line', (line) => {
if (headerFields.length === 0) {
headerFields = common.retrieveHeaderFields(line, csvSeparator);
streamResultJson.write('[');
return;
}
if (line.length) {
if (isMoreOneObject) {
streamResultJson.write(',');
} else {
isMoreOneObject = true;
}
const objectResult = common.transformLineToObject(line,csvSeparator, headerFields);
streamResultJson.write(JSON.stringify(objectResult, null, 4));
}
});
rl.on('close', () => {
streamResultJson.write(']');
common.end(pathBenchmarkOutputFile);
});
};
let csvFilepath = (process.argv[2]) ? process.argv[2] : undefined;
let csvSeparator = (process.argv[3]) ? process.argv[3] : undefined;
let pathBenchmarkOutputFile = (process.argv[4]) ? process.argv[4] : undefined;
if (csvFilepath) {
convertCSVtoJSON(csvFilepath, csvSeparator, pathBenchmarkOutputFile);
} else {
console.info('a syntax is : $ node <script_name> <path of CSV file> <CSV separator>');
}