Skip to content

Instantly share code, notes, and snippets.

@chadlavi
Forked from Benargee/rainbowLog.js
Last active February 29, 2020 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chadlavi/e79221fa64c44ef23e295cebce1e4a20 to your computer and use it in GitHub Desktop.
Save chadlavi/e79221fa64c44ef23e295cebce1e4a20 to your computer and use it in GitHub Desktop.
A console.log with a little more... flair.
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