Version 1
[yaffs-website] / node_modules / core-js / modules / es7.string.match-all.js
1 'use strict';
2 // https://tc39.github.io/String.prototype.matchAll/
3 var $export     = require('./_export')
4   , defined     = require('./_defined')
5   , toLength    = require('./_to-length')
6   , isRegExp    = require('./_is-regexp')
7   , getFlags    = require('./_flags')
8   , RegExpProto = RegExp.prototype;
9
10 var $RegExpStringIterator = function(regexp, string){
11   this._r = regexp;
12   this._s = string;
13 };
14
15 require('./_iter-create')($RegExpStringIterator, 'RegExp String', function next(){
16   var match = this._r.exec(this._s);
17   return {value: match, done: match === null};
18 });
19
20 $export($export.P, 'String', {
21   matchAll: function matchAll(regexp){
22     defined(this);
23     if(!isRegExp(regexp))throw TypeError(regexp + ' is not a regexp!');
24     var S     = String(this)
25       , flags = 'flags' in RegExpProto ? String(regexp.flags) : getFlags.call(regexp)
26       , rx    = new RegExp(regexp.source, ~flags.indexOf('g') ? flags : 'g' + flags);
27     rx.lastIndex = toLength(regexp.lastIndex);
28     return new $RegExpStringIterator(rx, S);
29   }
30 });