cond-flow
Version:
Elixir style cond for easy javascript control flow
14 lines (13 loc) • 569 B
JavaScript
// Manual type guard is necessary, see https://stackoverflow.com/questions/62500700/error-2349-when-dealing-with-union-type-of-generic-t-and-function
function isFunction(value) {
return typeof value === "function";
}
function processMatch(match) {
return isFunction(match) ? match() : match;
}
const cond = (pairs, options) => {
const found = pairs.find(([predicate]) => predicate);
const match = found === undefined ? options === null || options === void 0 ? void 0 : options.default : found[1];
return processMatch(match);
};
export default cond;