source: imaps-frontend/node_modules/es-abstract/2024/CreateRegExpStringIterator.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.4 KB
Line 
1'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4var hasSymbols = require('has-symbols')();
5
6var $TypeError = require('es-errors/type');
7var IteratorPrototype = GetIntrinsic('%IteratorPrototype%', true);
8
9var AdvanceStringIndex = require('./AdvanceStringIndex');
10var CreateIterResultObject = require('./CreateIterResultObject');
11var DefineMethodProperty = require('./DefineMethodProperty');
12var Get = require('./Get');
13var OrdinaryObjectCreate = require('./OrdinaryObjectCreate');
14var RegExpExec = require('./RegExpExec');
15var Set = require('./Set');
16var ToLength = require('./ToLength');
17var ToString = require('./ToString');
18var Type = require('./Type');
19
20var SLOT = require('internal-slot');
21var setToStringTag = require('es-set-tostringtag');
22
23var 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
40if (IteratorPrototype) {
41 RegExpStringIterator.prototype = OrdinaryObjectCreate(IteratorPrototype);
42}
43
44var 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};
83DefineMethodProperty(RegExpStringIterator.prototype, 'next', RegExpStringIteratorNext, false);
84
85if (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
98module.exports = function CreateRegExpStringIterator(R, S, global, fullUnicode) {
99 // assert R.global === global && R.unicode === fullUnicode?
100 return new RegExpStringIterator(R, S, global, fullUnicode);
101};
Note: See TracBrowser for help on using the repository browser.