| 9869 | * @since 4.0.0 |
| 9870 | */ |
| 9871 | export function OptionFromOptionalNullOr<S extends Constraint>( |
| 9872 | schema: S, |
| 9873 | options?: { |
| 9874 | readonly onNoneEncoding: "omit" | null | undefined |
| 9875 | } |
| 9876 | ): OptionFromOptionalNullOr<S> { |
| 9877 | const onNoneEncoding = options === undefined ? "omit" : options.onNoneEncoding |
| 9878 | const noneValue = onNoneEncoding === null |
| 9879 | ? null as S["Type"] | null | undefined |
| 9880 | : undefined as S["Type"] | null | undefined |
| 9881 | return optional(NullOr(schema)).pipe(decodeTo( |
| 9882 | Option(toType(schema)), |
| 9883 | SchemaTransformation.transformOptional<Option_.Option<S["Type"]>, S["Type"] | null | undefined>({ |
| 9884 | decode: (oe) => oe.pipe(Option_.filter(Predicate.isNotNullish), Option_.some), |
| 9885 | encode: onNoneEncoding === "omit" |
| 9886 | ? Option_.flatten |
| 9887 | : (ot) => Option_.some(Option_.getOrElse(Option_.flatten(ot), () => noneValue)) |
| 9888 | }) |
| 9889 | )) |
| 9890 | } |
| 9891 | |
| 9892 | /** |
| 9893 | * Type-level representation returned by {@link Result}. |