ts-cast
Version:
Runtime typechecking
13 lines (12 loc) • 412 B
JavaScript
import { withName } from '../helpers/names';
import { throwTypeError } from './throw-type-error';
const either = (caster, leftFactory, rightFactory) => withName((value, context) => {
try {
const casted = caster(value, context, throwTypeError);
return rightFactory(casted);
}
catch (err) {
return leftFactory(err);
}
}, `either<*, ${caster.name}>`);
export default either;