/** * Represents a conversion from an value that can be undefined to an Optional type * * @since 0.0.4 */ import { Kind, Kind2, URIS, URIS2, HKT2 } from 'fp-ts/HKT' import * as D from 'io-ts/Decoder' import * as Enc from 'io-ts/Encoder' import * as Eq_ from 'fp-ts/Eq' import * as E from 'fp-ts/Either' import * as fc from 'fast-check' import * as TE from 'fp-ts/TaskEither' import * as G from 'io-ts/Guard' import * as TD from 'io-ts/TaskDecoder' import * as t from 'io-ts/Type' import * as O from 'fp-ts/Option' import * as Arb from '../internal/ArbitraryBase' import { flow, pipe } from 'fp-ts/function' /** * @since 0.0.4 * @category Model */ export type SchemableParams = ( sa: HKT2 ) => HKT2> /** * @since 0.0.4 * @category Model */ export type SchemableParams1 = (sa: Kind) => Kind> /** * @since 0.0.4 * @category Model */ export type SchemableParams2 = ( sa: Kind2 ) => Kind2> /** * @since 0.0.4 * @category Model */ export type SchemableParams2C = ( sa: Kind2 ) => Kind2> /** * @since 0.0.4 * @category Instances */ export const Decoder: SchemableParams2C = decoderA => ({ decode: u => u === undefined ? D.success(O.none) : pipe(decoderA.decode(u), E.map(O.some)), }) /** * @since 0.0.4 * @category Instances */ export const Encoder: SchemableParams2 = encoderA => ({ encode: flow(O.map(encoderA.encode), O.toUndefined), }) /** * @since 0.0.4 * @category Instances */ export const Eq: SchemableParams1 = O.getEq /** * @since 0.0.4 * @category Instances */ export const Guard: SchemableParams1 = guardA => G.union( G.struct({ _tag: G.literal('None'), }), G.struct({ _tag: G.literal('Some'), value: guardA, }) ) /** * @since 0.0.4 * @category Instances */ export const TaskDecoder: SchemableParams2C = taskDecoderA => ({ decode: u => u === undefined ? TD.success(O.none) : pipe(taskDecoderA.decode(u), TE.map(O.some)), }) /** * @since 0.0.4 * @category Instances */ export const Type: SchemableParams1 = typeA => t.union( t.struct({ _tag: t.literal('None'), }), t.struct({ _tag: t.literal('Some'), value: typeA, }) ) /** * @since 0.0.4 * @category Instances */ export const Arbitrary: SchemableParams1 = arbA => fc.oneof( Arb.struct({ _tag: Arb.literal('None') }), Arb.struct({ _tag: Arb.literal('Some'), value: arbA }) )