(value: A)
| 16039 | * @since 3.10.0 |
| 16040 | */ |
| 16041 | export 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 | * |
no test coverage detected