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
Next Next commit
reorganize functions code to make them shorter
  • Loading branch information
borodean committed Oct 12, 2021
commit 873b46dc295152ae4ea99839b8dcee28d1298864
11 changes: 6 additions & 5 deletions picocolors.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ let formatter =
(input) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are going this way, should we change Prettier settings to reduce ( and ) for single argument function?

Copy link
Contributor Author

@borodean borodean Oct 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense if we're all-in on squeezing every byte out of it: ed55367.

let string = "" + input
let index = string.indexOf(close, open.length)
return !~index
? open + string + close
: open + replaceClose(string, close, replace, index) + close
return open + (~index ? replaceClose(string, close, replace, index) : string) + close
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This most likely affects benchmarks. I tried to use ternary operator within a concat expression and always saw notable drop in performance (see complex benchmark)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. Updated in a way that is still shorter, but preserves the original logic: 000f178.

}

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 + end : start + replaceClose(end, close, replace, nextIndex)
return (
string.substring(0, index) +
replace +
(~nextIndex ? replaceClose(end, close, replace, nextIndex) : end)
)
}

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