Skip to content

Instantly share code, notes, and snippets.

@chadlavi chadlavi/rainbowLog.ts forked from Benargee/rainbowLog.js
Last active Feb 29, 2020

Embed
What would you like to do?
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
You can’t perform that action at this time.