-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't format empty strings #69
Conversation
Could you please rebase this? |
@kibertoad sure |
Actually, couldn't we lift this check outside the body of the function? We just need to know if the string is empty or not. |
Nevermind. I think we can't, lol. 😁 |
Yeah, I was going to question that 😅 |
Using default function parameters might be better. const raw =
(open, close, searchRegex, replaceValue) =>
(s = "") =>
s === ""
? s
: open +
(~(s += "").indexOf(close, 4) // skip opening \x1b[
? s.replace(searchRegex, replaceValue)
: s) +
close And add these tests to export default [
t("colorette", [
t("emptiness", [
equal(c.blue(), ""),
equal(c.blue(""), ""),
equal(c.blue(undefined), ""),
equal(c.blue(0), "\x1b[34m0\x1b[39m"),
equal(c.blue(null), "\x1b[34mnull\x1b[39m"),
equal(c.blue(false), "\x1b[34mfalse\x1b[39m"),
]),
// ... |
This is not the perfect solution, because the default value will only be used if > String(undefined)
'undefined' The common usage is to style a string with some content, while colour is usually enabled. That means, that the UPDATE: As I am writing this, I have realized, that a simple check is not enough. For example, this is how Chalk behaves on empty/undefined input: > const chalk = require('chalk')
undefined
> chalk.bold('')
''
> chalk.bold(undefined)
'\x1B[1mundefined\x1B[22m'
> chalk.bold()
'' This is harder to implement using the default values, since "no arguments" is equivalent to "argument is undefined": > const test = (s = "") => (s += "") ? s : "STRING IS EMPTY"
undefined
> test()
'STRING IS EMPTY'
> test(undefined)
'STRING IS EMPTY' I wonder how to implement this nicely 🤔
Sure, I will. I'll leave out the |
After this change, I expect Colorette to behave as follows: import { blue } from "colorette"
blue() //=> ""
blue("") //=> ""
blue(undefined) //=> ""
blue(0) //=> "\x1b[34m0\x1b[39m"
blue(null) //=> "\x1b[34mnull\x1b[39m"
blue(false) //=> "\x1b[34mfalse\x1b[39m" What was your expectation? |
My expectation at first was to make it behave like Chalk, so that it could be considered as a drop-in replacement: > const { blue } = require('chalk')
> blue()
''
> blue("")
''
> blue(undefined)
'\x1B[34mundefined\x1B[39m'
> blue(0)
'\x1B[34m0\x1B[39m'
> blue(null)
'\x1B[34mnull\x1B[39m'
> blue(false)
'\x1B[34mfalse\x1B[39m' |
@NickKaramoff How is not returning empty string on undefined not surprising for the user? |
We're not really a drop-in replacement. I think #69 (comment) is more sensible, so let's go with that, please. |
@jorgebucaran I second that. |
Add the default parameter so that `style(undefined)` also resolves to an empty string. See #69
Codecov Report
@@ Coverage Diff @@
## main #69 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 1 1
Lines 125 129 +4
=========================================
+ Hits 125 129 +4
Continue to review full report at Codecov.
|
@jorgebucaran all done |
Now available in |
Thank you for finishing this 😅 |
Chalk has this feature that when a string is empty, it doesn't add opening and closing modifiers to it, unlike Colorette:
This makes the migration from Chalk sometimes problematic, especially in tests, which often depend on pre-generated fixtures. Apart from that, returning a self-closing ANSI sequence seems suboptimal to me.
This PR rewrites the
raw()
function and adds a string length check. This didn't seem to cause any slowdowns when running benchmarks, since the needed conversion has been done anyway.