import { O } from "https://deno.land/x/ahh@v0.10.0/src/option/mod.ts";
Functionality for Option
.
Static Methods
and<T, U>(opt: Option<T>, other: Option<U>): Option<U>
Return other
if opt
is Some
, or
None
.
Examples
import { O, None } from "./mod.ts";
console.log(O.and(1, 2)); // 2
console.log(O.and(None, 2)); // None
console.log(O.and(2, None)); // None
console.log(O.and(None, None)); // None
contains<T>(opt: Option<T>, value: T): boolean
Returns whether opt
strictly equals value
, or
false
if it is an None
.
Examples
import { O, None } from "./mod.ts";
console.log(O.contains(1, 1)); // true
console.log(O.contains(1, 2)); // false
console.log(O.contains(None, 1)); // false
filter<T>(opt: Option<T>, f: (_: T) => boolean): Option<T>
Calls f
with opt
, and returns it if the result is
true
.
Examples
import { O, None } from "./mod.ts";
console.log(O.filter(2, (i) => i % 2 === 0)); // 2
console.log(O.filter(1, (i) => i % 2 === 0)); // None
console.log(O.filter(None, (i: number) => i % 2 === 0)); // None
inspect<T>(opt: Option<T>, f: (_: T) => unknown): Option<T>
Calls f
with opt
, and returns the original
opt
.
Examples
import { O, None } from "./mod.ts";
console.log(O.inspect(1, (i) => i + 1)); // 1
console.log(O.inspect(None, (i: number) => i + 1)); // undefined
isNone<T>(opt: Option<T>): opt is None
Returns whether opt
is a None
.
Examples
import { O, None } from "./mod.ts";
console.log(O.isNone(1)); // false
console.log(O.isNone(None)); // true
isSome<T>(opt: Option<T>): opt is Some<T>
Returns whether opt
is a Some
.
Examples
import { O, None } from "./mod.ts";
console.log(O.isSome(1)); // true
console.log(O.isSome(None)); // false
map<T, U>(opt: Option<T>, f: (_: T) => U): Option<U>
Calls f
with opt
, and returns the result.
Examples
import { O, None } from "./mod.ts";
console.log(O.map(1, (i) => i + 1)); // 2
console.log(O.map(None, (i: number) => i + 1)); // undefined
okOr<T, E extends Error>(opt: Option<T>, err: E): Result<T, E>
Converts opt
into an Ok
if it is a
Some
.
Examples
import { O, None } from "./mod.ts";
console.log(O.okOr(1, Error())); // 1
console.log(O.okOr(None, Error())); // Error()
or<T>(opt: Option<T>, other: Option<T>): Option<T>
Return opt
if it is a Some
, or other
.
Examples
import { O, None } from "./mod.ts";
console.log(O.or(1, 2)); // 1
console.log(O.or(None, 2)); // 2
console.log(O.or(2, None)); // 2
console.log(O.or(None, None)); // undefined
unwrap<T>(opt: Option<T>): opt extends Some<T> ? T : never
Returns opt
if it is a Some
, or throws.
Examples
import { O, None } from "./mod.ts";
console.log(O.unwrap(1)); // 1
O.unwrap(None); // throws
unwrapOr<T>(opt: Option<T>, default_: T): T
Returns opt
if it is a Some
, or returns
default_
.
Examples
import { O, None } from "./mod.ts";
console.log(O.unwrapOr(1, 2)); // 1
console.log(O.unwrapOr(None, 2)); // 2