-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataTable.js
More file actions
112 lines (83 loc) · 1.92 KB
/
DataTable.js
File metadata and controls
112 lines (83 loc) · 1.92 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
class DataType {
constructor() {
//this.TYPE_OBJECT = "Object";
this.TYPE_NUMBER = "Number";
this.TYPE_STRING = "String";
}
get_type (string) {
if (!isNaN(string))
return this.TYPE_NUMBER;
else
return this.TYPE_STRING;
}
}
class DataColumn {
constructor(name,type) {
this.name = name;
this.type = type;
}
}
dt = new DataType;
class DataTable {
constructor() {
this.name = "";
this.cols = [];
this.rows = [];
this.json = "";
}
import_csv(csv_string, rowsset_name){
this.name = rowsset_name
let line = csv_string.split('\n');
let self = this;
this.load_cols(line[0],line[1],()=>{
self.load_rows(csv_string,()=>{
console.log(self.rows);
});
});
}
import_json (json_arr, rowsset_name, callback = ()=>{} ){
this.json = json_arr;
this.cols = Object.keys(json_arr[0]);
for (let i = 0; i < json_arr.length; i++ ) {
if ( Object.keys(json_arr[i]).length != this.cols.length ) {
}else{
this.rows[i] = Object.values(json_arr[i]);
}
if (i >= json_arr.length - 1) { callback(); }
}
}
load_cols (name_csv_line,rows_csv_line,callback) {
let names = name_csv_line.split(',');
let rowss = rows_csv_line.split(',');
if (names.length != rowss.length )
return;
for (let i = 0; i < names.length; i++ ) {
let name = names[i];
let rows = rowss[i];
let type = dt.get_type(rows);
this.cols[i] = new DataColumn(name,type);
if ( i == names.length - 1)
callback();
}
}
load_rows(csv_string,callback){
if (csv_string[csv_string.length-1] != '\n') {
csv_string += '\n';
}
let s = csv_string.split('\n');
let num_of_row = s.length - 1;
for (let i = 1; i < num_of_row; i++ ) {
this.add_row(i-1,s[i]);
if ( i == num_of_row -1 )
callback();
}
}
add_row(index,row_csv){
let s = row_csv.split(',');
if ( s.length != this.cols.length ) {
console.log("row size not equal");
}else{
this.rows[index] = s;
}
}
}