Skip to content

Commit

Permalink
drop an extra stringIndexOf from es.string.replace-all
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jan 1, 2024
1 parent 1cdaab2 commit 5b960e3
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions packages/core-js/modules/es.string.replace-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,9 @@ var replace = uncurryThis(''.replace);
var stringSlice = uncurryThis(''.slice);
var max = Math.max;

var stringIndexOf = function (string, searchValue, fromIndex) {
if (fromIndex > string.length) return -1;
if (searchValue === '') return fromIndex;
return indexOf(string, searchValue, fromIndex);
};

// `String.prototype.replaceAll` method
// https://tc39.es/ecma262/#sec-string.prototype.replaceall
$({ target: 'String', proto: true }, {
$({ target: 'String', proto: true, forced: true }, {
replaceAll: function replaceAll(searchValue, replaceValue) {
var O = requireObjectCoercible(this);
var IS_REG_EXP, flags, replacer, string, searchString, functionalReplace, searchLength, advanceBy, replacement;
Expand All @@ -54,14 +48,14 @@ $({ target: 'String', proto: true }, {
if (!functionalReplace) replaceValue = toString(replaceValue);
searchLength = searchString.length;
advanceBy = max(1, searchLength);
position = stringIndexOf(string, searchString, 0);
position = indexOf(string, searchString);
while (position !== -1) {
replacement = functionalReplace
? toString(replaceValue(searchString, position, string))
: getSubstitution(searchString, string, position, [], undefined, replaceValue);
result += stringSlice(string, endOfLastMatch, position) + replacement;
endOfLastMatch = position + searchLength;
position = stringIndexOf(string, searchString, position + advanceBy);
position = position + advanceBy > string.length ? -1 : indexOf(string, searchString, position + advanceBy);
}
if (endOfLastMatch < string.length) {
result += stringSlice(string, endOfLastMatch);
Expand Down

0 comments on commit 5b960e3

Please sign in to comment.