Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(functions): reset RegExp.lastIndex to zero when using cached RegExp objects #2079

Merged
merged 1 commit into from Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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