Example problem: I have an Enum variable Difficulty. In a function, I want to set the config DifficultyConfig depending on the value of Difficulty. Here's an inelegant way that I can think of:
export interface DifficultyConfig {
healthModifier: number,
deathIsPermanent: boolean,
}
export interface AppProps {
difficultyConfig: DifficultyConfig
}
export const NormalDifficultyConfig: DifficultyConfig = {
enemyHealthModifier: 1,
deathIsPermanent: false,
}
export const HigherDifficultyConfig: DifficultyConfig = {
enemyHealthModifier: 1.3,
deathIsPermanent: true,
}
export enum Difficulty {
NORMAL = 'Normal',
HARD = 'Hard',
ADVANCED = 'Advanced',
}
function createApp(difficulty: Difficulty) {
let difficultyConfig: DifficultyConfig;
switch(difficulty) {
case Difficulty.NORMAL:
difficultyConfig = NormalDifficultyConfig;
break;
// Both HARD and ADVANCED get HigherDifficultyConfig
case Difficulty.HARD:
difficultyConfig = HigherDifficultyConfig;
break;
case Difficulty.ADVANCED:
difficultyConfig = HigherDifficultyConfig;
break;
default:
difficultyConfig = NormalDifficultyConfig;
break;
}
return new App({
difficultyConfig
});
}
I'm not fond of the Switch case syntax for something so simple. My ideal would be something like this in Scala:
val difficultyConfig = difficulty match {
case Difficulty.NORMAL => NormalDifficultyConfig
case Difficulty.HARD | Difficulty.ADVANCED => HigherDifficultyConfig
case _ => NormalDifficultyConfig
}
Is there an equivalent for this in JavaScript?