@typed/io
Version:
Runtime IO type system
16 lines • 631 B
JavaScript
import { fromRight, isRight } from '@typed/either';
import { toString } from '@typed/strings';
import { catchDecodeFailure, DecodeError, decodeFailure } from './Decoder';
export const union = (decoders, expected = decoders.map((d) => d.expected).join(' | ')) => ({
expected,
*decode(i) {
for (const { decode } of decoders) {
const either = yield* catchDecodeFailure(decode(i));
if (isRight(either)) {
return fromRight(either);
}
}
return yield* decodeFailure(DecodeError.create(expected, toString(i)));
},
});
//# sourceMappingURL=Union.js.map