1 | 'use strict';
|
---|
2 | /* eslint-disable es/no-string-prototype-matchall -- safe */
|
---|
3 | var $ = require('../internals/export');
|
---|
4 | var createIteratorConstructor = require('../internals/create-iterator-constructor');
|
---|
5 | var requireObjectCoercible = require('../internals/require-object-coercible');
|
---|
6 | var toLength = require('../internals/to-length');
|
---|
7 | var toString = require('../internals/to-string');
|
---|
8 | var aFunction = require('../internals/a-function');
|
---|
9 | var anObject = require('../internals/an-object');
|
---|
10 | var classof = require('../internals/classof-raw');
|
---|
11 | var isRegExp = require('../internals/is-regexp');
|
---|
12 | var getRegExpFlags = require('../internals/regexp-flags');
|
---|
13 | var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
|
---|
14 | var fails = require('../internals/fails');
|
---|
15 | var wellKnownSymbol = require('../internals/well-known-symbol');
|
---|
16 | var speciesConstructor = require('../internals/species-constructor');
|
---|
17 | var advanceStringIndex = require('../internals/advance-string-index');
|
---|
18 | var InternalStateModule = require('../internals/internal-state');
|
---|
19 | var IS_PURE = require('../internals/is-pure');
|
---|
20 |
|
---|
21 | var MATCH_ALL = wellKnownSymbol('matchAll');
|
---|
22 | var REGEXP_STRING = 'RegExp String';
|
---|
23 | var REGEXP_STRING_ITERATOR = REGEXP_STRING + ' Iterator';
|
---|
24 | var setInternalState = InternalStateModule.set;
|
---|
25 | var getInternalState = InternalStateModule.getterFor(REGEXP_STRING_ITERATOR);
|
---|
26 | var RegExpPrototype = RegExp.prototype;
|
---|
27 | var regExpBuiltinExec = RegExpPrototype.exec;
|
---|
28 | var nativeMatchAll = ''.matchAll;
|
---|
29 |
|
---|
30 | var WORKS_WITH_NON_GLOBAL_REGEX = !!nativeMatchAll && !fails(function () {
|
---|
31 | 'a'.matchAll(/./);
|
---|
32 | });
|
---|
33 |
|
---|
34 | var regExpExec = function (R, S) {
|
---|
35 | var exec = R.exec;
|
---|
36 | var result;
|
---|
37 | if (typeof exec == 'function') {
|
---|
38 | result = exec.call(R, S);
|
---|
39 | if (typeof result != 'object') throw TypeError('Incorrect exec result');
|
---|
40 | return result;
|
---|
41 | } return regExpBuiltinExec.call(R, S);
|
---|
42 | };
|
---|
43 |
|
---|
44 | // eslint-disable-next-line max-len -- ignore
|
---|
45 | var $RegExpStringIterator = createIteratorConstructor(function RegExpStringIterator(regexp, string, global, fullUnicode) {
|
---|
46 | setInternalState(this, {
|
---|
47 | type: REGEXP_STRING_ITERATOR,
|
---|
48 | regexp: regexp,
|
---|
49 | string: string,
|
---|
50 | global: global,
|
---|
51 | unicode: fullUnicode,
|
---|
52 | done: false
|
---|
53 | });
|
---|
54 | }, REGEXP_STRING, function next() {
|
---|
55 | var state = getInternalState(this);
|
---|
56 | if (state.done) return { value: undefined, done: true };
|
---|
57 | var R = state.regexp;
|
---|
58 | var S = state.string;
|
---|
59 | var match = regExpExec(R, S);
|
---|
60 | if (match === null) return { value: undefined, done: state.done = true };
|
---|
61 | if (state.global) {
|
---|
62 | if (toString(match[0]) === '') R.lastIndex = advanceStringIndex(S, toLength(R.lastIndex), state.unicode);
|
---|
63 | return { value: match, done: false };
|
---|
64 | }
|
---|
65 | state.done = true;
|
---|
66 | return { value: match, done: false };
|
---|
67 | });
|
---|
68 |
|
---|
69 | var $matchAll = function (string) {
|
---|
70 | var R = anObject(this);
|
---|
71 | var S = toString(string);
|
---|
72 | var C, flagsValue, flags, matcher, global, fullUnicode;
|
---|
73 | C = speciesConstructor(R, RegExp);
|
---|
74 | flagsValue = R.flags;
|
---|
75 | if (flagsValue === undefined && R instanceof RegExp && !('flags' in RegExpPrototype)) {
|
---|
76 | flagsValue = getRegExpFlags.call(R);
|
---|
77 | }
|
---|
78 | flags = flagsValue === undefined ? '' : toString(flagsValue);
|
---|
79 | matcher = new C(C === RegExp ? R.source : R, flags);
|
---|
80 | global = !!~flags.indexOf('g');
|
---|
81 | fullUnicode = !!~flags.indexOf('u');
|
---|
82 | matcher.lastIndex = toLength(R.lastIndex);
|
---|
83 | return new $RegExpStringIterator(matcher, S, global, fullUnicode);
|
---|
84 | };
|
---|
85 |
|
---|
86 | // `String.prototype.matchAll` method
|
---|
87 | // https://tc39.es/ecma262/#sec-string.prototype.matchall
|
---|
88 | $({ target: 'String', proto: true, forced: WORKS_WITH_NON_GLOBAL_REGEX }, {
|
---|
89 | matchAll: function matchAll(regexp) {
|
---|
90 | var O = requireObjectCoercible(this);
|
---|
91 | var flags, S, matcher, rx;
|
---|
92 | if (regexp != null) {
|
---|
93 | if (isRegExp(regexp)) {
|
---|
94 | flags = toString(requireObjectCoercible('flags' in RegExpPrototype
|
---|
95 | ? regexp.flags
|
---|
96 | : getRegExpFlags.call(regexp)
|
---|
97 | ));
|
---|
98 | if (!~flags.indexOf('g')) throw TypeError('`.matchAll` does not allow non-global regexes');
|
---|
99 | }
|
---|
100 | if (WORKS_WITH_NON_GLOBAL_REGEX) return nativeMatchAll.apply(O, arguments);
|
---|
101 | matcher = regexp[MATCH_ALL];
|
---|
102 | if (matcher === undefined && IS_PURE && classof(regexp) == 'RegExp') matcher = $matchAll;
|
---|
103 | if (matcher != null) return aFunction(matcher).call(regexp, O);
|
---|
104 | } else if (WORKS_WITH_NON_GLOBAL_REGEX) return nativeMatchAll.apply(O, arguments);
|
---|
105 | S = toString(O);
|
---|
106 | rx = new RegExp(regexp, 'g');
|
---|
107 | return IS_PURE ? $matchAll.call(rx, S) : rx[MATCH_ALL](S);
|
---|
108 | }
|
---|
109 | });
|
---|
110 |
|
---|
111 | IS_PURE || MATCH_ALL in RegExpPrototype || createNonEnumerableProperty(RegExpPrototype, MATCH_ALL, $matchAll);
|
---|