Skip to content

Commit

Permalink
fix: iteratively replace "close" to avoid maximum stack error (#64)
Browse files Browse the repository at this point in the history
* fix: iteratively replace "close" to avoid maximum stack error

* perf: apply suggestion to special case initial check
  • Loading branch information
hi-ogawa authored May 14, 2024
1 parent b626148 commit a014200
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions picocolors.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ let formatter =
}

let replaceClose = (string, close, replace, index) => {
let start = string.substring(0, index) + replace
let end = string.substring(index + close.length)
let nextIndex = end.indexOf(close)
return ~nextIndex ? start + replaceClose(end, close, replace, nextIndex) : start + end
let result = ""
let cursor = 0
do {
result += string.substring(cursor, index) + replace
cursor = index + close.length
index = string.indexOf(close, cursor)
} while (~index)
return result + string.substring(cursor)
}

let createColors = (enabled = isColorSupported) => ({
Expand Down

0 comments on commit a014200

Please sign in to comment.