-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.js
52 lines (52 loc) · 1.3 KB
/
parser.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
module.exports.parse = (text, headcount) => {
//console.log(typeof text)
if (!text) throw new Error("Empty response from API")
let arr = text.toString().trim().split('\n');
//console.log(arr);
let c = 0;
if (!headcount) {
headcount = 10;
}
let head = undefined;
let i = 0;
let hi = 0;
for (i = 0; i < arr.length; i++) {
arr[i] = arr[i].split('\t');
arr[i] = arr[i].filter(val => {
if (val !== '')
return val;
})
//console.log(arr[i]);
if (arr[i][0] === 'H') {
if (c < headcount) {
head = arr[i];
hi = i;
} else {
arr.splice(i);
break;
}
c++;
}
if (arr[i][0] == '$') {
arr.splice(i, 1)
}
}
if (head == undefined) {
throw new Error("Player not found");
}
arr = arr.slice(hi + 1);
//console.log(arr);
for (let i = 0; i < arr.length; i++) {
arr[i].shift();
}
head.shift();
if (arr.includes(head)) {
throw new Error("Player not found");
}
//console.log(head);
//console.log(arr);
return {
'arr': arr,
'head': head
};
}