Skip to content

Commit

Permalink
Merge pull request #516 from zloirock/string-match-all-stage-4
Browse files Browse the repository at this point in the history
Move `String#matchAll` to stable ES, expose `Symbol.matchAll`
  • Loading branch information
zloirock authored May 11, 2019
2 parents 3030456 + df3e6e2 commit 1f8b914
Show file tree
Hide file tree
Showing 36 changed files with 200 additions and 138 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Changelog
##### 3.1.0
- `String#matchAll` moved to stable ES, exposed `Symbol.matchAll`, [babel/proposals#57 (comment)](https://github.com/babel/proposals/issues/57#issuecomment-476783115)
- `Promise.allSettled` moved to stage 3, [babel/proposals#57 (comment)](https://github.com/babel/proposals/issues/57#issuecomment-476837985)
- `Promise.any` moved to stage 1, [babel/proposals#57 (comment)](https://github.com/babel/proposals/issues/57#issuecomment-477335963)
- Removed `es.regexp.flags` dependency from `es.regexp.to-string`, [#536](https://github.com/zloirock/core-js/issues/536), [#537](https://github.com/zloirock/core-js/issues/537)
Expand Down
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ Array(1).includes(undefined); // => true
```

#### ECMAScript: String and RegExp
The main part of `String` features: modules [`es.string.from-code-point`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.from-code-point.js), [`es.string.raw`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.raw.js), [`es.string.iterator`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.iterator.js), [`es.string.split`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.split.js), [`es.string.code-point-at`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.code-point-at.js), [`es.string.ends-with`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.ends-with.js), [`es.string.includes`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.includes.js), [`es.string.repeat`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.repeat.js), [`es.string.pad-start`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.pad-start.js), [`es.string.pad-end`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.pad-end.js), [`es.string.starts-with`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.starts-with.js), [`es.string.trim`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.trim.js), [`es.string.trim-start`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.trim-start.js), [`es.string.trim-end`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.trim-end.js).
The main part of `String` features: modules [`es.string.from-code-point`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.from-code-point.js), [`es.string.raw`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.raw.js), [`es.string.iterator`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.iterator.js), [`es.string.split`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.split.js), [`es.string.code-point-at`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.code-point-at.js), [`es.string.ends-with`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.ends-with.js), [`es.string.includes`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.includes.js), [`es.string.repeat`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.repeat.js), [`es.string.pad-start`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.pad-start.js), [`es.string.pad-end`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.pad-end.js), [`es.string.starts-with`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.starts-with.js), [`es.string.trim`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.trim.js), [`es.string.trim-start`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.trim-start.js), [`es.string.trim-end`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.trim-end.js), [`es.string.match-all`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.match-all.js).

Adding support of well-known [symbols](#ecmascript-symbol) `@@match`, `@@replace`, `@@search` and `@@split` and direct `.exec` calls to related `String` methods, modules [`es.string.match`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.match.js), [`es.string.replace`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.replace.js), [`es.string.search`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.search.js) and [`es.string.split`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.string.split.js).

Expand All @@ -636,6 +636,7 @@ class String {
padEnd(length: number, fillStr?: string = ' '): string;
codePointAt(pos: number): number | void;
match(template: any): any; // ES2015+ fix for support @@match
matchAll(regexp: RegExp): Iterator;
replace(template: any, replacer: any): any; // ES2015+ fix for support @@replace
search(template: any): any; // ES2015+ fix for support @@search
split(template: any, limit: any): any; // ES2015+ fix for support @@split, some fixes for old engines
Expand Down Expand Up @@ -676,13 +677,18 @@ class RegExp {
core-js(-pure)/es|stable|features/string
core-js(-pure)/es|stable|features/string/from-code-point
core-js(-pure)/es|stable|features/string/raw
core-js(-pure)/es|stable|features/string/code-point-at
core-js(-pure)/es|stable|features/string/ends-with
core-js(-pure)/es|stable|features/string/includes
core-js(-pure)/es|stable|features/string/starts-with
core-js(-pure)/es|stable|features/string/ends-with
core-js/es|stable|features/string/match
core-js(-pure)/es|stable|features/string/match-all
core-js(-pure)/es|stable|features/string/repeat
core-js(-pure)/es|stable|features/string/pad-start
core-js(-pure)/es|stable|features/string/pad-end
core-js(-pure)/es|stable|features/string/code-point-at
core-js/es|stable|features/string/replace
core-js/es|stable|features/string/search
core-js/es|stable|features/string/split
core-js(-pure)/es|stable|features/string/trim
core-js(-pure)/es|stable|features/string/trim-start
core-js(-pure)/es|stable|features/string/trim-end
Expand All @@ -705,6 +711,7 @@ core-js(-pure)/es|stable|features/string/iterator
core-js(-pure)/es|stable|features/string/virtual/includes
core-js(-pure)/es|stable|features/string/virtual/starts-with
core-js(-pure)/es|stable|features/string/virtual/ends-with
core-js(-pure)/es|stable|features/string/virtual/match-all
core-js(-pure)/es|stable|features/string/virtual/repeat
core-js(-pure)/es|stable|features/string/virtual/pad-start
core-js(-pure)/es|stable|features/string/virtual/pad-end
Expand Down Expand Up @@ -732,12 +739,8 @@ core-js/es|stable|features/regexp
core-js/es|stable|features/regexp/constructor
core-js(-pure)/es|stable|features/regexp/flags
core-js/es|stable|features/regexp/to-string
core-js/es|stable|features/regexp/match
core-js/es|stable|features/regexp/replace
core-js/es|stable|features/regexp/search
core-js/es|stable|features/regexp/split
```
[*Examples*](https://goo.gl/DR1Q72):
[*Examples*](https://goo.gl/E6e7s6):
```js
for (let value of 'a𠮷b') {
console.log(value); // => 'a', '𠮷', 'b'
Expand Down Expand Up @@ -784,6 +787,10 @@ RegExp.prototype.toString.call({ source: 'foo', flags: 'bar' }); // => '/foo/bar
' hello '.trimRight(); // => ' hello'
' hello '.trimStart(); // => 'hello '
' hello '.trimEnd(); // => ' hello'

for (let [_, d, D] of '1111a2b3cccc'.matchAll(/(\d)(\D)/g)) {
console.log(d, D); // => 1 a, 2 b, 3 c
}
```
#### ECMAScript: Number
Module [`es.number.constructor`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/es.number.constructor.js). `Number` constructor support binary and octal literals, [*example*](http://goo.gl/jRd6b3):
Expand Down Expand Up @@ -1563,18 +1570,13 @@ instance.c; // => 42
`core-js/stage/4` entry point contains only stage 4 proposals, `core-js/stage/3` - stage 3 and stage 4, etc.
#### Stage 4 proposals

Stage 4 proposals already marked in `core-js` as stable ECMAScript, they will be removed from proposals namespace in the next major `core-js` version.
[*CommonJS entry points:*](#commonjs-api)
```js
core-js(-pure)/stage/4
```
None.
* `String#matchAll` [proposal](https://github.com/tc39/proposal-string-matchall) - module [`esnext.string.match-all`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/esnext.string.match-all.js).

#### Stage 3 proposals
[*CommonJS entry points:*](#commonjs-api)
```js
core-js(-pure)/stage/3
```
* `String#matchAll` [proposal](https://github.com/tc39/proposal-string-matchall) - module [`esnext.string.match-all`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/esnext.string.match-all.js)
```js
class String {
matchAll(regexp: RegExp): Iterator;
Expand All @@ -1583,14 +1585,12 @@ class String {
[*CommonJS entry points:*](#commonjs-api)
```js
core-js/proposals/string-match-all
core-js(-pure)/features/string/match-all
core-js(-pure)/features/string/virtual/match-all
```
[*Examples*](https://goo.gl/AxSqHT):

#### Stage 3 proposals
[*CommonJS entry points:*](#commonjs-api)
```js
for (let [_, d, D] of '1111a2b3cccc'.matchAll(/(\d)(\D)/g)) {
console.log(d, D); // => 1 a, 2 b, 3 c
}
core-js(-pure)/stage/3
```
* `globalThis` [proposal](https://github.com/tc39/proposal-global) - module [`esnext.global-this`](https://github.com/zloirock/core-js/blob/v3.0.1/packages/core-js/modules/esnext.global-this.js).
```js
Expand Down
18 changes: 13 additions & 5 deletions packages/core-js-compat/src/data.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
module.exports = {
const data = {
'es.symbol': {
edge: '15',
chrome: '49',
Expand Down Expand Up @@ -39,6 +39,10 @@ module.exports = {
firefox: '40',
safari: '10.0',
},
'es.symbol.match-all': {
chrome: '73',
firefox: '67',
},
'es.symbol.replace': {
chrome: '50',
firefox: '49',
Expand Down Expand Up @@ -832,6 +836,9 @@ module.exports = {
firefox: '49',
safari: '10.0',
},
'es.string.match-all': {
chrome: '73',
},
'es.string.pad-end': {
edge: '15',
chrome: '57',
Expand Down Expand Up @@ -1316,10 +1323,6 @@ module.exports = {
},
'esnext.string.code-points': {
},
'esnext.string.match-all': {
chrome: '73',
firefox: '67',
},
'esnext.string.replace-all': {
},
'esnext.symbol.dispose': {
Expand Down Expand Up @@ -1393,3 +1396,8 @@ module.exports = {
safari: '12.0',
},
};

// TODO: Remove from `core-js@4`
data['esnext.string.match-all'] = data['es.string.match-all'];

module.exports = data;
5 changes: 4 additions & 1 deletion packages/core-js-compat/src/modules-by-versions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
'use strict';
module.exports = {
// '3.1': ['esnext.builtin.feature'],
3.1: [
'es.string.match-all',
'es.symbol.match-all',
],
};
2 changes: 2 additions & 0 deletions packages/core-js/es/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require('../modules/es.symbol.has-instance');
require('../modules/es.symbol.is-concat-spreadable');
require('../modules/es.symbol.iterator');
require('../modules/es.symbol.match');
require('../modules/es.symbol.match-all');
require('../modules/es.symbol.replace');
require('../modules/es.symbol.search');
require('../modules/es.symbol.species');
Expand Down Expand Up @@ -75,6 +76,7 @@ require('../modules/es.string.code-point-at');
require('../modules/es.string.ends-with');
require('../modules/es.string.includes');
require('../modules/es.string.match');
require('../modules/es.string.match-all');
require('../modules/es.string.pad-end');
require('../modules/es.string.pad-start');
require('../modules/es.string.repeat');
Expand Down
8 changes: 8 additions & 0 deletions packages/core-js/es/instance/match-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var matchAll = require('../string/virtual/match-all');
var StringPrototype = String.prototype;

module.exports = function (it) {
var own = it.matchAll;
return typeof it === 'string' || it === StringPrototype
|| (it instanceof String && own === StringPrototype.matchAll) ? matchAll : own;
};
1 change: 1 addition & 0 deletions packages/core-js/es/string/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require('../../modules/es.string.code-point-at');
require('../../modules/es.string.ends-with');
require('../../modules/es.string.includes');
require('../../modules/es.string.match');
require('../../modules/es.string.match-all');
require('../../modules/es.string.pad-end');
require('../../modules/es.string.pad-start');
require('../../modules/es.string.repeat');
Expand Down
3 changes: 3 additions & 0 deletions packages/core-js/es/string/match-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require('../../modules/es.string.match-all');

module.exports = require('../../internals/entry-unbind')('String', 'matchAll');
1 change: 1 addition & 0 deletions packages/core-js/es/string/virtual/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require('../../../modules/es.string.code-point-at');
require('../../../modules/es.string.ends-with');
require('../../../modules/es.string.includes');
require('../../../modules/es.string.match');
require('../../../modules/es.string.match-all');
require('../../../modules/es.string.pad-end');
require('../../../modules/es.string.pad-start');
require('../../../modules/es.string.repeat');
Expand Down
3 changes: 3 additions & 0 deletions packages/core-js/es/string/virtual/match-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require('../../../modules/es.string.match-all');

module.exports = require('../../../internals/entry-virtual')('String').matchAll;
1 change: 1 addition & 0 deletions packages/core-js/es/symbol/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require('../../modules/es.symbol.has-instance');
require('../../modules/es.symbol.is-concat-spreadable');
require('../../modules/es.symbol.iterator');
require('../../modules/es.symbol.match');
require('../../modules/es.symbol.match-all');
require('../../modules/es.symbol.replace');
require('../../modules/es.symbol.search');
require('../../modules/es.symbol.species');
Expand Down
4 changes: 4 additions & 0 deletions packages/core-js/es/symbol/match-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require('../../modules/es.symbol.match-all');
require('../../modules/es.string.match-all');

module.exports = require('../../internals/wrapped-well-known-symbol').f('matchAll');
9 changes: 1 addition & 8 deletions packages/core-js/features/instance/match-all.js
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
var matchAll = require('../string/virtual/match-all');
var StringPrototype = String.prototype;

module.exports = function (it) {
var own = it.matchAll;
return typeof it === 'string' || it === StringPrototype
|| (it instanceof String && own === StringPrototype.matchAll) ? matchAll : own;
};
module.exports = require('../../es/instance/match-all');
1 change: 0 additions & 1 deletion packages/core-js/features/string/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = require('../../es/string');

require('../../modules/esnext.string.at');
require('../../modules/esnext.string.match-all');
require('../../modules/esnext.string.replace-all');
4 changes: 1 addition & 3 deletions packages/core-js/features/string/match-all.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
require('../../modules/esnext.string.match-all');

module.exports = require('../../internals/entry-unbind')('String', 'matchAll');
module.exports = require('../../es/string/match-all');
1 change: 0 additions & 1 deletion packages/core-js/features/string/virtual/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = require('../../../es/string/virtual');

require('../../../modules/esnext.string.at');
require('../../../modules/esnext.string.match-all');
require('../../../modules/esnext.string.replace-all');
4 changes: 1 addition & 3 deletions packages/core-js/features/string/virtual/match-all.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
require('../../../modules/esnext.string.match-all');

module.exports = require('../../../internals/entry-virtual')('String').matchAll;
module.exports = require('../../../es/string/virtual/match-all');
1 change: 1 addition & 0 deletions packages/core-js/features/symbol/match-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../es/symbol/match-all');
91 changes: 91 additions & 0 deletions packages/core-js/modules/es.string.match-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use strict';
var createIteratorConstructor = require('../internals/create-iterator-constructor');
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 classof = require('../internals/classof');
var getFlags = require('../internals/regexp-flags');
var hide = require('../internals/hide');
var speciesConstructor = require('../internals/species-constructor');
var advanceStringIndex = require('../internals/advance-string-index');
var MATCH_ALL = require('../internals/well-known-symbol')('matchAll');
var IS_PURE = require('../internals/is-pure');
var REGEXP_STRING = 'RegExp String';
var REGEXP_STRING_ITERATOR = REGEXP_STRING + ' Iterator';
var InternalStateModule = require('../internals/internal-state');
var setInternalState = InternalStateModule.set;
var getInternalState = InternalStateModule.getterFor(REGEXP_STRING_ITERATOR);
var RegExpPrototype = RegExp.prototype;
var regExpBuiltinExec = RegExpPrototype.exec;

var regExpExec = function (R, S) {
var exec = R.exec;
var result;
if (typeof exec == 'function') {
result = exec.call(R, S);
if (typeof result != 'object') throw TypeError('Incorrect exec result');
return result;
} return regExpBuiltinExec.call(R, S);
};

// eslint-disable-next-line max-len
var $RegExpStringIterator = createIteratorConstructor(function RegExpStringIterator(regexp, string, global, fullUnicode) {
setInternalState(this, {
type: REGEXP_STRING_ITERATOR,
regexp: regexp,
string: string,
global: global,
unicode: fullUnicode,
done: false
});
}, REGEXP_STRING, function next() {
var state = getInternalState(this);
if (state.done) return { value: undefined, done: true };
var R = state.regexp;
var S = state.string;
var match = regExpExec(R, S);
if (match === null) return { value: undefined, done: state.done = true };
if (state.global) {
if (String(match[0]) == '') R.lastIndex = advanceStringIndex(S, toLength(R.lastIndex), state.unicode);
return { value: match, done: false };
}
state.done = true;
return { value: match, done: false };
});

var $matchAll = function (string) {
var R = anObject(this);
var S = String(string);
var C, flagsValue, flags, matcher, global, fullUnicode;
C = speciesConstructor(R, RegExp);
flagsValue = R.flags;
if (flagsValue === undefined && R instanceof RegExp && !('flags' in RegExpPrototype)) {
flagsValue = getFlags.call(R);
}
flags = flagsValue === undefined ? '' : String(flagsValue);
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, matcher, rx;
if (regexp != null) {
matcher = regexp[MATCH_ALL];
if (matcher === undefined && IS_PURE && classof(regexp) == 'RegExp') matcher = $matchAll;
if (matcher != null) return aFunction(matcher).call(regexp, O);
}
S = String(O);
rx = new RegExp(regexp, 'g');
return IS_PURE ? $matchAll.call(rx, S) : rx[MATCH_ALL](S);
}
});

IS_PURE || MATCH_ALL in RegExpPrototype || hide(RegExpPrototype, MATCH_ALL, $matchAll);
2 changes: 2 additions & 0 deletions packages/core-js/modules/es.symbol.match-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// `Symbol.matchAll` well-known symbol
require('../internals/define-well-known-symbol')('matchAll');
Loading

0 comments on commit 1f8b914

Please sign in to comment.