(value: A)
| 9609 | * @since 3.10.0 |
| 9610 | */ |
| 9611 | export function Option<A extends Constraint>(value: A): Option<A> { |
| 9612 | const schema = declareConstructor< |
| 9613 | Option_.Option<A["Type"]>, |
| 9614 | Option_.Option<A["Encoded"]>, |
| 9615 | OptionIso<A> |
| 9616 | >()( |
| 9617 | [value], |
| 9618 | ([value]) => (input, ast, options) => { |
| 9619 | if (Option_.isOption(input)) { |
| 9620 | if (Option_.isNone(input)) { |
| 9621 | return Effect.succeedNone |
| 9622 | } |
| 9623 | return Effect.mapBothEager( |
| 9624 | SchemaParser.decodeUnknownEffect(value)(input.value, options), |
| 9625 | { |
| 9626 | onSuccess: Option_.some, |
| 9627 | onFailure: (issue) => SchemaIssue.makeCompositeAtKey(ast, "value", issue, input, options) |
| 9628 | } |
| 9629 | ) |
| 9630 | } |
| 9631 | return Effect.fail(new SchemaIssue.InvalidType(ast, input, options)) |
| 9632 | }, |
| 9633 | { |
| 9634 | representation: { |
| 9635 | id: "effect/schema/Option", |
| 9636 | payload: null |
| 9637 | }, |
| 9638 | toCode: ({ typeParameters }) => ({ |
| 9639 | runtime: `Schema.Option(${typeParameters[0].runtime})`, |
| 9640 | Type: `Option.Option<${typeParameters[0].Type}>`, |
| 9641 | importDeclarations: [`import * as Option from "effect/Option"`] |
| 9642 | }), |
| 9643 | expected: "Option", |
| 9644 | toCodec: ([value]) => |
| 9645 | link<Option_.Option<A["Encoded"]>>()( |
| 9646 | Union([ |
| 9647 | Struct({ _tag: Literal("Some"), value }), |
| 9648 | Struct({ _tag: Literal("None") }) |
| 9649 | ]), |
| 9650 | SchemaTransformation.transform({ |
| 9651 | decode: (e) => e._tag === "None" ? Option_.none() : Option_.some(e.value), |
| 9652 | encode: (o) => (Option_.isSome(o) ? { _tag: "Some", value: o.value } as const : { _tag: "None" } as const) |
| 9653 | }) |
| 9654 | ), |
| 9655 | toArbitrary: ([value]) => (fc, ctx) => { |
| 9656 | const terminal = fc.constant(Option_.none()) |
| 9657 | const arbitrary = fc.oneof( |
| 9658 | terminal, |
| 9659 | value.arbitrary.map(Option_.some) |
| 9660 | ) |
| 9661 | return withRecursion(fc, ctx, terminal, arbitrary) |
| 9662 | }, |
| 9663 | toEquivalence: ([value]) => Option_.makeEquivalence(value), |
| 9664 | toFormatter: ([value]) => |
| 9665 | Option_.match({ |
| 9666 | onNone: () => "none()", |
| 9667 | onSome: (t) => `some(${value(t)})` |
| 9668 | }) |
no test coverage detected