Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Just<T>

A Just instance is the present variant instance of the Maybe type, representing the presence of a value which may be absent. For a full discussion, see the module docs.

Type parameters

  • T

    The type wrapped in this Just variant of Maybe.

Hierarchy

  • Just

Implements

Index

Constructors

constructor

  • new Just(value?: T | null): Just
  • Create an instance of Maybe.Just with new.

    note

    While you may create the Just type via normal JavaScript class construction, it is not recommended for the functional style for which the library is intended. Instead, use Maybe.of (for the general case) or Maybe.just for this specific case.

    // Avoid:
    const aString = new Maybe.Just('characters');
    
    // Prefer:
    const aString = Maybe.just('characters);
    throws

    If you pass null or undefined.

    Parameters

    • Optional value: T | null

      The value to wrap in a Maybe.Just.

      null and undefined are allowed by the type signature so that the constructor may throw on those rather than constructing a type like Maybe<undefined>.

    Returns Just

Properties

value

value: T

The wrapped value.

variant

variant: Just = Variant.Just

Just is always Variant.Just.

Methods

and

andThen

  • andThen<U>(this: Maybe<T>, andThenFn: function): Maybe<U>

ap

chain

  • chain<U>(this: Maybe<T>, chainFn: function): Maybe<U>

equals

  • equals(this: Maybe<T>, comparison: Maybe<T>): boolean

flatMap

  • flatMap<U>(this: Maybe<T>, flatMapFn: function): Maybe<U>

get

  • get<K>(this: Maybe<T>, key: K): Maybe<NonNullable<T[K]>>
  • Method variant for Maybe.get

    If you have a Maybe of an object type, you can do thatMaybe.get('a key') to look up the next layer down in the object.

    type DeepOptionalType = {
    something?: {
    with?: {
    deeperKeys?: string;
    }
    }
    };
    
    const fullySet: DeepType = {
    something: {
    with: {
    deeperKeys: 'like this'
    }
    }
    };
    
    const deepJust = Maybe.of(fullySet)
    .get('something')
    .get('with')
    .get('deeperKeys');
    
    console.log(deepJust); // Just('like this');
    
    const partiallyUnset: DeepType = { something: { } };
    
    const deepEmpty = Maybe.of(partiallyUnset)
    .get('something')
    .get('with')
    .get('deeperKeys');
    
    console.log(deepEmpty); // Nothing

    Type parameters

    • K: keyof T

    Parameters

    Returns Maybe<NonNullable<T[K]>>

isJust

  • isJust(this: Maybe<T>): this is Just<T>

isNothing

  • isNothing(this: Maybe<T>): this is Nothing<T>

map

  • map<U>(this: Maybe<T>, mapFn: function): Maybe<U>
  • Method variant for Maybe.map

    Type parameters

    • U

    Parameters

    • this: Maybe<T>
    • mapFn: function
        • (t: T): U
        • Parameters

          • t: T

          Returns U

    Returns Maybe<U>

mapOr

  • mapOr<U>(this: Maybe<T>, orU: U, mapFn: function): U
  • Method variant for Maybe.mapOr

    Type parameters

    • U

    Parameters

    • this: Maybe<T>
    • orU: U
    • mapFn: function
        • (t: T): U
        • Parameters

          • t: T

          Returns U

    Returns U

mapOrElse

  • mapOrElse<U>(this: Maybe<T>, orElseFn: function, mapFn: function): U
  • Method variant for Maybe.mapOrElse

    Type parameters

    • U

    Parameters

    • this: Maybe<T>
    • orElseFn: function
        • (): U
        • Returns U

    • mapFn: function
        • (t: T): U
        • Parameters

          • t: T

          Returns U

    Returns U

match

or

orElse

  • orElse(this: Maybe<T>, orElseFn: function): Maybe<T>

toJSON

toOkOrElseErr

  • toOkOrElseErr<E>(this: Maybe<T>, elseFn: function): Result<T, E>

toOkOrErr

toString

  • toString(this: Maybe<T>): string

unsafelyUnwrap

  • unsafelyUnwrap(): T

unwrapOr

  • unwrapOr(this: Maybe<T>, defaultValue: T): T

unwrapOrElse

  • unwrapOrElse(this: Maybe<T>, elseFn: function): T

Static unwrap

  • unwrap<J>(theJust: Just<J>): J
  • Unwrap the contained value. A convenience method for functional idioms.

    A common scenario where you might want to use this is in a pipeline of functions:

    import Maybe, { Just } from 'true-myth/maybe';
    
    function getLengths(maybeStrings: Array<Maybe<string>>): Array<number> {
    return maybeStrings
    .filter(Maybe.isJust)
    .map(Just.unwrap)
    .map(s => s.length);
    }

    Type parameters

    • J

    Parameters

    Returns J

Generated using TypeDoc