Skip to content

Commit

Permalink
Fix random matches are missed + add test
Browse files Browse the repository at this point in the history
Alternative implementation to #47
  • Loading branch information
fbartho committed Jun 11, 2020
1 parent 979f888 commit e8f140e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/TextExtraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class TextExtraction {
);

textLeft = textLeft.substr(matches.index + matches[0].length);
indexOfMatchedString += matches[0].length;
indexOfMatchedString += matches[0].length - 1;

This comment has been minimized.

Copy link
@sh-helen

sh-helen Jun 11, 2020

Hi, seems this doesn't fix the problem

This comment has been minimized.

Copy link
@sh-helen

sh-helen Jun 11, 2020

I've checked it with my example left in the comment here #72 (comment)

// Global RegExps are stateful, this makes it operate on the "remainder" of the string
pattern.pattern.lastIndex = indexOfMatchedString;
}
Expand Down
28 changes: 28 additions & 0 deletions test/TextExtraction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,34 @@ describe('TextExtraction', () => {
expect(textExtraction.parse()).toEqual([{ children: 'abcdef' }]);
});

it('still works even if the RegExp has a previously-used pattern', () => {
const r = /c/g;
r.lastIndex = 2;
const textExtraction = new TextExtraction('cc something c something', [
{ pattern: r, renderText: () => 'Found!' },
]);

expect(textExtraction.parse()).toMatchInlineSnapshot(`
Array [
Object {
"children": "Found!",
},
Object {
"children": "Found!",
},
Object {
"children": " something ",
},
Object {
"children": "Found!",
},
Object {
"children": " something",
},
]
`);
});

it('returns an array with text parts if there is matches', () => {
const textExtraction = new TextExtraction(
'hello my website is http://foo.bar, bar is good.',
Expand Down

0 comments on commit e8f140e

Please sign in to comment.