Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/optio/mod.ts

Minimum option type port of Rust
Latest
import * as optio from "https://deno.land/x/optio@1.0.0/mod.ts";

Optional values. Type Option represents an optional value: every Option is either Some and contains a value, or None, and does not.

Operators overview

Option provides a wide variety of different operators.

Querying the variant

The isSome and isNone return true if the Option is Some or None, respectively.

Extracting the contained value

Extract the contained value in an Option<T> when it is the Some.

If the Option is None:

  • expect throws with a provided custom message
  • unwrap throws with generic message
  • unwrapOr returns the provided default value
  • unwrapOrElse returns the result of evaluating the provided function

Transforming contained values

Transform the Some:

  • filter calls the provided predicate function on the contained value T if the Option is Some<T>, and returns Some<T> if the function returns true; otherwise, returns None
  • flat removes one level of nesting from an Option<Option<T>>
  • map transforms Option<T> to Option<U> by applying the provided function to the contained value of Some and leaving None values unchanged

Transform Option<T> to a value of a possibly different type U:

  • mapOr applies the provided function to the contained value of Some, or returns the provided default value if the Option is None
  • mapOrElse applies the provided function to the contained value of Some, or returns the result of evaluating the provided fallback function if the Option is None

Combine the Some of two Option values:

  • zip returns Some<[T, | U]> if option is Some<T> and the provided Option value is Some<U>; otherwise, returns None

Logical operators

Treat the Option as a boolean value, where Some acts like true and None acts like false.

The and, or, and xor take another Option, and produce an Option as output. Only the and can produce an Option<U> value having a different inner type U than Option<T>.

name option input output
and None - None
and Some<T> None None
and Some<T> Some<U> Some<U>
or None None None
or None Some<U> Some<U>
or Some<T> - Some<T>
xor None None None
xor None Some<U> Some<U>
xor Some<T> None Some<T>
xor Some<T> Some<U> None

The andThen and orElse take a function as input, and only evaluate the function when they need to produce a new value. Only the andThen can produce an Option<U> value having a different inner type U than Option<T>.

name option function input function result output
andThen None - - None
andThen Some<T> T None None
andThen Some<T> T Some<U> Some<U>
orElse None - None None
orElse None - Some<U> Some<U>
orElse Some<T> - - Some<T>

Enums

The option type.

Variables

No value.

Some constructor.

Functions

Returns None if the option is None, otherwise returns optb.

Returns None if the option is None, otherwise calls fn with the wrapped value and returns the result.

Returns the contained Some value.

Returns None if the option is None, otherwise calls predicate with the wrapped value and returns:

  • Some if predicate returns true.
  • None if predicate returns false.

Converts from Option<Option<T>> to Option<T>.

Returns true if the option is a None.

Returns true if the option is a Some.

Maps an Option<T> to Option<U> by applying a function to a contained value(if Some) or returns None(if None).

Returns the provided default value (if None), or applies a function to the contained value (if Some).

Computes a default function result (if None), or applies a different function to the contained value (if Some).

Pattern matching for option. Match on matcher.Some if Some, otherwise match on matcher.None.

Returns the option if it contains a value, otherwise returns obtb.

Returns the option if it Some, otherwise calls fn and returns the result.

Returns the contained Some value.

Returns the contained Some value, otherwise defaultValue.

Returns the contained Some value, otherwise computes it from a closure.

Returns Some if exactly one of option, optb is Some, otherwise returns None.

Zips option with another Option.

Interfaces

Option matcher.

The None API.

The Some API.

Type Aliases

Representation of Some or None.