Skip to content

Commit

Permalink
update String#matchAll per tc39/proposal-string-matchall#38
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Aug 28, 2018
1 parent d2576af commit 2305ee3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
43 changes: 20 additions & 23 deletions packages/core-js/modules/esnext.string.match-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
};
Expand Down Expand Up @@ -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);

0 comments on commit 2305ee3

Please sign in to comment.