import * as t from 'io-ts' import * as E from 'fp-ts/lib/Either' import * as O from 'fp-ts/lib/Option' import { pipe } from 'fp-ts/lib/pipeable' import { isOption, opTraverse } from '@monorail/sharedHelpers/fp-ts-ext/Option' /** * Codec for converting null or undefined members to/from Option * @param typeC */ export const optionC = ( typeC: t.Type, ): t.Type, TO | undefined | null, unknown> => new t.Type, TO | undefined | null, unknown>( `Option<${typeC.name}>`, (input: unknown): input is O.Option => isOption(input) && pipe( input, O.fold(() => true, typeC.is), ), (input, context) => pipe(O.fromNullable(input), opTraverse(E.either)(typeC.decode)), O.fold(() => null, typeC.encode), )