-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.js
65 lines (59 loc) · 1.27 KB
/
trace.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
59
60
61
62
63
64
65
/**
Created by Complynx on 22.03.2019,
http://complynx.net
<[email protected]> Daniel Drizhuk
*/
let traceEntryRe = [
{ // Firefox 30+
re:/^([^@]+)?@(.+):([0-9]+):([0-9]+)$/,
groups: {
func:1,
file:2,
line:3,
column:4
}
},
{ // Chrome
re:/^\s*at((\s+(\S+))?\s+\()?(.*):([0-9]+):([0-9]+)\)?$/,
groups: {
func:3,
file:4,
line:5,
column:6
}
}
];
/**
* trace string parser
* @param {string} trace
* @returns {[*]} parsed trace
*/
export function parseTrace(trace){
trace = trace.split('\n');
if(trace[0] === 'Error'){ // Chrome
trace.shift();
}
if(trace[trace.length - 1] === ''){ // Firefox
trace.pop();
}
let re;
for(re of traceEntryRe) if(re.re.test(trace[0])) break;
return trace.map(e=>{
let r = re.re.exec(e);
if(!r) return e;
let ret = {};
for(let i in re.groups)
ret[i] = r[re.groups[i]];
return ret;
});
}
/**
* returns current callstack
* @returns {[*]}
*/
export function getTrace(){
let e = new Error();
let trace = parseTrace(e.stack);
trace.shift(); // Remove self
return trace;
}