-
Notifications
You must be signed in to change notification settings - Fork 7
/
result_set.js
58 lines (53 loc) · 1.47 KB
/
result_set.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
var ResultSet = function(rows, schema) {
this.rows = rows;
this.schema = schema.fieldSchemas;
};
ResultSet.prototype.each = function(cb) {
for(var i in this.rows) {
var rowArray = this.rows[i].split("\t");
var row = {};
var headers = this.headers();
var schema = this.schema;
typecast = function(column, stringValue) {
var found = schema.filter(function(s) { return s.name === column });
var type = 'string';
if(found.length === 1) type = found[0].type;
if(type === 'double' || type === 'float' || type === 'int') return Number(stringValue);
return stringValue;
};
for(var a in rowArray) {
row[headers[a]] = typecast(headers[a], rowArray[a]);
}
cb(row);
};
};
ResultSet.prototype.toArray = function() {
var ret = [];
for(var i in this.rows) {
ret.push(this.rows[i].split("\t"));
};
return ret;
};
ResultSet.prototype.headers = function() {
var colCount = this.rows[0].split("\t").length;
var ret = [];
var partitionCount = 1;
var i = 0;
while(i < colCount) {
schemaName = this.schema[i] ? this.schema[i].name : ("_p" + partitionCount++);
ret.push(schemaName);
i++;
}
this.headers = function() { return ret; };
return ret;
};
ResultSet.prototype.toTSV = function(headers) {
var body = this.rows.join("\n");
if(headers) {
body = this.headers().join("\t") + "\n" + body;
}
return body;
};
exports.create = function(rows, schema) {
return (new ResultSet(rows, schema))
};