Skip to content

Instantly share code, notes, and snippets.

@itssimmons
Last active January 19, 2022 23:14
Show Gist options
  • Save itssimmons/dcdc6e59b3e5e216e6d9c987b013e71d to your computer and use it in GitHub Desktop.
Save itssimmons/dcdc6e59b3e5e216e6d9c987b013e71d to your computer and use it in GitHub Desktop.
Don't use switch use an object! 😉
import { joke, hardJoke, riddle } from '@/content'
import getQuote, { getAllQuotes } from '@/helpers/quotes'
import { Fact } from '@/types/common.types'
export function boredSwitch(topic: string): Fact {
switch (topic.toLowerCase()) {
case 'quote':
return getQuote()
break
case 'all-quotes':
return getAllQuotes()
break
case 'joke':
return joke
break
case 'hardjoke':
return hardJoke
break
case 'riddle':
return riddle
break
default:
return "Selected option doesn't exist"
break
}
}
import { joke, hardJoke, riddle } from '@/content'
import getQuote, { getAllQuotes } from '@/helpers/quotes'
import { Fact } from '@/types/common.types'
import { topicOption } from '@/types/topics.types'
export function coolSwitch(topic: string): Fact {
const topics: topicOption = {
'quote': getQuote(),
'all-quotes': getAllQuotes(),
'joke': joke,
'hardjoke': hardJoke,
'riddle': riddle
}
const DEFAULT_TOPIC: string = "Selected option doesn't exist"
return topics[topic.toLowerCase()] || DEFAULT_TOPIC
}
import { Fact } from "./common.types"
/** Recieve a string key value and returns a Fact */
export type topicOption = {
[key: string]: Fact
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment