MCPcopy Create free account
hub / github.com/Effect-TS/effect / Option

Function Option

packages/effect/src/Schema.ts:16041–16083  ·  view source on GitHub ↗
(value: A)

Source from the content-addressed store, hash-verified

16039 * @since 3.10.0
16040 */
16041export function Option<A extends Constraint>(value: A): Option<A> {
16042 const schema = declareConstructor<
16043 Option_.Option<A["Type"]>,
16044 Option_.Option<A["Encoded"]>,
16045 OptionIso<A>
16046 >()(
16047 [value],
16048 ([value]) => (input, ast, options) => {
16049 if (Option_.isOption(input)) {
16050 if (Option_.isNone(input)) {
16051 return Effect.succeedNone
16052 }
16053 return Effect.mapBothEager(
16054 SchemaParser.decodeUnknownEffect(value)(input.value, options),
16055 {
16056 onSuccess: Option_.some,
16057 onFailure: (issue) => SchemaIssue.makeCompositeAtKey(ast, "value", issue, input, options)
16058 }
16059 )
16060 }
16061 return Effect.fail(new SchemaIssue.InvalidType(ast, input, options))
16062 },
16063 {
16064 representation: {
16065 id: "effect/schema/Option",
16066 payload: null
16067 },
16068 expected: "Option",
16069 toCodec: ([value]) =>
16070 link<Option_.Option<A["Encoded"]>>()(
16071 Union([
16072 Struct({ _tag: Literal("Some"), value }),
16073 Struct({ _tag: Literal("None") })
16074 ]),
16075 SchemaTransformation.transform({
16076 decode: (e) => e._tag === "None" ? Option_.none() : Option_.some(e.value),
16077 encode: (o) => (Option_.isSome(o) ? { _tag: "Some", value: o.value } as const : { _tag: "None" } as const)
16078 })
16079 )
16080 }
16081 )
16082 return make(schema.ast, { value })
16083}
16084/**
16085 * Type-level representation returned by {@link OptionFromNullOr}.
16086 *

Callers 6

OptionFromNullOrFunction · 0.70
OptionFromUndefinedOrFunction · 0.70
OptionFromNullishOrFunction · 0.70
OptionFromOptionalKeyFunction · 0.70
OptionFromOptionalFunction · 0.70
OptionFromOptionalNullOrFunction · 0.70

Calls 9

someMethod · 0.80
declareConstructorFunction · 0.70
linkFunction · 0.70
UnionFunction · 0.70
StructFunction · 0.70
LiteralFunction · 0.70
makeFunction · 0.70
failMethod · 0.65
transformMethod · 0.65

Tested by

no test coverage detected