Skip to content

Commit

Permalink
move String#replaceAll to stable ES (#842)
Browse files Browse the repository at this point in the history
per Jun TC39 meeting
  • Loading branch information
pustovalov committed Oct 26, 2020
1 parent 389307d commit 8b121e3
Show file tree
Hide file tree
Showing 19 changed files with 89 additions and 68 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,4 +1,7 @@
## Changelog
##### Unreleased
- `String#replaceAll` moved to stage 4, per Jun TC39 meeting

##### 3.6.5 - 2020.04.09
- Updated Browserlist [#755](https://github.com/zloirock/core-js/issues/755)
- Fixed `setImmediate` in Safari [#770](https://github.com/zloirock/core-js/issues/770), thanks [@dtinth](https://github.com/dtinth)
Expand Down
14 changes: 7 additions & 7 deletions README.md
Expand Up @@ -1693,6 +1693,13 @@ class Promise {
```js
core-js/proposals/promise-all-settled
```
##### [`String#replaceAll`](https://github.com/tc39/proposal-string-replace-all)
Module [`esnext.string.replace-all`](https://github.com/zloirock/core-js/blob/v3.6.5/packages/core-js/modules/esnext.string.replace-all.js)
```js
class String {
replaceAll(searchValue: string | RegExp, replaceString: string | (searchValue, index, this) => string): string;
}
```

#### Stage 3 proposals
[*CommonJS entry points:*](#commonjs-api)
Expand Down Expand Up @@ -1732,13 +1739,6 @@ Promise.any([
Promise.reject(3),
]).catch(({ errors }) => console.log(errors)); // => [1, 2, 3]
```
##### [`String#replaceAll`](https://github.com/tc39/proposal-string-replace-all)
Module [`esnext.string.replace-all`](https://github.com/zloirock/core-js/blob/v3.6.5/packages/core-js/modules/esnext.string.replace-all.js)
```js
class String {
replaceAll(searchValue: string | RegExp, replaceString: string | (searchValue, index, this) => string): string;
}
```
[*CommonJS entry points:*](#commonjs-api)
```js
core-js/proposals/string-replace-all
Expand Down
10 changes: 8 additions & 2 deletions packages/core-js-compat/src/data.js
Expand Up @@ -1228,6 +1228,12 @@ const data = {
firefox: '53',
safari: '10.0',
},
'es.string.replace-all': {
chrome: '85',
firefox: '77',
safari: '13.1',
ios: '13.4',
},
'esnext.aggregate-error': {
},
'esnext.array.is-template-object': {
Expand Down Expand Up @@ -1443,8 +1449,7 @@ const data = {
},
// TODO: Remove from `core-js@4`
'esnext.string.match-all': null,
'esnext.string.replace-all': {
},
'esnext.string.replace-all': null,
'esnext.symbol.async-dispose': {
},
'esnext.symbol.dispose': {
Expand Down Expand Up @@ -1523,6 +1528,7 @@ const data = {
};

// TODO: Remove from `core-js@4`
data['esnext.string.replace-all'] = data['es.string.replace-all'];
data['esnext.global-this'] = data['es.global-this'];
data['esnext.promise.all-settled'] = data['es.promise.all-settled'];
data['esnext.string.match-all'] = data['es.string.match-all'];
Expand Down
1 change: 1 addition & 0 deletions packages/core-js-compat/src/modules-by-versions.js
Expand Up @@ -55,5 +55,6 @@ module.exports = {
3.6: [
'es.regexp.sticky',
'es.regexp.test',
'es.string.replace-all',
],
};
1 change: 1 addition & 0 deletions packages/core-js/es/index.js
Expand Up @@ -102,6 +102,7 @@ require('../modules/es.string.small');
require('../modules/es.string.strike');
require('../modules/es.string.sub');
require('../modules/es.string.sup');
require('../modules/es.string.replace-all');
require('../modules/es.regexp.constructor');
require('../modules/es.regexp.exec');
require('../modules/es.regexp.flags');
Expand Down
1 change: 1 addition & 0 deletions packages/core-js/es/string/index.js
Expand Up @@ -10,6 +10,7 @@ require('../../modules/es.string.pad-end');
require('../../modules/es.string.pad-start');
require('../../modules/es.string.repeat');
require('../../modules/es.string.replace');
require('../../modules/es.string.replace-all');
require('../../modules/es.string.search');
require('../../modules/es.string.split');
require('../../modules/es.string.starts-with');
Expand Down
4 changes: 4 additions & 0 deletions packages/core-js/es/string/replace-all.js
@@ -0,0 +1,4 @@
require('../../modules/es.string.replace-all');
var entryUnbind = require('../../internals/entry-unbind');

module.exports = entryUnbind('String', 'replaceAll');
1 change: 1 addition & 0 deletions packages/core-js/es/string/virtual/index.js
Expand Up @@ -7,6 +7,7 @@ require('../../../modules/es.string.pad-end');
require('../../../modules/es.string.pad-start');
require('../../../modules/es.string.repeat');
require('../../../modules/es.string.replace');
require('../../../modules/es.string.replace-all');
require('../../../modules/es.string.search');
require('../../../modules/es.string.split');
require('../../../modules/es.string.starts-with');
Expand Down
5 changes: 1 addition & 4 deletions packages/core-js/features/string/replace-all.js
@@ -1,4 +1 @@
require('../../modules/esnext.string.replace-all');
var entryUnbind = require('../../internals/entry-unbind');

module.exports = entryUnbind('String', 'replaceAll');
module.exports = require('../../es/string/replace-all');
50 changes: 50 additions & 0 deletions packages/core-js/modules/es.string.replace-all.js
@@ -0,0 +1,50 @@
'use strict';
var $ = require('../internals/export');
var requireObjectCoercible = require('../internals/require-object-coercible');
var isRegExp = require('../internals/is-regexp');
var getRegExpFlags = require('../internals/regexp-flags');
var wellKnownSymbol = require('../internals/well-known-symbol');
var IS_PURE = require('../internals/is-pure');

var REPLACE = wellKnownSymbol('replace');
var RegExpPrototype = RegExp.prototype;

// `String.prototype.replaceAll` method
// https://github.com/tc39/proposal-string-replace-all
$({ target: 'String', proto: true }, {
replaceAll: function replaceAll(searchValue, replaceValue) {
var O = requireObjectCoercible(this);
var IS_REG_EXP, flags, replacer, string, searchString, template, result, position, index;
if (searchValue != null) {
IS_REG_EXP = isRegExp(searchValue);
if (IS_REG_EXP) {
flags = String(requireObjectCoercible('flags' in RegExpPrototype
? searchValue.flags
: getRegExpFlags.call(searchValue)
));
if (!~flags.indexOf('g')) throw TypeError('`.replaceAll` does not allow non-global regexes');
}
replacer = searchValue[REPLACE];
if (replacer !== undefined) {
return replacer.call(searchValue, O, replaceValue);
} else if (IS_PURE && IS_REG_EXP) {
return String(O).replace(searchValue, replaceValue);
}
}
string = String(O);
searchString = String(searchValue);
if (searchString === '') return replaceAll.call(string, /(?:)/g, replaceValue);
template = string.split(searchString);
if (typeof replaceValue !== 'function') {
return template.join(String(replaceValue));
}
result = template[0];
position = result.length;
for (index = 1; index < template.length; index++) {
result += String(replaceValue(searchString, position, string));
position += searchString.length + template[index].length;
result += template[index];
}
return result;
}
});
52 changes: 2 additions & 50 deletions packages/core-js/modules/esnext.string.replace-all.js
@@ -1,50 +1,2 @@
'use strict';
var $ = require('../internals/export');
var requireObjectCoercible = require('../internals/require-object-coercible');
var isRegExp = require('../internals/is-regexp');
var getRegExpFlags = require('../internals/regexp-flags');
var wellKnownSymbol = require('../internals/well-known-symbol');
var IS_PURE = require('../internals/is-pure');

var REPLACE = wellKnownSymbol('replace');
var RegExpPrototype = RegExp.prototype;

// `String.prototype.replaceAll` method
// https://github.com/tc39/proposal-string-replace-all
$({ target: 'String', proto: true }, {
replaceAll: function replaceAll(searchValue, replaceValue) {
var O = requireObjectCoercible(this);
var IS_REG_EXP, flags, replacer, string, searchString, template, result, position, index;
if (searchValue != null) {
IS_REG_EXP = isRegExp(searchValue);
if (IS_REG_EXP) {
flags = String(requireObjectCoercible('flags' in RegExpPrototype
? searchValue.flags
: getRegExpFlags.call(searchValue)
));
if (!~flags.indexOf('g')) throw TypeError('`.replaceAll` does not allow non-global regexes');
}
replacer = searchValue[REPLACE];
if (replacer !== undefined) {
return replacer.call(searchValue, O, replaceValue);
} else if (IS_PURE && IS_REG_EXP) {
return String(O).replace(searchValue, replaceValue);
}
}
string = String(O);
searchString = String(searchValue);
if (searchString === '') return replaceAll.call(string, /(?:)/g, replaceValue);
template = string.split(searchString);
if (typeof replaceValue !== 'function') {
return template.join(String(replaceValue));
}
result = template[0];
position = result.length;
for (index = 1; index < template.length; index++) {
result += String(replaceValue(searchString, position, string));
position += searchString.length + template[index].length;
result += template[index];
}
return result;
}
});
// TODO: Remove from `core-js@4`
require('./es.string.replace-all');
3 changes: 3 additions & 0 deletions packages/core-js/stable/string/replace-all.js
@@ -0,0 +1,3 @@
var parent = require('../../es/string/replace-all');

module.exports = parent;
1 change: 0 additions & 1 deletion packages/core-js/stage/3.js
@@ -1,5 +1,4 @@
require('../proposals/promise-any');
require('../proposals/string-replace-all');
var parent = require('./4');

module.exports = parent;
1 change: 1 addition & 0 deletions packages/core-js/stage/4.js
@@ -1,6 +1,7 @@
require('../proposals/global-this');
require('../proposals/promise-all-settled');
require('../proposals/string-match-all');
require('../proposals/string-replace-all');
var path = require('../internals/path');

module.exports = path;
2 changes: 2 additions & 0 deletions tests/commonjs.js
Expand Up @@ -581,6 +581,7 @@ for (const _PATH of ['../packages/core-js-pure', '../packages/core-js']) {
ok(typeof load('stable/string/strike') === 'function');
ok(typeof load('stable/string/sub') === 'function');
ok(typeof load('stable/string/sup') === 'function');
ok(typeof load('stable/string/replace-all') === 'function');
ok(load('stable/string/pad-start')('a', 3) === ' a');
ok(load('stable/string/pad-end')('a', 3) === 'a ');
ok(load('stable/string/trim-start')(' a ') === 'a ');
Expand Down Expand Up @@ -851,6 +852,7 @@ for (const _PATH of ['../packages/core-js-pure', '../packages/core-js']) {
ok(typeof load('es/string/strike') === 'function');
ok(typeof load('es/string/sub') === 'function');
ok(typeof load('es/string/sup') === 'function');
ok(typeof load('es/string/replace-all') === 'function');
ok(load('es/string/pad-start')('a', 3) === ' a');
ok(load('es/string/pad-end')('a', 3) === 'a ');
ok('next' in load('es/string/iterator')('qwe'));
Expand Down
6 changes: 3 additions & 3 deletions tests/compat/tests.js
Expand Up @@ -875,6 +875,9 @@ GLOBAL.tests = {
&& 'a'.replace(/./, '$0') === '$0'
&& /./[Symbol.replace]('a', '$0') === '$0';
},
'es.string.replace-all': function () {
return String.prototype.replaceAll;
},
'es.string.search': function () {
var O = {};
O[Symbol.search] = function () { return 7; };
Expand Down Expand Up @@ -1401,9 +1404,6 @@ GLOBAL.tests = {
'esnext.string.code-points': function () {
return String.prototype.codePoints;
},
'esnext.string.replace-all': function () {
return String.prototype.replaceAll;
},
'esnext.symbol.dispose': function () {
return Symbol.dispose;
},
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/tests/index.js
Expand Up @@ -137,6 +137,7 @@ import './es.string.pad-start';
import './es.string.raw';
import './es.string.repeat';
import './es.string.replace';
import './es.string.replace-all';
import './es.string.search';
import './es.string.small';
import './es.string.split';
Expand Down Expand Up @@ -294,7 +295,6 @@ import './esnext.set.symmetric-difference';
import './esnext.set.union';
import './esnext.string.at';
import './esnext.string.code-points';
import './esnext.string.replace-all';
import './esnext.symbol.async-dispose';
import './esnext.symbol.dispose';
import './esnext.symbol.observable';
Expand Down

0 comments on commit 8b121e3

Please sign in to comment.