Skip to content

Commit

Permalink
fix: remove recursion in replaceClose to prevent stack overflow(#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delagen committed Feb 19, 2025
1 parent 0928e67 commit 481bbeb
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ const replaceClose = (
string,
close,
replace,
head = string.substring(0, index) + replace,
tail = string.substring(index + close.length),
next = tail.indexOf(close)
) => head + (next < 0 ? tail : replaceClose(next, tail, close, replace))
) => {
let result = '';
do {
const head = string.substring(0, index) + replace;
string = string.substring(index + close.length);
index = string.indexOf(close);
result += head;
} while (index >= 0);
return result + string;
}

const clearBleed = (index, string, open, close, replace) =>
index < 0
Expand Down

0 comments on commit 481bbeb

Please sign in to comment.