diff --git a/package.json b/package.json index c773691..4562ec9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-pdf-extractor", - "version": "0.1.2", + "version": "0.1.3", "description": "This library allows you to extract pdfs file data using matches specifics patterns.", "main": "lib/commonjs/index.js", "module": "lib/module/index.js", diff --git a/src/Match.ts b/src/Match.ts index 1eaa72d..d5bedc3 100644 --- a/src/Match.ts +++ b/src/Match.ts @@ -1,5 +1,6 @@ const reducer = (pattern: RegExp) => (matches: string[], line: string) => { - const iterator = line.matchAll(pattern); + const regexp = pattern.global ? pattern : new RegExp(pattern, 'g'); + const iterator = line.matchAll(regexp); let stop = false; do { diff --git a/src/__tests__/Match.test.tsx b/src/__tests__/Match.test.tsx index 8253a11..0c5b05d 100644 --- a/src/__tests__/Match.test.tsx +++ b/src/__tests__/Match.test.tsx @@ -19,6 +19,22 @@ describe('Match', () => { expect(matches[1]).toBe('mail@provider2.com'); }); + it('Should convert regex to global and return matches when it exists', async () => { + const data = [ + 'this is a line with an email: mail@provider.com', + 'this e-mail mail@provider2.com is on 2nd line and is duplicated: mail@provider3.com', + ]; + + const matches = await Match(/(\S+@\w+\.\w+)/, data); + + expect(Array.isArray(matches)).toBe(true); + expect(matches).toStrictEqual([ + 'mail@provider.com', + 'mail@provider2.com', + 'mail@provider3.com', + ]); + }); + it('Should remove duplicated matches and return when it exists', async () => { const data = [ 'this is a line with an email: mail@provider.com',