Last active
January 19, 2022 23:14
-
-
Save itssimmons/dcdc6e59b3e5e216e6d9c987b013e71d to your computer and use it in GitHub Desktop.
Don't use switch use an object! 😉
This file contains 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
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 | |
} | |
} | |
This file contains 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
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 | |
} |
This file contains 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
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