-
Notifications
You must be signed in to change notification settings - Fork 1
/
csvConverter.js
executable file
·40 lines (32 loc) · 1.05 KB
/
csvConverter.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
#!/usr/bin/env node
var fs = require('fs');
var os = require("os");
var moment = require('moment');
var inputFile = process.argv[2] ? process.argv[2] : 'speeds.csv';
var outputFile = process.argv[3] ? process.argv[3] : 'output-speeds.csv';
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream(inputFile)
});
try {
fs.unlinkSync(outputFile);
fs.writeFileSync(outputFile, 'Date,Download (Mbit/s),Upload (Mbit/s)' + os.EOL);
} catch(e) {}
lineReader.on('line', function (line) {
let csv = line.split(';');
csv[0] = '"' + convertDate(csv[0]) + '"';
csv[1] = '"' + convertFloatDelimitr(csv[1]) + '"';
csv[2] = '"' + convertFloatDelimitr(csv[2]) + '"';
csv[3] = os.EOL;
console.log(csv[0]);
fs.appendFileSync(outputFile, csv.join(','));
});
function convertDate(timestamp) {
return moment.unix(timestamp).format("DD/MM/YYYY HH:mm:ss");
}
/**
* @param {string} stringNumber
* @return {*}
*/
function convertFloatDelimitr (stringNumber) {
return stringNumber.replace('.', ',');
}