Skip to content

Commit

Permalink
fixed indexes in String#replaceAll callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Nov 8, 2019
1 parent 7f64202 commit 1519167
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Changelog
##### Unreleased
- Throw when `(Async)Iterator#flatMap` mapper returns a non-iterable, per [tc39/proposal-iterator-helpers/55](https://github.com/tc39/proposal-iterator-helpers/issues/55) and [tc39/proposal-iterator-helpers/59](https://github.com/tc39/proposal-iterator-helpers/pull/59)
- Fixed indexes in `String#replaceAll` callbacks
- `String#replaceAll` marked as supported by FF72

##### 3.4.0 - 2019.11.07
Expand Down
6 changes: 4 additions & 2 deletions packages/core-js/modules/esnext.string.replace-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var RegExpPrototype = RegExp.prototype;
$({ target: 'String', proto: true }, {
replaceAll: function replaceAll(searchValue, replaceValue) {
var O = requireObjectCoercible(this);
var IS_REG_EXP, flags, replacer, string, searchString, template, result, index;
var IS_REG_EXP, flags, replacer, string, searchString, template, result, position, index;
if (searchValue != null) {
IS_REG_EXP = isRegExp(searchValue);
if (IS_REG_EXP) {
Expand All @@ -39,8 +39,10 @@ $({ target: 'String', proto: true }, {
return template.join(String(replaceValue));
}
result = template[0];
position = result.length;
for (index = 1; index < template.length; index++) {
result += String(replaceValue(searchString, index - 1, string));
result += String(replaceValue(searchString, position, string));
position += searchString.length + template[index].length;
result += template[index];
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion tests/pure/esnext.string.replace-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ QUnit.test('String#replaceAll', assert => {
assert.same(replaceAll({}, 'bject', 'lolo'), '[ololo Ololo]');
assert.same(replaceAll('aba', 'b', (search, i, string) => {
assert.same(search, 'b', '`search` is `b`');
assert.same(i, 0, '`i` is 0');
assert.same(i, 1, '`i` is 1');
assert.same(string, 'aba', '`string` is `aba`');
return 'c';
}), 'aca');
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/esnext.string.replace-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ QUnit.test('String#replaceAll', assert => {
assert.same(replaceAll.call({}, 'bject', 'lolo'), '[ololo Ololo]');
assert.same('aba'.replaceAll('b', (search, i, string) => {
assert.same(search, 'b', '`search` is `b`');
assert.same(i, 0, '`i` is 0');
assert.same(i, 1, '`i` is 1');
assert.same(string, 'aba', '`string` is `aba`');
return 'c';
}), 'aca');
Expand Down

0 comments on commit 1519167

Please sign in to comment.