ts_maybe
Nullability Helpers for TypeScript
Glossary
Usage
Installation:
npm install ts_maybe
Import
import { Maybe } from 'ts_maybe';
Create Maybe
const variable: number = 1;
Maybe.of(variable); //=> Just<number>(1)
const variable: number | undefined = undefined;
Maybe.of(variable); //=> Nothing
const variable: number | undefined = undefined;
const defaultValue: number = 0;
Maybe.of(variable, defaultValue); //=> Just<number>(0)
const variable: number | undefined = undefined;
const defaultValue: Maybe<number> = Maybe.of(5);
Maybe.of(variable, defaultValue); //=> Just<number>(5)
const variables: number[] = [ undefined, null, 2, 1];
Maybe.ofs(variables); //=> Just<number>(2)
#deprecated, save use Maybe.of
const variable: string = 'hello';
Just(variable); //=> Just<string>('hello')
#deprecated, save use Maybe.of
const variable: string | undefined = undefined;
Just(variable); //=> throws Error
Nothing(); //=> Nothing
Documentation
Functions on Maybe instances
MaybeInstance.map(f: (value: T) => R) => Maybe<R>
-
f
: Function for map T -> R
Returns Maybe[R]
MaybeInstance.fmap(f: (value: T) => Maybe<R>) => Maybe<R>
-
f
: Function for fmap T -> Maybe[R]
Returns Maybe[R]
MaybeInstance.isJust() => type is Just
Returns Type is Just
MaybeInstance.isNothing() => type is Nothing
Returns Type is Nothing
MaybeInstance.if(f: (value: T) => void) => Maybe<T>
-
f
: Function to compute with value if present
Returns Maybe[T]
MaybeInstance.else(f: () => void) => Maybe<T>
-
f
: Function to compute if no value is present
Returns Maybe[T]
MaybeInstance.reset(f: (value: T) => void) => Nothing<T>
-
f
: Function to compute with value if present, then reset to Nothing
Returns Nothing[T]
MaybeInstance.safe(f: () => T) => T
-
f
: Function to compute to create returnValue if no value is present
Returns [T]
MaybeInstance.unsafe() => T
Returns [T] (throws Error if no value is present)
MaybeInstance.unwrap() => T | undefined
Returns [T] if value is present or undefined
MaybeInstance.or(ifNothing: Maybe<T> | T) => Maybe<T>
-
ifNothing
: Value or Maybe if instance is Nothing
Returns Maybe[T]
Maybe.compare(left: Maybe<T>,right: Maybe<T>, comp: ((left: T, right: T) => boolean), onBothNothing?: boolean)
-
left
: first Maybe -
right
: right Maybe -
compo
: Function to be calculated for comparison -
onBothNothing
: boolean to return if both Maybe's are Nothing
Returns boolean