Skip to content

Commit 4839527

Browse files
authored
fix(functions): reset RegExp.lastIndex to zero when using cached RegExp objects (#2079)
1 parent dda11ef commit 4839527

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

packages/functions/src/__tests__/pattern.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ describe('Core Functions / Pattern', () => {
1818
expect(await runPattern('aBc', { match: '/[abc]+/im' })).toEqual([]);
1919
});
2020

21+
it('should return same results when given a global (g) marker (pattern cache usecase)', async () => {
22+
expect(await runPattern('abc', { match: '/[abc]+/gi' })).toEqual([]);
23+
expect(await runPattern('abc', { match: '/[abc]+/gi' })).toEqual([]);
24+
expect(await runPattern('abc', { match: '/[abc]+/gi' })).toEqual([]);
25+
});
26+
2127
it('given string regex containing invalid flags, should throw an exception', async () => {
2228
await expect(runPattern('aBc', { match: '/[abc]+/invalid' })).rejects.toThrow(
2329
"Invalid flags supplied to RegExp constructor 'invalid'",

packages/functions/src/pattern.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const cache = new Map<string, RegExp>();
2727
function getFromCache(pattern: string): RegExp {
2828
const existingPattern = cache.get(pattern);
2929
if (existingPattern !== void 0) {
30+
existingPattern.lastIndex = 0;
3031
return existingPattern;
3132
}
3233

0 commit comments

Comments
 (0)