-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (60 loc) · 2.18 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const fs = require('fs');
function merge_csvs(inputFiles, options) {
//setting up default options.
let { delimiter = ',', wrapper = '"', newlineSeperator = '\r\n', outputPath = 'output.csv' } = options;
let readStreams = [];
inputFiles.forEach(filePath => {
let readStream = fs.createReadStream(`${filePath}`, 'utf-8');
readStreams.push(readStream);
});
let writeStream = fs.createWriteStream(outputPath, 'utf-8');
writeStream.on('finish', () => {
console.log(`File merged and written successfully at ${outputPath}`);
});
let headers = [];
processStream(0);
function processStream(index = 0) {
let isFirstChunk = true;
if (index >= readStreams.length) {
writeStream.end();
return;
}
let readStream = readStreams[index];
console.log(`Processing ${index + 1} : ${readStream.path}`);
readStream.on('end', () => {
index = index + 1;
processStream(index);
});
readStream.on('data', (chunk) => {
//split data on new line.
let lines = chunk.split(newlineSeperator);
let firstLine = null;
if (isFirstChunk) {
isFirstChunk = false;
firstLine = lines.shift();
}
//generate headers.
if (!headers.length) {
headers = processLine(firstLine, delimiter, wrapper);
writeStream.write(headers.join(`${delimiter}`));
}
writeStream.write(`${newlineSeperator}`);
writeStream.write(lines.join(`${newlineSeperator}`));
});
}
function processLine(line, delimiter = ',', wrapper = '"') {
let values = []; let str = ''; let wrapStart = false;
for (let i = 0; i < line.length; i++) {
if (line[i] == wrapper) {
wrapStart = !wrapStart;
}
else if (!wrapStart && line[i] == delimiter) {
values.push(str);
str = '';
} else str += line[i];
}
values.push(str); //last item.
return values;
}
}
module.exports = merge_csvs;