Files
headlessui/packages/@headlessui-react/src/utils/match.ts
T
2020-09-16 18:20:49 +02:00

25 lines
672 B
TypeScript

export function match<TValue extends string | number = string, TReturnValue = unknown>(
value: TValue,
lookup: Record<TValue, TReturnValue | ((...args: any[]) => TReturnValue)>,
...args: any[]
): TReturnValue {
if (value in lookup) {
const returnValue = lookup[value]
return typeof returnValue === 'function' ? returnValue(...args) : returnValue
}
const error = new Error(
`Tried to handle "${value}" but there is no handler defined. Only defined handlers are: ${Object.keys(
lookup
)
.map(key => `"${key}"`)
.join(', ')}.`
)
if (Error.captureStackTrace) {
Error.captureStackTrace(error, match)
}
throw error
}