forked from maeyler/JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.js
36 lines (36 loc) · 965 Bytes
/
Utilities.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
"use strict";
class TabularData {
constructor(sample, name='TabularData') {
this.name = name
this.proto = Object.getPrototypeOf(sample)
this.keys = Object.getOwnPropertyNames(sample)
console.log(this.name, this)
this.data = []
}
readData(url, callback) {
let k = this.keys
let toArray = (t) => {
for (let s of t.split('\n')) {
if (!s) break //end of loop
let b = s.split('\t'); //TAB
let n = Math.min(k.length, b.length)
let x = {}
for (let i=0; i<n; i++)
x[k[i]] = b[i]
if (n < b.length) { //remainder
let r = []
for (let i=n-1; i<b.length; i++)
r.push(b[i])
x[k[n-1]] = r //last key
}
Object.setPrototypeOf(x, this.proto)
this.data.push(x)
}
if (callback) callback(t)
}
fetch(url).then(x => x.text()).then(toArray)
}
toString() {
return this.keys.join(', ')
}
}