source: imaps-frontend/node_modules/es-abstract/2023/CreateRegExpStringIterator.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

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