Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/eitherway/lib/core/mod.ts>Option.fromFallible

Yet Another Option and Result Implementation - providing safe abstractions for fallible flows inspired by F# and Rust
Latest
function Option.fromFallible
import { Option } from "https://deno.land/x/eitherway@0.10.0/lib/core/mod.ts";
const { fromFallible } = Option;

Use this if instances of Error should be evaluated to None

Behaves like Option.from() but also returns None for instances of Error

Examples

Example 1

import { assert } from "./assert.ts";
import { Option, None, Some } from "./option.ts";

const str = "thing" as string | undefined;
const undef = undefined as string | undefined;
const err = new Error() as string | Error | TypeError;

const some: Option<string> = Option.fromFallible(str);
const none: Option<string> = Option.fromFallible(undef);
const alsoNone: Option<string> = Option.fromFallible(err);

assert(some instanceof Option === true);
assert(none instanceof Option === true);
assert(alsoNone instanceof Option === true);
assert(some.isSome() === true);
assert(none.isNone() === true);
assert(alsoNone.isNone() === true);