-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
regexp-matchall.js
59 lines (50 loc) · 2.12 KB
/
regexp-matchall.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict';
// var Construct = require('es-abstract/2023/Construct');
var CreateRegExpStringIterator = require('es-abstract/2024/CreateRegExpStringIterator');
var Get = require('es-abstract/2024/Get');
var Set = require('es-abstract/2024/Set');
var SpeciesConstructor = require('es-abstract/2024/SpeciesConstructor');
var ToLength = require('es-abstract/2024/ToLength');
var ToString = require('es-abstract/2024/ToString');
var Type = require('es-abstract/2024/Type');
var flagsGetter = require('regexp.prototype.flags');
var setFunctionName = require('set-function-name');
var callBound = require('call-bind/callBound');
var GetIntrinsic = require('get-intrinsic');
var $TypeError = require('es-errors/type');
var $indexOf = callBound('String.prototype.indexOf');
var OrigRegExp = GetIntrinsic('%RegExp%');
var supportsConstructingWithFlags = 'flags' in OrigRegExp.prototype;
var constructRegexWithFlags = function constructRegex(C, R) {
var matcher;
// workaround for older engines that lack RegExp.prototype.flags
var flags = 'flags' in R ? Get(R, 'flags') : ToString(flagsGetter(R));
if (supportsConstructingWithFlags && typeof flags === 'string') {
matcher = new C(R, flags);
} else if (C === OrigRegExp) {
// workaround for older engines that can not construct a RegExp with flags
matcher = new C(R.source, flags);
} else {
matcher = new C(R, flags);
}
return { flags: flags, matcher: matcher };
};
var regexMatchAll = setFunctionName(function SymbolMatchAll(string) {
var R = this;
if (Type(R) !== 'Object') {
throw new $TypeError('"this" value must be an Object');
}
var S = ToString(string);
var C = SpeciesConstructor(R, OrigRegExp);
var tmp = constructRegexWithFlags(C, R);
// var flags = ToString(Get(R, 'flags'));
var flags = tmp.flags;
// var matcher = Construct(C, [R, flags]);
var matcher = tmp.matcher;
var lastIndex = ToLength(Get(R, 'lastIndex'));
Set(matcher, 'lastIndex', lastIndex, true);
var global = $indexOf(flags, 'g') > -1;
var fullUnicode = $indexOf(flags, 'u') > -1;
return CreateRegExpStringIterator(matcher, S, global, fullUnicode);
}, '[Symbol.matchAll]', true);
module.exports = regexMatchAll;