Skip to content

Commit

Permalink
fix(functions): reset RegExp.lastIndex to zero when using cached RegE…
Browse files Browse the repository at this point in the history
…xp objects (#2079)
  • Loading branch information
pavelkornev committed Mar 7, 2022
1 parent dda11ef commit 4839527
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/functions/src/__tests__/pattern.test.ts
Expand Up @@ -18,6 +18,12 @@ describe('Core Functions / Pattern', () => {
expect(await runPattern('aBc', { match: '/[abc]+/im' })).toEqual([]);
});

it('should return same results when given a global (g) marker (pattern cache usecase)', async () => {
expect(await runPattern('abc', { match: '/[abc]+/gi' })).toEqual([]);
expect(await runPattern('abc', { match: '/[abc]+/gi' })).toEqual([]);
expect(await runPattern('abc', { match: '/[abc]+/gi' })).toEqual([]);
});

it('given string regex containing invalid flags, should throw an exception', async () => {
await expect(runPattern('aBc', { match: '/[abc]+/invalid' })).rejects.toThrow(
"Invalid flags supplied to RegExp constructor 'invalid'",
Expand Down
1 change: 1 addition & 0 deletions packages/functions/src/pattern.ts
Expand Up @@ -27,6 +27,7 @@ const cache = new Map<string, RegExp>();
function getFromCache(pattern: string): RegExp {
const existingPattern = cache.get(pattern);
if (existingPattern !== void 0) {
existingPattern.lastIndex = 0;
return existingPattern;
}

Expand Down

0 comments on commit 4839527

Please sign in to comment.