Allow color functions to receive a parameter of any type
Is your feature request related to a problem? Please describe.
Is is slightly annoying to have to call toString on parameters that are not original string when using colors. For example, you cannot do:
console.log(red(new Date());
As date is not a string. Ideally red accepts anything and converts it to its string representation using toString.
Describe the solution you'd like
Add a string conversion internally if the parameters is not a string.
Describe alternatives you've considered
Not really any alternatives.
Happy to try my luck at contributing if the Deno team thinks this is worthwhile.
I personally think the current design is more explicit and fine.
You can alternatively do the below (6 chars less than .toString()):
red(`${new Date()}`)
I agree, the current design is not bad, it can just be made better :)
My two cents: the backticks are cognitive overload in what is already difficult to parse nested string interpolations.
Thanks for responding.
new Date() is a great example for why library functions shouldn't implicitly stringify non-string arguments. There are many different ways to serialize a date, and which is best depends on your use case.
const date = new Date(0)
date.toString() // Thu Jan 01 1970 08:00:00 GMT+0800 (China Standard Time)
date.toDateString() // Thu Jan 01 1970
date.toTimeString() // 08:00:00 GMT+0800 (China Standard Time)
date.toUTCString() // Thu, 01 Jan 1970 00:00:00 GMT
date.toISOString() // 1970-01-01T00:00:00.000Z
date.toLocaleString() // 01/01/1970, 08:00:00
date.toLocaleDateString() // 01/01/1970
date.toLocaleTimeString() // 08:00:00
date.toLocaleString('ar-EG') // ١/١/١٩٧٠ ٨:٠٠:٠٠ ص
date.toLocaleString('th-TH') // 1/1/2513 08:00:00