@@ -6,7 +6,7 @@ class TextExtraction {
6
6
* @param {RegExp } patterns[].pattern - RegExp to be used for parsing
7
7
*/
8
8
constructor ( text , patterns ) {
9
- this . text = text ;
9
+ this . text = text ;
10
10
this . patterns = patterns || [ ] ;
11
11
}
12
12
@@ -15,7 +15,7 @@ class TextExtraction {
15
15
* @return {Object[] } - props for all the parts of the text
16
16
*/
17
17
parse ( ) {
18
- let parsedTexts = [ { children : this . text } ] ;
18
+ let parsedTexts = [ { children : this . text } ] ;
19
19
this . patterns . forEach ( ( pattern ) => {
20
20
let newParts = [ ] ;
21
21
@@ -27,27 +27,37 @@ class TextExtraction {
27
27
return ;
28
28
}
29
29
30
- let parts = [ ] ;
30
+ let parts = [ ] ;
31
31
let textLeft = parsedText . children ;
32
32
let indexOfMatchedString = 0 ;
33
33
34
- while ( textLeft ) {
35
- let matches = pattern . pattern . exec ( textLeft ) ;
36
-
37
- if ( ! matches ) { break ; }
38
-
34
+ /** @type {RegExpExecArray } */
35
+ let matches ;
36
+ // Global RegExps are stateful, this makes it start at 0 if reused
37
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec
38
+ pattern . pattern . lastIndex = 0 ;
39
+ while ( textLeft && ( matches = pattern . pattern . exec ( textLeft ) ) ) {
39
40
let previousText = textLeft . substr ( 0 , matches . index ) ;
40
- indexOfMatchedString + = matches . index ;
41
+ indexOfMatchedString = matches . index ;
41
42
42
- parts . push ( { children : previousText } ) ;
43
+ parts . push ( { children : previousText } ) ;
43
44
44
- parts . push ( this . getMatchedPart ( pattern , matches [ 0 ] , matches , indexOfMatchedString ) ) ;
45
+ parts . push (
46
+ this . getMatchedPart (
47
+ pattern ,
48
+ matches [ 0 ] ,
49
+ matches ,
50
+ indexOfMatchedString ,
51
+ ) ,
52
+ ) ;
45
53
46
54
textLeft = textLeft . substr ( matches . index + matches [ 0 ] . length ) ;
47
55
indexOfMatchedString += matches [ 0 ] . length ;
56
+ // Global RegExps are stateful, this makes it operate on the "remainder" of the string
57
+ pattern . pattern . lastIndex = indexOfMatchedString ;
48
58
}
49
59
50
- parts . push ( { children : textLeft } ) ;
60
+ parts . push ( { children : textLeft } ) ;
51
61
52
62
newParts . push ( ...parts ) ;
53
63
} ) ;
@@ -56,9 +66,9 @@ class TextExtraction {
56
66
} ) ;
57
67
58
68
// Remove _matched key.
59
- parsedTexts . forEach ( ( parsedText ) => delete ( parsedText . _matched ) ) ;
69
+ parsedTexts . forEach ( ( parsedText ) => delete parsedText . _matched ) ;
60
70
61
- return parsedTexts . filter ( t => ! ! t . children ) ;
71
+ return parsedTexts . filter ( ( t ) => ! ! t . children ) ;
62
72
}
63
73
64
74
// private
@@ -75,7 +85,9 @@ class TextExtraction {
75
85
let props = { } ;
76
86
77
87
Object . keys ( matchedPattern ) . forEach ( ( key ) => {
78
- if ( key === 'pattern' || key === 'renderText' ) { return ; }
88
+ if ( key === 'pattern' || key === 'renderText' ) {
89
+ return ;
90
+ }
79
91
80
92
if ( typeof matchedPattern [ key ] === 'function' ) {
81
93
props [ key ] = ( ) => matchedPattern [ key ] ( text , index ) ;
@@ -85,7 +97,10 @@ class TextExtraction {
85
97
} ) ;
86
98
87
99
let children = text ;
88
- if ( matchedPattern . renderText && typeof matchedPattern . renderText === 'function' ) {
100
+ if (
101
+ matchedPattern . renderText &&
102
+ typeof matchedPattern . renderText === 'function'
103
+ ) {
89
104
children = matchedPattern . renderText ( text , matches ) ;
90
105
}
91
106
0 commit comments