Skip to content

Commit 224379e

Browse files
Polyfill string.matchAll to unblock #7770 (#7776)
Manually write a polyfill for string.matchAll to unblock #7770. This should be replaced with core-js in the future.
1 parent 6ab5271 commit 224379e

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @license
3+
* Copyright 2023 Google LLC.
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
* =============================================================================
16+
*/
17+
18+
// TODO(mattSoulanille): Replace this with automatic polyfilling using core-js.
19+
export function *matchAll(str: string, regexp: RegExp): IterableIterator<RegExpMatchArray> {
20+
// Remove the global flag since str.match does not work with it.
21+
const flags = new Set(regexp.flags.split(''));
22+
flags.delete('g');
23+
regexp = new RegExp(regexp, [...flags].join(''));
24+
25+
let match = str.match(regexp);
26+
let offset = 0;
27+
let restOfStr = str;
28+
while (match != null) {
29+
if (match.index == null) {
30+
console.error(match);
31+
throw new Error(`Matched string '${match[0]}' has no index`);
32+
}
33+
34+
// Remove up to and including the first match from the input string
35+
// so the next loop can find the next match.
36+
const matchEnd = match.index + match[0].length;
37+
restOfStr = restOfStr.slice(matchEnd);
38+
39+
// Adjust the match to look like a result from matchAll.
40+
match.index += offset;
41+
match.input = str;
42+
43+
offset += matchEnd;
44+
yield match;
45+
match = restOfStr.match(regexp);
46+
}
47+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license
3+
* Copyright 2023 Google LLC.
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
* =============================================================================
16+
*/
17+
18+
import {matchAll} from './match_all_polyfill';
19+
20+
function format({match, index, input}: {match: string, index: number,
21+
input: string}) {
22+
const res: RegExpMatchArray = [match];
23+
res.index = index;
24+
res.input = input;
25+
return jasmine.objectContaining(res);
26+
}
27+
28+
describe('matchAll', () => {
29+
it('finds all matches of a regexp on a string', () => {
30+
const input = 'asdfasdfasdfasdf';
31+
expect([...matchAll(input , /asd/g)]).toEqual([
32+
{match: 'asd', index: 0, input},
33+
{match: 'asd', index: 4, input},
34+
{match: 'asd', index: 8, input},
35+
{match: 'asd', index: 12, input},
36+
].map(format));
37+
});
38+
39+
it('supports regexp flags', () => {
40+
const input = 'asdfASDFasdfASDF';
41+
// Case sensitive
42+
expect([...matchAll(input, /asd/g)]).toEqual([
43+
{match: 'asd', index: 0, input},
44+
{match: 'asd', index: 8, input},
45+
].map(format));
46+
47+
// case insensitive
48+
expect([...matchAll('asdfASDFasdfASDF', /asd/gi)]).toEqual([
49+
{match: 'asd', index: 0, input},
50+
{match: 'ASD', index: 4, input},
51+
{match: 'asd', index: 8, input},
52+
{match: 'ASD', index: 12, input},
53+
].map(format));
54+
});
55+
});

0 commit comments

Comments
 (0)