My coworkwer, who left recently, wrote this and I'm trying to understand how to best catch errors if an invalid key is passed to the parent function. This child function uses that key to map the call of another function to return appropriate formatting for currencies.
Fairly new to JavaScript, so this is challenging for me. It doesn't seem like I can easily call the key for the mapping I made in the numTypeMap function?
I tried calling keys on the object or use toString to see if it would output the mapping options in some way. I'm probably misunderstanding something basic about JS.
export const getFormattedValue = (
value: number | null | undefined,
numType: string,
options?: { round?: boolean; expanded?: boolean }
): string => {
if (value === null || value === undefined) {
return '';
}
const numTypeMap: { [t: string]: (n: number, round?: boolean, expanded?: boolean) => string } = {
number: toReadableNumber,
percent: toReadablePercentage,
dollar: toReadableDollar,
canadianDollar: toReadableCanadianDollar,
pound: toReadablePound,
euro: toReadableEuro,
};
return numTypeMap[numType](value, options?.round, options?.expanded) as string;
};