@reactory/switch-map
Version:
Easy objects switch maps in React.
32 lines (29 loc) • 1.02 kB
JavaScript
// TODO: implement createSwitchMap(...) to create switchMap with e.g.: the default fallback
function switchMap(expression, cases, options) {
if (typeof cases !== 'object' || cases === null) {
throw new TypeError(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`Cases must be an object, got: ${cases} (typeof === "${typeof cases}").`);
}
if (expression in cases) {
return cases[expression];
}
if ('default' in cases) {
return cases.default;
}
let hasFallback = false;
let fallback;
if (typeof options === 'object' && options !== null) {
if ('fallback' in options) {
hasFallback = true;
fallback = options.fallback;
}
}
if (Object.keys(cases).length === 0 && !hasFallback) {
throw new Error('At least a default case or a fallback must be provided.');
}
return fallback;
}
module.exports = switchMap;
//# sourceMappingURL=index.js.map
;