From 2305ee3958ffc80adb69cc935fbc6ff130ea6296 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Tue, 28 Aug 2018 20:11:46 +0700 Subject: [PATCH] update `String#matchAll` per https://github.com/tc39/proposal-string-matchall/pull/38 --- CHANGELOG.md | 2 +- .../modules/esnext.string.match-all.js | 43 +++++++++---------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bb9c211f8ef..c2edda68bdc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,7 +55,7 @@ - Remove mongolian vowel separator (U+180E) from the list of whitespaces for methods like `String#trim` (ES6 -> ES7) - Update [`Observable`](https://github.com/tc39/proposal-observable) (#257, #276, etc.) - Update `Array#flatten` -> `Array#flat` and `Array#flatMap` and move to the stage 3 - - Update `String#matchAll` (mainly [this PR](https://github.com/tc39/proposal-string-matchall/pull/17)) and move to the stage 3 + - Update `String#matchAll` ([proposal-string-matchall#17](https://github.com/tc39/proposal-string-matchall/pull/17), [proposal-string-matchall#38](https://github.com/tc39/proposal-string-matchall/pull/38), etc.) and move to the stage 3 - Update `.name` properties of `String#{trimStart, trimEnd , trimLeft, trimRight}`, move to the stage 3 - Mark ES2016, ES2017 and ES2018 features as stable: - `Array#includes` and `%TypedArray%#includes` diff --git a/packages/core-js/modules/esnext.string.match-all.js b/packages/core-js/modules/esnext.string.match-all.js index 102ed7e3c235..b50089af629e 100644 --- a/packages/core-js/modules/esnext.string.match-all.js +++ b/packages/core-js/modules/esnext.string.match-all.js @@ -4,7 +4,7 @@ var requireObjectCoercible = require('../internals/require-object-coercible'); var toLength = require('../internals/to-length'); var aFunction = require('../internals/a-function'); var anObject = require('../internals/an-object'); -var isRegExp = require('../internals/is-regexp'); +var classof = require('../internals/classof'); var getFlags = require('../internals/regexp-flags'); var hide = require('../internals/hide'); var speciesConstructor = require('../internals/species-constructor'); @@ -19,24 +19,6 @@ var getInternalState = InternalStateModule.getterFor(REGEXP_STRING_ITERATOR); var RegExpPrototype = RegExp.prototype; var regExpBuiltinExec = RegExpPrototype.exec; -var matchAllIterator = function (R, O) { - var S = String(O); - var C, matcher, global, fullUnicode; - if (isRegExp(R)) { - C = speciesConstructor(R, RegExp); - matcher = new C(C === RegExp ? R.source : R, 'flags' in RegExpPrototype ? String(R.flags) : getFlags.call(R)); - global = !!matcher.global; - fullUnicode = !!matcher.unicode; - matcher.lastIndex = toLength(R.lastIndex); - } else { - matcher = new RegExp(R, 'g'); - global = true; - fullUnicode = false; - if (matcher.lastIndex !== 0) throw TypeError('Incorrect lastIndex!'); - } - return new $RegExpStringIterator(matcher, S, global, fullUnicode); -}; - var advanceStringIndex = function (S, index, unicode) { return index + (unicode ? at(S, index).length : 1); }; @@ -76,18 +58,33 @@ var $RegExpStringIterator = createIteratorConstructor(function RegExpStringItera return { value: match, done: false }; }); +var $matchAll = function (string) { + var R = anObject(this); + var S = String(string); + var C, flags, matcher, global, fullUnicode; + C = speciesConstructor(R, RegExp); + flags = 'flags' in RegExpPrototype ? String(R.flags) : getFlags.call(R); + matcher = new C(C === RegExp ? R.source : R, flags); + global = !!~flags.indexOf('g'); + fullUnicode = !!~flags.indexOf('u'); + matcher.lastIndex = toLength(R.lastIndex); + return new $RegExpStringIterator(matcher, S, global, fullUnicode); +}; + // `String.prototype.matchAll` method // https://github.com/tc39/proposal-string-matchall require('../internals/export')({ target: 'String', proto: true }, { matchAll: function matchAll(regexp) { var O = requireObjectCoercible(this); + var S; if (regexp != null) { var matcher = regexp[MATCH_ALL]; + if (matcher === undefined && IS_PURE && classof(regexp) == 'RegExp') matcher = $matchAll; if (matcher != null) return aFunction(matcher).call(regexp, O); - } return matchAllIterator(regexp, O); + } + S = String(O); + return new $RegExpStringIterator(new RegExp(regexp, 'g'), S, true, false); } }); -IS_PURE || MATCH_ALL in RegExpPrototype || hide(RegExpPrototype, MATCH_ALL, function (string) { - return matchAllIterator(anObject(this), string); -}); +IS_PURE || MATCH_ALL in RegExpPrototype || hide(RegExpPrototype, MATCH_ALL, $matchAll);