36
loading...
This website collects cookies to deliver better user experience
getMonth
function which would return the translation key according to the given month:const getMonth = (month: string) => {
let translationKey = ''
switch (month) {
case 'January':
translationKey = 'JANUARY_TRANSLATION_KEY'
break
case 'February':
translationKey = 'FEBRUARY_TRANSLATION_KEY'
break
case 'March':
translationKey = 'MARCH_TRANSLATION_KEY'
break
case 'April':
translationKey = 'APRIL_TRANSLATION_KEY'
break
case 'May':
translationKey = 'MAY_TRANSLATION_KEY'
break
case 'June':
translationKey = 'JUNE_TRANSLATION_KEY'
break
case 'July':
translationKey = 'JULY_TRANSLATION_KEY'
break
case 'August':
translationKey = 'AUGUST_TRANSLATION_KEY'
break
case 'September':
translationKey = 'SEPTEMBER_TRANSLATION_KEY'
break
case 'October':
translationKey = 'OCTOBER_TRANSLATION_KEY'
break
case 'November':
translationKey = 'NOVEMBER_TRANSLATION_KEY'
break
case 'December':
translationKey = 'DECEMBER_TRANSLATION_KEY'
}
return translationKey
}
getMonth
function is doing, you realize that it's doing nothing but mapping keys to values, which is exactly what an object does! ✨type Month =
| 'January'
| 'February'
| 'March'
| 'April'
| 'May'
| 'June'
| 'July'
| 'August'
| 'September'
| 'October'
| 'November'
| 'December'
type Mapping = Record<Month, string>
const MONTH_TO_TRANSLATION_KEY: Mapping = {
January: 'JANUARY_TRANSLATION_KEY',
February: 'FEBRUARY_TRANSLATION_KEY',
March: 'MARCH_TRANSLATION_KEY',
April: 'APRIL_TRANSLATION_KEY',
May: 'MAY_TRANSLATION_KEY',
June: 'JUNE_TRANSLATION_KEY',
July: 'JULY_TRANSLATION_KEY',
August: 'AUGUST_TRANSLATION_KEY',
September: 'SEPTEMBER_TRANSLATION_KEY',
October: 'OCTOBER_TRANSLATION_KEY',
November: 'NOVEMBER_TRANSLATION_KEY',
December: 'DECEMBER_TRANSLATION_KEY',
}
const getMonth = (month: Month) => MONTH_TO_TRANSLATION_KEY[month]