Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: iteratively replace "close" to avoid maximum stack error
  • Loading branch information
hi-ogawa committed May 10, 2024
commit 3236f18fd5733315919d39827d2eeece2af16b9c
24 changes: 12 additions & 12 deletions picocolors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ let isColorSupported =
let formatter =
(open, close, replace = open) =>
input => {
let string = "" + input
let index = string.indexOf(close, open.length)
return ~index
? open + replaceClose(string, close, replace, index) + close
: open + string + close
let string = "" + input;
let result = "";
while (true) {
let i = string.indexOf(close);
if (i === -1) {
result += string;
break;
}
result += string.substring(0, i) + replace;
string = string.substring(i + close.length);
}
return open + result + close;
}

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 createColors = (enabled = isColorSupported) => ({
isColorSupported: enabled,
reset: enabled ? s => `\x1b[0m${s}\x1b[0m` : String,
Expand Down