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
Prev Previous commit
perf: apply suggestion to special case initial check
  • Loading branch information
hi-ogawa committed May 14, 2024
commit 898d93d95ebd12eef864906793aa098c79c5c865
28 changes: 16 additions & 12 deletions picocolors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ let isColorSupported =
let formatter =
(open, close, replace = open) =>
input => {
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 string = "" + input
let index = string.indexOf(close, open.length)
return ~index
? open + replaceClose(string, close, replace, index) + close
: open + string + close
}

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