Have ideas to improve npm?Join in the discussion! »

    @pacote/option
    TypeScript icon, indicating that this package has built-in type declarations

    2.0.1 • Public • Published

    @pacote/option

    version minified minified + gzip

    Option type inspired by Rust. It represents an optional value, an Option is either Some concrete value or it is None.

    Installation

    yarn add @pacote/option

    Usage

    import { None, Some, map } from '@pacote/option'
    import { pipe } from '@pacote/pipe'
    
    function divide(numerator: number, denominator: number): Option<number> {
      return denominator === 0 : None ? Some(numerator / denominator)
    }
    
    pipe(divide(4, 2), map(n => n + 1)) // => Some(3)
    pipe(divide(4, 0), map(n => n + 1)) // => None

    Type

    Option<T> = None | Some<T>

    Constructors

    None: None

    None represents no value.

    Some<T>(value: T): Some<T>

    Some() represents a value of type T.

    Functions

    tryCatch<T>(fn: () => T): Option<T>

    Creates a new instance of Option based on a function call. If the function throws an error, it returns None. Otherwise, it returns the result value in a Some.

    ofNullable<T>(value?: T | null): Option<T>

    Creates a new instance of Option based on the value being passed. If null or undefined, it returns None. Otherwise, it returns Some(value).

    isSome<T>(option: Option<T>): boolean

    Returns true if the passed option is a Some. Otherwise, it returns false.

    isNone<T>(option: Option<T>): boolean

    Returns true if the passed option is a None. Otherwise, it returns false.

    contains<T>(match: T): (option: Option<T>) => boolean

    Returns true if the Some value is the same as match. Otherwise, it returns false.

    flatten<T>(option: Option<Option<T>>): Option<T>

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

    getOrElse<T>(onNone: () => T): (option: Option<T>) => T

    Returns the value contained in the option. If the option is None, it evaluates the provided function for an alternative.

    map<T, U>(fn: (value: T) => U): (option: Option<T>) => Option<U>

    Maps an Option<T> to Option<U> by applying a function to a contained value.

    flatMap<T, U>(fn: (value: T) => Option<U>): (option: Option<T>) => Option<U>

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

    fold<T, U>(onSome: (value: T) => U, onNone: () => U): (option: Option<T>) => U

    Applies the onSome function to the contained value, otherwise it computes a default using onNone.

    filter<T>(predicate: (value: T) => boolean): (option: Option<T>) => Option<T>

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

    • Some(T) if predicate returns true, and
    • None if predicate returns false.

    or<T>(alternative: Option<T>): (option: Option<T>) => Option<T>

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

    and<T>(alternative: Option<T>): (option: Option<T>) => Option<T>

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

    License

    MIT © Luís Rodrigues.

    Install

    npm i @pacote/option

    DownloadsWeekly Downloads

    8

    Version

    2.0.1

    License

    MIT

    Unpacked Size

    21.8 kB

    Total Files

    10

    Last publish

    Collaborators

    • avatar