Skip to content

Commit

Permalink
fix: make replaceAll() anticipate word occurence within searchIndex a…
Browse files Browse the repository at this point in the history
…nd offset
  • Loading branch information
Im-Beast committed Jul 3, 2022
1 parent e093b6c commit 7f6fb33
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ export function replaceAll(
if (searchIndex === -1) return string;

const offset = search.length;
const lookBackOffset = replaceValue.length;

do {
string = string.slice(0, searchIndex) + replaceValue +
string.slice(searchIndex + search.length);
searchIndex = string.indexOf(search, searchIndex + offset);
searchIndex = string.indexOf(search, searchIndex - offset + lookBackOffset);
} while (searchIndex !== -1);

return string;
Expand Down

0 comments on commit 7f6fb33

Please sign in to comment.