I'm studying javascript, and I've already seen that there's a way to reduce the number of switch - case with Object Literals. I'm trying to change this method, but I am not able to reduce the number of switches
static darkerColors(value: string, theme: ThemeMode) {
const test = theme === ThemeMode.LIGHT
switch (value) {
case Colors.BLUE: {
return test ? '#253F82' : '#BED1FF'
}
case Colors.CYAN: {
return test ? '#066262' : '#A2EAEA'
}
case Colors.PURPLE: {
return test ? '#4727B0' : '#D3C6FD'
}
case Colors.ORANGE: {
return test ? '#9C2100' : '#FF9377'
}
case Colors.YELLOW: {
return test ? '#6C5200' : '#F9E298'
}
default:
return test ? '#32363B' : '#C9CED4'
}
}
you can use an object to handle the configuration
like this
const config = { light: { blue: '#253F82', default: '#32363B' }, default: { blue: '#BED1FF', default: '#C9CED4' } } function darkerColors(value, theme) { const fallback = 'default' const colors = config[theme] || config[fallback] return colors[value] || colors[fallback] } console.log(darkerColors('blue', 'light')) console.log(darkerColors('red', 'light')) console.log(darkerColors('blue', 'dark')) console.log(darkerColors('red', 'dark'))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.