GitXplorerGitXplorer
s

opty

public
8 stars
3 forks
0 issues

Commits

List of commits on branch master.
Unverified
d7f7bdf4e59d208a0692592dd91cb3e8ce05a51f

Merge pull request #1 from pyrho/master

ss-panferov committed 8 years ago
Unverified
fefe41d2560579c6382e6e9176428b9c16796d08

Fixed compilation error due to unreachable code with tsc 1.8.2

ddashlanebot committed 9 years ago
Unverified
c872125107962b713ab93640832ee3796d31ae1d

chore(ver): 0.1.3

ss-panferov committed 10 years ago
Unverified
4638727177cb1edc947b773f5a8a25935725f06f

docs(readme): update readme

ss-panferov committed 10 years ago
Unverified
2e6155aa96c19e67b55a8faa59c90561ce5d752e

chore(ver): 0.1.1

ss-panferov committed 10 years ago
Unverified
c4b460dfa91c0a0f599f948e7a79206b500fde9c

docs(license): add license file

ss-panferov committed 10 years ago

README

The README file for this repository.

Opty

This library contains Option and Result types ported from Rust. You can use it for JavaScript and TypeScript projects to create null-safe applications.

Usage

npm install --save opty
var opty = require("opty");

Require .d.ts for typescript:

/// <reference path="./node_modules/opty/index.d.ts" />
import opty = require("opty");

Interfaces

Option

interface Option<T> {
    /**
     * Returns true if the option is a Some value
     */
    isSome(): boolean;

    /**
     * Returns true if the option is a None value
     */
    isNone(): boolean;

    /**
     * Returns check result if the option is a Some value
     */
    isSomeAnd(fn: (a: T) => boolean): boolean;

    /**
     * Returns check result if the option is a None value
     */
    isNoneAnd(fn: () => boolean): boolean;

    /**
     * Returns the inner T of a Some(T). Throws an exception if the self value equals None.
     */
    unwrap(): T;

    /**
     * Returns the contained value or a default.
     * @param def
     */
    unwrapOr(def: T): T;

    /**
     * Returns the contained value or computes it from a closure.
     * @param f
     */
    unwrapOrElse(f: () => T): T;

    /**
     * Maps an Option<T> to Option<U> by applying a function to a contained value
     * @param f
     */
    map<U>(f: (a: T) => U): Option<U>;

    /**
     * Applies a function to the contained value or returns a default.
     * @param def
     * @param f
     */
    mapOr<U>(def: U, f: (a: T) => U): U;

    /**
     * Applies a function to the contained value or computes a default.
     * @param def
     * @param f
     */
    mapOrElse<U>(def: () => U, f: (a: T) => U): U;

    /**
     * Transforms the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(err).
     * @param err
     */
    okOr<E>(err: E): Result<T, E>;

    /**
     * Transforms the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(err()).
     * @param err
     */
    okOrElse<E>(err: () => E): Result<T, E>;

    /**
     * Returns None if the option is None, otherwise returns optb.
     * @param optb
     */
    and<U>(optb: Option<U>): Option<U>;

    /**
     * Returns None if the option is None, otherwise calls f with the wrapped value and returns the result.
     * Some languages call this operation flatmap.
     * @param f
     */
    andThen<U>(f: (a: T) => Option<U>): Option<U>;

    /**
     * Returns the option if it contains a value, otherwise returns optb.
     * @param optb
     */
    or(optb: Option<T>): Option<T>;

    /**
     * Returns the option if it contains a value, otherwise calls f and returns the result.
     * @param f
     */
    orElse(f: () => Option<T>): Option<T>;
}

Result

export interface Result<T, E> {
    /**
     * Returns true if the result is Ok
     */
    isOk(): boolean;

    /**
     * Returns true if the result is Err
     */
    isErr(): boolean;

    /**
     * Convert from Result<T, E> to Option<T>
     * Converts self into an Option<T>, and discarding the error, if any.
     */
    ok(): Option<T>;

    /**
     * Convert from Result<T, E> to Option<E>
     * Converts self into an Option<E>, and discarding the value, if any.
     */
    err(): Option<E>;

    /**
     * Maps a Result<T, E> to Result<U, E> by applying a function to an contained Ok value, leaving an Err value untouched.
     * This function can be used to compose the results of two functions.
     * @param fn
     */
    map<U>(fn: (a: T) => U): Result<U, E>;

    /**
     * Maps a Result<T, E> to Result<T, F> by applying a function to an contained Err value, leaving an Ok value untouched.
     * This function can be used to pass through a successful result while handling an error.
     * @param fn
     */
    mapErr<U>(fn: (a: E) => U): Result<T, U>;

    /**
     * Returns res if the result is Ok, otherwise returns the Err value of self.
     * @param res
     */
    and<U>(res: Result<U,E>): Result<U,E>;

    /**
     * Calls op if the result is Ok, otherwise returns the Err value of self.
     * This function can be used for control flow based on result values.
     * @param op
     */
    andThen<U>(op: (a: T) => Result<U,E>): Result<U,E>;

    /**
     * Returns res if the result is Err, otherwise returns the Ok value of self.
     * @param res
     */
    or(res: Result<T,E>): Result<T,E>;

    /**
     * Calls op if the result is Err, otherwise returns the Ok value of self.
     * This function can be used for control flow based on result values.
     * @param op
     */
    orElse<U>(op: (a: E) => Result<T,U>): Result<T,U>;

    /**
     * Unwraps a result, yielding the content of an Ok.
     */
    unwrap(): T;

    /**
     * Unwraps a result, yielding the content of an Ok. Else it returns optb.
     */
    unwrapOr(optb: T): T;

    /**
     * Unwraps a result, yielding the content of an Err.
     * @param op
     */
    unwrapOrElse(op: (u: E) => T): T
}