File size: 833 Bytes
dc89ab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export function parseLine(l: string) {
  const res = l.match(/((?:\s+"[^"]+")|(?:[^,"]+))/g) || [];
  for (let i = 0; i < res.length; i++) {
    res[i] = res[i].replace(/^\s+/, '').replace(/^"/, '').replace(/[\r\n"]+$/, '')
  }
  return res;
}

export function parse(t: string, cb: (o: object) => void) {
  const lines = t.split('\n');
  const header = parseLine(lines[0]);
  for (let i = 1; i < lines.length; i++) {
    if (!lines[i].length) {
      continue;
    }
    const l = parseLine(lines[i]) || []

    if (l.length < header.length) {
      console.error(`couldn't parse '${lines[i]}' yielded '${l}' of length ${l.length} expected ${header.length}: ${header}`);
      return null;
    }
    const e = { [header[0]]: l[0] };
    for (let j = 1; j < header.length; j++) {
      e[`${header[j]}`] = l[j];
    }
    cb(e)
  }
}