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:
import opty = require("opty");
Interfaces
Option
interface Option<T> {
isSome(): boolean;
isNone(): boolean;
isSomeAnd(fn: (a: T) => boolean): boolean;
isNoneAnd(fn: () => boolean): boolean;
unwrap(): T;
unwrapOr(def: T): T;
unwrapOrElse(f: () => T): T;
map<U>(f: (a: T) => U): Option<U>;
mapOr<U>(def: U, f: (a: T) => U): U;
mapOrElse<U>(def: () => U, f: (a: T) => U): U;
okOr<E>(err: E): Result<T, E>;
okOrElse<E>(err: () => E): Result<T, E>;
and<U>(optb: Option<U>): Option<U>;
andThen<U>(f: (a: T) => Option<U>): Option<U>;
or(optb: Option<T>): Option<T>;
orElse(f: () => Option<T>): Option<T>;
}
Result
export interface Result<T, E> {
isOk(): boolean;
isErr(): boolean;
ok(): Option<T>;
err(): Option<E>;
map<U>(fn: (a: T) => U): Result<U, E>;
mapErr<U>(fn: (a: E) => U): Result<T, U>;
and<U>(res: Result<U,E>): Result<U,E>;
andThen<U>(op: (a: T) => Result<U,E>): Result<U,E>;
or(res: Result<T,E>): Result<T,E>;
orElse<U>(op: (a: E) => Result<T,U>): Result<T,U>;
unwrap(): T;
unwrapOr(optb: T): T;
unwrapOrElse(op: (u: E) => T): T
}