Hi there! Are you looking for the official Deno documentation? Try docs.deno.com for all your Deno learning needs.

Usage

import * as fun from "https://raw.githubusercontent.com/baetheus/fun/main/option.ts";

The Option type is generally considered functional programming's response to handling null or undefined. Sometimes Option is also called Maybe. Its purpose is to represent the possibility that some data is not available.

§Variables

ApplicableOption

The canonical implementation of Applicable for Option.

bind
bindTo
FilterableOption

The canonical implementation of Filterable for Option.

FlatmappableOption

The canonical implementation of Flatmappable for Option.

FoldableOption

The canonical implementation of Foldable for Option.

MappableOption

The canonical implementation of Mappable for Option.

none

The cannonical implementation of the None type. Since all None values are equivalent there is no reason to construct more than one object instance.

tap
TraversableOption

The canonical implementation of Traversable for Option.

WrappableOption

The canonical implementation of Wrappable for Option.

§Functions

alt

Replace an first with second if first is None. This allows one to offer a a replacement or default.

apply

Apply a value A wrapped in an option to a function (a: A) => I wrapped in an Option. If either the wrapped value or the wrapped function are None then the result is None, if they are both Some then the result is Some.

constNone

The constNone is a thunk that returns the canonical none instance.

exists

Apply a predicate to the inner value of an Option, returning true if the option is Some and the predicate returns true, otherwise returning false.

fail

Fail is an alias of constNone.

filter

Apply a refinement or predicate to the inner value of an Option, returning the original option if the value exists and the predicate/refinement return true, otherwise returning None.

filterMap

Apply a filter and mapping operation at the same time against an Option. This is equivalent to the flatmap function for Option.

flatmap

Apply a function (a: A) => Option to the wrapped value of an Option if the wrapped value exists, flattening the application result into an Option. This is the equivalent of first mapping from Option to Option<Option> and then calling join to flatten the Options.

fold

Reduce over an Option. Since an Option contains at most one value this function operates a lot like getOrElse. If the passed option is None then it returns the initial value, otherwise the foldr function is called with both the initial value and the inner A.

fromNullable

The fromNullable function takes a potentially null or undefined value and maps null or undefined to None and non-null and non-undefined values to Some<NonNullable>.

fromPredicate

The fromPredicate function will test the value a with the predicate. If the predicate evaluates to false then the function will return a None, otherwise it will return the value wrapped in Some.

getCombinableOption
getComparableOption

Create an instance of Comparable<Option> given an instance of Comparable.

getInitializableOption

Create an instance of Initializable<Option> given an instance of Initializable.

getOrElse

getOrElse operates like a simplified fold. One supplies a thunk that returns a default inner value of the Option for the cases where the option is None.

getShowableOption

Create an instance of Showable for Option given an instance of Showable for A.

getSortableOption

Create an instance of Sortable<Option> given an instance of Sortable.

init
isNone

Tests wether an Option is None, returning true if the passed option is None and false if it is Some.

isSome

Tests wether an Option is Some, returning true if the passed option is Some and false if it is None.

map

Apply the mapping function fai to the inner value of an Option if it exists. If the option is None then this function does nothing.

mapNullable

Apply a mapping function to an Option but if the mapping function returns null or undefined the null or undefined value is lifted into None.

match

The match functionis the standard catamorphism on an Option. It operates like a switch case operator over the two potential cases for an Option type. One supplies functions for handling the Some case and the None case with matching return types and fold calls the correct function for the given option.

partition

Given a refinement or predicate, return a function that splits an Option into a Pair<Option, Option>. Due to the nature of the option type this will always return Pair<Some, None>, Pair<None, None>, or Pair<None, Some>.

partitionMap

Map and partition over the inner value of an Option at the same time. If the option passed is None then the result is [None, None], otherwise Right will result in [Some, None], and Left will result in [None, Some].

some

The some constructer takes any value and wraps it in the Some type.

toNull

toNullable returns either null or the inner value of an Option. This is useful for interacting with code that handles null but has no concept of the Option type.

toUndefined

toUndefined returns either undefined or the inner value of an Option. This is useful for interacting with code that handles undefined but has no concept of the Option type.

traverse

Traverse over an Option using the supplied Applicable. This allows one to turn an Option into Kind<V, Option>.

tryCatch

Take a function that can throw and wrap it in a try/catch block. Returns a new function that takes the same arguments as the original but returns the original value wrapped in an Option. If the function throws then the new function returns None, otherwise it returns Some.

wrap

Create an Option by wrapping any value A in Some.

§Interfaces

KindOption

Specifies Option as a Higher Kinded Type, with covariant parameter A corresponding to the 0th index of any substitutions.

§Type Aliases

None

The None type represents the non-existence of a value.

Option

The Option represents a type A that may or may not exist. It's the functional progamming equivalent of A | undefined | null.

Some

The Some type represents the existence of a value.