-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (45 loc) · 1.19 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
var fs = require('fs');
var arff = require('node-arff');
var csvStr = require('csv-stringify');
// read params
var fileDest = null;
var dest = process.argv.indexOf('-d');
if (dest !== -1) {
fileDest = process.argv[dest + 1];
}
var normalize = (process.argv.indexOf('-n') !== -1);
var headless = (process.argv.indexOf('-h') === -1);
var arffFilePath = process.argv.find(arg => /.+\.arff$/i.test(arg));
// read arff file
arff.load(arffFilePath, function(err, arrFile) {
if (err) {
return console.error(err);
}
// -n param specified
if (normalize) {
arrFile.normalize();
}
let fileName = arrFile.name;
let attrs = arrFile.attributes;
let csvData = arrFile.data.map(data => attrs.map(attrName => data[attrName]));
// default output csv file
if (fileDest === null) {
fileDest = process.cwd() + "/" + fileName + ".csv";
}
// -h param specified
if (headless === false) {
csvData = [attrs].concat(csvData);
}
// write csv
csvStr(csvData, function(err, output) {
if(err) {
return console.log(err);
}
fs.writeFile(fileDest, output, function(err) {
if(err) {
return console.log(err);
}
console.log('created file', fileDest);
});
});
})