Skip to content

Commit

Permalink
simplify fix-regexp-well-known-symbol-logic
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jun 20, 2021
1 parent 3646a74 commit f7b619d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 32 deletions.
15 changes: 3 additions & 12 deletions packages/core-js/internals/fix-regexp-well-known-symbol-logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var createNonEnumerableProperty = require('../internals/create-non-enumerable-pr
var SPECIES = wellKnownSymbol('species');
var RegExpPrototype = RegExp.prototype;

module.exports = function (KEY, length, exec, FORCED, sham) {
module.exports = function (KEY, exec, FORCED, sham) {
var SYMBOL = wellKnownSymbol(KEY);

var DELEGATES_TO_SYMBOL = !fails(function () {
Expand Down Expand Up @@ -63,18 +63,9 @@ module.exports = function (KEY, length, exec, FORCED, sham) {
}
return { done: false };
});
var stringMethod = methods[0];
var regexMethod = methods[1];

redefine(String.prototype, KEY, stringMethod);
redefine(RegExpPrototype, SYMBOL, length == 2
// 21.2.5.8 RegExp.prototype[@@replace](string, replaceValue)
// 21.2.5.11 RegExp.prototype[@@split](string, limit)
? function (string, arg) { return regexMethod.call(string, this, arg); }
// 21.2.5.6 RegExp.prototype[@@match](string)
// 21.2.5.9 RegExp.prototype[@@search](string)
: function (string) { return regexMethod.call(string, this); }
);
redefine(String.prototype, KEY, methods[0]);
redefine(RegExpPrototype, SYMBOL, methods[1]);
}

if (sham) createNonEnumerableProperty(RegExpPrototype[SYMBOL], 'sham', true);
Expand Down
10 changes: 5 additions & 5 deletions packages/core-js/modules/es.string.match.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var advanceStringIndex = require('../internals/advance-string-index');
var regExpExec = require('../internals/regexp-exec-abstract');

// @@match logic
fixRegExpWellKnownSymbolLogic('match', 1, function (MATCH, nativeMatch, maybeCallNative) {
fixRegExpWellKnownSymbolLogic('match', function (MATCH, nativeMatch, maybeCallNative) {
return [
// `String.prototype.match` method
// https://tc39.es/ecma262/#sec-string.prototype.match
Expand All @@ -18,12 +18,12 @@ fixRegExpWellKnownSymbolLogic('match', 1, function (MATCH, nativeMatch, maybeCal
},
// `RegExp.prototype[@@match]` method
// https://tc39.es/ecma262/#sec-regexp.prototype-@@match
function (regexp) {
var res = maybeCallNative(nativeMatch, regexp, this);
function (string) {
var res = maybeCallNative(nativeMatch, this, string);
if (res.done) return res.value;

var rx = anObject(regexp);
var S = String(this);
var rx = anObject(this);
var S = String(string);

if (!rx.global) return regExpExec(rx, S);

Expand Down
10 changes: 5 additions & 5 deletions packages/core-js/modules/es.string.replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE = (function () {
})();

// @@replace logic
fixRegExpWellKnownSymbolLogic('replace', 2, function (_, nativeReplace, maybeCallNative) {
fixRegExpWellKnownSymbolLogic('replace', function (_, nativeReplace, maybeCallNative) {
var UNSAFE_SUBSTITUTE = REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE ? '$' : '$0';

return [
Expand All @@ -49,17 +49,17 @@ fixRegExpWellKnownSymbolLogic('replace', 2, function (_, nativeReplace, maybeCal
},
// `RegExp.prototype[@@replace]` method
// https://tc39.es/ecma262/#sec-regexp.prototype-@@replace
function (regexp, replaceValue) {
function (string, replaceValue) {
if (
(!REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE && REPLACE_KEEPS_$0) ||
(typeof replaceValue === 'string' && replaceValue.indexOf(UNSAFE_SUBSTITUTE) === -1)
) {
var res = maybeCallNative(nativeReplace, regexp, this, replaceValue);
var res = maybeCallNative(nativeReplace, this, string, replaceValue);
if (res.done) return res.value;
}

var rx = anObject(regexp);
var S = String(this);
var rx = anObject(this);
var S = String(string);

var functionalReplace = typeof replaceValue === 'function';
if (!functionalReplace) replaceValue = String(replaceValue);
Expand Down
10 changes: 5 additions & 5 deletions packages/core-js/modules/es.string.search.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var sameValue = require('../internals/same-value');
var regExpExec = require('../internals/regexp-exec-abstract');

// @@search logic
fixRegExpWellKnownSymbolLogic('search', 1, function (SEARCH, nativeSearch, maybeCallNative) {
fixRegExpWellKnownSymbolLogic('search', function (SEARCH, nativeSearch, maybeCallNative) {
return [
// `String.prototype.search` method
// https://tc39.es/ecma262/#sec-string.prototype.search
Expand All @@ -17,12 +17,12 @@ fixRegExpWellKnownSymbolLogic('search', 1, function (SEARCH, nativeSearch, maybe
},
// `RegExp.prototype[@@search]` method
// https://tc39.es/ecma262/#sec-regexp.prototype-@@search
function (regexp) {
var res = maybeCallNative(nativeSearch, regexp, this);
function (string) {
var res = maybeCallNative(nativeSearch, this, string);
if (res.done) return res.value;

var rx = anObject(regexp);
var S = String(this);
var rx = anObject(this);
var S = String(string);

var previousLastIndex = rx.lastIndex;
if (!sameValue(previousLastIndex, 0)) rx.lastIndex = 0;
Expand Down
10 changes: 5 additions & 5 deletions packages/core-js/modules/es.string.split.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var SPLIT_WORKS_WITH_OVERWRITTEN_EXEC = !fails(function () {
});

// @@split logic
fixRegExpWellKnownSymbolLogic('split', 2, function (SPLIT, nativeSplit, maybeCallNative) {
fixRegExpWellKnownSymbolLogic('split', function (SPLIT, nativeSplit, maybeCallNative) {
var internalSplit;
if (
'abbc'.split(/(b)*/)[1] == 'c' ||
Expand Down Expand Up @@ -97,12 +97,12 @@ fixRegExpWellKnownSymbolLogic('split', 2, function (SPLIT, nativeSplit, maybeCal
//
// NOTE: This cannot be properly polyfilled in engines that don't support
// the 'y' flag.
function (regexp, limit) {
var res = maybeCallNative(internalSplit, regexp, this, limit, internalSplit !== nativeSplit);
function (string, limit) {
var res = maybeCallNative(internalSplit, this, string, limit, internalSplit !== nativeSplit);
if (res.done) return res.value;

var rx = anObject(regexp);
var S = String(this);
var rx = anObject(this);
var S = String(string);
var C = speciesConstructor(rx, RegExp);

var unicodeMatching = rx.unicode;
Expand Down

0 comments on commit f7b619d

Please sign in to comment.