1 | 'use strict';
|
---|
2 |
|
---|
3 | var GetIntrinsic = require('get-intrinsic');
|
---|
4 | var hasSymbols = require('has-symbols')();
|
---|
5 |
|
---|
6 | var $TypeError = require('es-errors/type');
|
---|
7 | var IteratorPrototype = GetIntrinsic('%IteratorPrototype%', true);
|
---|
8 |
|
---|
9 | var AdvanceStringIndex = require('./AdvanceStringIndex');
|
---|
10 | var CreateIterResultObject = require('./CreateIterResultObject');
|
---|
11 | var DefineMethodProperty = require('./DefineMethodProperty');
|
---|
12 | var Get = require('./Get');
|
---|
13 | var OrdinaryObjectCreate = require('./OrdinaryObjectCreate');
|
---|
14 | var RegExpExec = require('./RegExpExec');
|
---|
15 | var Set = require('./Set');
|
---|
16 | var ToLength = require('./ToLength');
|
---|
17 | var ToString = require('./ToString');
|
---|
18 | var Type = require('./Type');
|
---|
19 |
|
---|
20 | var SLOT = require('internal-slot');
|
---|
21 | var setToStringTag = require('es-set-tostringtag');
|
---|
22 |
|
---|
23 | var RegExpStringIterator = function RegExpStringIterator(R, S, global, fullUnicode) {
|
---|
24 | if (typeof S !== 'string') {
|
---|
25 | throw new $TypeError('`S` must be a string');
|
---|
26 | }
|
---|
27 | if (typeof global !== 'boolean') {
|
---|
28 | throw new $TypeError('`global` must be a boolean');
|
---|
29 | }
|
---|
30 | if (typeof fullUnicode !== 'boolean') {
|
---|
31 | throw new $TypeError('`fullUnicode` must be a boolean');
|
---|
32 | }
|
---|
33 | SLOT.set(this, '[[IteratingRegExp]]', R);
|
---|
34 | SLOT.set(this, '[[IteratedString]]', S);
|
---|
35 | SLOT.set(this, '[[Global]]', global);
|
---|
36 | SLOT.set(this, '[[Unicode]]', fullUnicode);
|
---|
37 | SLOT.set(this, '[[Done]]', false);
|
---|
38 | };
|
---|
39 |
|
---|
40 | if (IteratorPrototype) {
|
---|
41 | RegExpStringIterator.prototype = OrdinaryObjectCreate(IteratorPrototype);
|
---|
42 | }
|
---|
43 |
|
---|
44 | var RegExpStringIteratorNext = function next() {
|
---|
45 | var O = this; // eslint-disable-line no-invalid-this
|
---|
46 | if (Type(O) !== 'Object') {
|
---|
47 | throw new $TypeError('receiver must be an object');
|
---|
48 | }
|
---|
49 | if (
|
---|
50 | !(O instanceof RegExpStringIterator)
|
---|
51 | || !SLOT.has(O, '[[IteratingRegExp]]')
|
---|
52 | || !SLOT.has(O, '[[IteratedString]]')
|
---|
53 | || !SLOT.has(O, '[[Global]]')
|
---|
54 | || !SLOT.has(O, '[[Unicode]]')
|
---|
55 | || !SLOT.has(O, '[[Done]]')
|
---|
56 | ) {
|
---|
57 | throw new $TypeError('"this" value must be a RegExpStringIterator instance');
|
---|
58 | }
|
---|
59 | if (SLOT.get(O, '[[Done]]')) {
|
---|
60 | return CreateIterResultObject(undefined, true);
|
---|
61 | }
|
---|
62 | var R = SLOT.get(O, '[[IteratingRegExp]]');
|
---|
63 | var S = SLOT.get(O, '[[IteratedString]]');
|
---|
64 | var global = SLOT.get(O, '[[Global]]');
|
---|
65 | var fullUnicode = SLOT.get(O, '[[Unicode]]');
|
---|
66 | var match = RegExpExec(R, S);
|
---|
67 | if (match === null) {
|
---|
68 | SLOT.set(O, '[[Done]]', true);
|
---|
69 | return CreateIterResultObject(undefined, true);
|
---|
70 | }
|
---|
71 | if (global) {
|
---|
72 | var matchStr = ToString(Get(match, '0'));
|
---|
73 | if (matchStr === '') {
|
---|
74 | var thisIndex = ToLength(Get(R, 'lastIndex'));
|
---|
75 | var nextIndex = AdvanceStringIndex(S, thisIndex, fullUnicode);
|
---|
76 | Set(R, 'lastIndex', nextIndex, true);
|
---|
77 | }
|
---|
78 | return CreateIterResultObject(match, false);
|
---|
79 | }
|
---|
80 | SLOT.set(O, '[[Done]]', true);
|
---|
81 | return CreateIterResultObject(match, false);
|
---|
82 | };
|
---|
83 | DefineMethodProperty(RegExpStringIterator.prototype, 'next', RegExpStringIteratorNext, false);
|
---|
84 |
|
---|
85 | if (hasSymbols) {
|
---|
86 | setToStringTag(RegExpStringIterator.prototype, 'RegExp String Iterator');
|
---|
87 |
|
---|
88 | if (Symbol.iterator && typeof RegExpStringIterator.prototype[Symbol.iterator] !== 'function') {
|
---|
89 | var iteratorFn = function SymbolIterator() {
|
---|
90 | return this;
|
---|
91 | };
|
---|
92 | DefineMethodProperty(RegExpStringIterator.prototype, Symbol.iterator, iteratorFn, false);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | // https://262.ecma-international.org/16.0/#sec-createregexpstringiterator
|
---|
97 |
|
---|
98 | module.exports = function CreateRegExpStringIterator(R, S, global, fullUnicode) {
|
---|
99 | // assert R.global === global && R.unicode === fullUnicode?
|
---|
100 | return new RegExpStringIterator(R, S, global, fullUnicode);
|
---|
101 | };
|
---|