-
-
Save chadlavi/e79221fa64c44ef23e295cebce1e4a20 to your computer and use it in GitHub Desktop.
A console.log with a little more... flair.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rainbow = (s: string) => { | |
const colors = [ | |
'red', | |
'orange', | |
'yellow', | |
'green', | |
'blue', | |
'indigo', | |
'violet', | |
] | |
const logArray: string[] = [] | |
let colorCounter = 0 | |
let colorDirection = 1 | |
let logString = '' | |
s.split('').forEach((c) => { | |
const color = colors[colorCounter] | |
let formattedString = c | |
if (!c.match(/\s/)) { | |
colorDirection = (colorCounter >= (colors.length - 1)) | |
? -1 | |
: (colorCounter <= 0) | |
? 1 | |
: colorDirection | |
colorCounter += colorDirection | |
formattedString = `%c${c}` | |
logArray.push(`color:${color};`) | |
} | |
logString += formattedString | |
}) | |
return [logString, ...logArray] | |
} | |
interface Console { | |
rainbow: (s: string) => void | |
} | |
console.rainbow = (s: string) => console.log(...rainbow(s)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment