@@ -6,63 +6,64 @@ const CLASS_METHOD_REGEXP = /(.+)\.([^.]+)$/;
66
77const EXCLUDED = [ "<anonymous>" ] ;
88
9- const parse = ( stackTrace : string ) : Trace [ ] => {
10- const lines = stackTrace . split ( "\n" ) ;
11- const traces : Trace [ ] = [ ] ;
9+ export class StacktraceParser {
10+ constructor ( ) { }
1211
13- for ( const line of lines ) {
14- const stackTraceLine = parseStackTraceLine ( line ) ;
15- if ( stackTraceLine && stackTraceLine ?. filename ) {
16- traces . push ( stackTraceLine ) ;
17- }
18- }
12+ public static parse ( stackTrace : string ) : Trace [ ] {
13+ const lines = stackTrace . split ( "\n" ) ;
14+ const traces : Trace [ ] = [ ] ;
1915
20- return traces ;
21- } ;
16+ for ( const line of lines ) {
17+ const stackTraceLine = this . parseStackTraceLine ( line ) ;
18+ if ( stackTraceLine && stackTraceLine ?. filename ) {
19+ traces . push ( stackTraceLine ) ;
20+ }
21+ }
2222
23- const parseStackTraceLine = ( line : string ) : Trace | null => {
24- const match = line . match ( LINE_REGEXP ) ;
25- if ( ! match || EXCLUDED . includes ( match [ 1 ] ) ) {
26- return null ;
23+ return traces ;
2724 }
2825
29- const [ , method , file , lineStr , columnStr ] = match ;
30- const lineNo = parseOptionalInt ( lineStr ) ;
31- const columnNo = parseOptionalInt ( columnStr ) ;
32- const ext = getFileExtension ( file ) ;
33- const methodName = getFullMethodName ( method ) ;
26+ private static parseStackTraceLine = ( line : string ) : Trace | null => {
27+ const match = line . match ( LINE_REGEXP ) ;
28+ if ( ! match || EXCLUDED . includes ( match [ 1 ] ) ) {
29+ return null ;
30+ }
31+
32+ const [ , method , file , lineStr , columnStr ] = match ;
33+ const lineNo = this . parseOptionalInt ( lineStr ) ;
34+ const columnNo = this . parseOptionalInt ( columnStr ) ;
35+ const ext = this . getFileExtension ( file ) ;
36+ const methodName = this . getFullMethodName ( method ) ;
3437
35- return {
36- filename : file ,
37- lineNo,
38- columnNo,
39- function : methodName ,
40- extension : ext
38+ return {
39+ filename : file ,
40+ lineNo,
41+ columnNo,
42+ function : methodName ,
43+ extension : ext
44+ } ;
4145 } ;
42- } ;
4346
44- const parseOptionalInt = ( str ?: string ) : number | null => {
45- return str ? parseInt ( str , 10 ) : null ;
46- } ;
47+ private static parseOptionalInt = ( str ?: string ) : number | null => {
48+ return str ? parseInt ( str , 10 ) : null ;
49+ } ;
4750
48- const getFileExtension = ( file ?: string ) : string | null => {
49- if ( ! file ) {
50- return null ;
51- }
51+ private static getFileExtension = ( file ?: string ) : string | null => {
52+ if ( ! file ) {
53+ return null ;
54+ }
5255
53- const match = file . match ( EXTENSION_REGEXP ) ;
54- return match ? match [ 1 ] : null ;
55- } ;
56+ const match = file . match ( EXTENSION_REGEXP ) ;
57+ return match ? match [ 1 ] : null ;
58+ } ;
5659
57- const getFullMethodName = ( method ?: string ) : string | null => {
58- if ( ! method ) {
59- return null ;
60- }
60+ private static getFullMethodName = ( method ?: string ) : string | null => {
61+ if ( ! method ) {
62+ return null ;
63+ }
6164
62- const match = method . match ( CLASS_METHOD_REGEXP ) ;
63- return match ? `${ match [ 1 ] } .${ match [ 2 ] } ` : method ;
64- } ;
65+ const match = method . match ( CLASS_METHOD_REGEXP ) ;
66+ return match ? `${ match [ 1 ] } .${ match [ 2 ] } ` : method ;
67+ } ;
6568
66- export const stacktrace = {
67- parse
68- } ;
69+ }
0 commit comments