Skip to content

Commit a014200

Browse files
authored
fix: iteratively replace "close" to avoid maximum stack error (#64)
* fix: iteratively replace "close" to avoid maximum stack error * perf: apply suggestion to special case initial check
1 parent b626148 commit a014200

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

picocolors.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ let formatter =
1919
}
2020

2121
let replaceClose = (string, close, replace, index) => {
22-
let start = string.substring(0, index) + replace
23-
let end = string.substring(index + close.length)
24-
let nextIndex = end.indexOf(close)
25-
return ~nextIndex ? start + replaceClose(end, close, replace, nextIndex) : start + end
22+
let result = ""
23+
let cursor = 0
24+
do {
25+
result += string.substring(cursor, index) + replace
26+
cursor = index + close.length
27+
index = string.indexOf(close, cursor)
28+
} while (~index)
29+
return result + string.substring(cursor)
2630
}
2731

2832
let createColors = (enabled = isColorSupported) => ({

0 commit comments

Comments
 (0)