-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstringify.js
More file actions
70 lines (59 loc) · 1.53 KB
/
stringify.js
File metadata and controls
70 lines (59 loc) · 1.53 KB
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
var fs = require('fs')
, EventEmitter = require('events').EventEmitter
module.exports = function stringify(data) {
var emitter = new EventEmitter();
if (!isArffData(data)) {
return emitter.emit('error', new Error('data is not ArffData'));
}
var content = "";//TODO probably add some comments
content += "\n@RELATION " + data.name + "\n";
data.attributes.forEach(function (field) {
content += "\n@ATTRIBUTE " + field + " ";
var type = data.types[field].type;
if (type === 'nominal') {
content += "{" + data.types[field].oneof.join(',') + "}";
}
else if (type === 'date') {
content += "date " + data.types[field].format;
}
else {
content += type;
}
});
content += "\n\n@DATA\n";
data.data.forEach(function (row) {
var values = [];
data.attributes.forEach(function (field) {
var type = data.types[field].type;
if (type === 'nominal') {
values.push(data.types[field].oneof[row[field]]);
}
else {
values.push(row[field]);
}
});
content += "\n" + values.join(',');
});
process.nextTick(function() {
emitter.emit('stringified', content);
});
process.nextTick(function() {
emitter.emit('end');
});
return emitter;
}
function isArffData(data) {
if (!data.hasOwnProperty('name')) {
return false;
}
if (!data.hasOwnProperty('attributes')) {
return false;
}
if (!data.hasOwnProperty('types')) {
return false;
}
if (!data.hasOwnProperty('data')) {
return false;
}
return true
}