1 | 'use strict';
|
---|
2 |
|
---|
3 | var Call = require('es-abstract/2024/Call');
|
---|
4 | var Get = require('es-abstract/2024/Get');
|
---|
5 | var GetMethod = require('es-abstract/2024/GetMethod');
|
---|
6 | var IsRegExp = require('es-abstract/2024/IsRegExp');
|
---|
7 | var ToString = require('es-abstract/2024/ToString');
|
---|
8 | var RequireObjectCoercible = require('es-object-atoms/RequireObjectCoercible');
|
---|
9 | var callBound = require('call-bind/callBound');
|
---|
10 | var hasSymbols = require('has-symbols')();
|
---|
11 | var flagsGetter = require('regexp.prototype.flags');
|
---|
12 | var GetIntrinsic = require('get-intrinsic');
|
---|
13 | var $TypeError = require('es-errors/type');
|
---|
14 |
|
---|
15 | var $RegExp = GetIntrinsic('%RegExp%');
|
---|
16 | var $indexOf = callBound('String.prototype.indexOf');
|
---|
17 |
|
---|
18 | var regexpMatchAllPolyfill = require('./polyfill-regexp-matchall');
|
---|
19 |
|
---|
20 | var getMatcher = function getMatcher(regexp) { // eslint-disable-line consistent-return
|
---|
21 | var matcherPolyfill = regexpMatchAllPolyfill();
|
---|
22 | if (hasSymbols && typeof Symbol.matchAll === 'symbol') {
|
---|
23 | var matcher = GetMethod(regexp, Symbol.matchAll);
|
---|
24 | if (matcher === $RegExp.prototype[Symbol.matchAll] && matcher !== matcherPolyfill) {
|
---|
25 | return matcherPolyfill;
|
---|
26 | }
|
---|
27 | return matcher;
|
---|
28 | }
|
---|
29 | // fallback for pre-Symbol.matchAll environments
|
---|
30 | if (IsRegExp(regexp)) {
|
---|
31 | return matcherPolyfill;
|
---|
32 | }
|
---|
33 | };
|
---|
34 |
|
---|
35 | module.exports = function matchAll(regexp) {
|
---|
36 | var O = RequireObjectCoercible(this);
|
---|
37 |
|
---|
38 | if (typeof regexp !== 'undefined' && regexp !== null) {
|
---|
39 | var isRegExp = IsRegExp(regexp);
|
---|
40 | if (isRegExp) {
|
---|
41 | // workaround for older engines that lack RegExp.prototype.flags
|
---|
42 | var flags = 'flags' in regexp ? Get(regexp, 'flags') : flagsGetter(regexp);
|
---|
43 | RequireObjectCoercible(flags);
|
---|
44 | if ($indexOf(ToString(flags), 'g') < 0) {
|
---|
45 | throw new $TypeError('matchAll requires a global regular expression');
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | var matcher = getMatcher(regexp);
|
---|
50 | if (typeof matcher !== 'undefined') {
|
---|
51 | return Call(matcher, regexp, [O]);
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | var S = ToString(O);
|
---|
56 | // var rx = RegExpCreate(regexp, 'g');
|
---|
57 | var rx = new $RegExp(regexp, 'g');
|
---|
58 | return Call(getMatcher(rx), rx, [S]);
|
---|
59 | };
|
---|