/** * @since 1.0.0 */ import * as t from 'io-ts' import * as O from 'fp-ts/Option' import * as E from 'fp-ts/Either' import { pipe } from 'fp-ts/function' /** * @since 1.0.0 */ export default function decode(decoder?: t.Decoder | undefined) { return function ( input: E.Either> ): E.Either> { return decoder ? pipe( input, E.chainW((data) => { if (O.isNone(data)) { return E.right(data) } const v = decoder.decode(data.value) return v._tag === 'Right' ? E.right(O.some(v.right)) : E.left(v.left) }) ) : input } }