-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert.js
45 lines (39 loc) · 1.18 KB
/
convert.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
var FS = require('fs');
var through = require('through');
var ES = require('event-stream');
var newLineChar = "\n";
(function(){
var
readFileName = process.argv[2],
writeFileName = readFileName.substr(0, readFileName.lastIndexOf(".")) + ".tsv",
columns = process.argv[3].split(','),
rstream = FS.createReadStream(readFileName),
//Using the append mode to prevent the headers getting flushed
wstream = FS.createWriteStream(writeFileName, {flags : 'a'});
//Write the headers
FS.writeFileSync(writeFileName, columns.join('\t') + newLineChar);
rstream.pipe(
ES.split()
).pipe(through(
function write(data) {
try {
if(!data)
return;
dataJson = JSON.parse(data.toString());
dataStr = "";
columns.forEach(function(col){
//add tab, replace the new line and tab characters with spaces and encode the string with double quotes
dataStr += (dataJson[col] ? dataJson[col].toString().replace(/[\r\n\t]+/g, " ") : " ") + "\t";
});
dataStr += newLineChar;
} catch(e) {
console.error(e);
return;
}
this.queue(dataStr);
}, function end() {
this.queue(null);
console.log(writeFileName + " created!");
}
)).pipe(wstream);
}());