From 481bbeb374ad6a42d8a32cb37ef1b588d4d504d6 Mon Sep 17 00:00:00 2001 From: Sergey Vohmyanin Date: Wed, 19 Feb 2025 14:31:02 +0300 Subject: [PATCH] fix: remove recursion in replaceClose to prevent stack overflow(#106) --- index.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 0d64e6b..5845136 100644 --- a/index.js +++ b/index.js @@ -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