/** Type used to represent either success or failure. */ // https://github.com/martinmroz/result/blob/master/src/index.ts export interface IResult { /** Returns the value if result is `Ok`, `undefined` if `Err`. */ ok(): T | undefined; /** Returns `true` if result is `Ok`, `false` if `Err`. */ isOk(): boolean; /** Returns the error if result is `Err`, `undefined` if `Ok`. */ err(): E | undefined; /** Returns `true` if result is `Err`, `false` if `Ok`. */ isErr(): boolean; /** * Maps an `IResult` to `IResult` by applying the function provided * to a contained `Ok` value, leaving an `Err` value untouched. */ map(op: (value: T) => U): IResult; /** * Maps an `IResult` to `IResult` by applying the function provided * to a contained `Err` value, leaving an `Ok` value untouched. */ mapErr(op: (error: E) => F): IResult; /** * Returns `res` if the receiver is `Ok`, otherwise returns the `Err` value of * the receiver. */ and(res: IResult): IResult; /** * Invokes `op` if the receiver is `Ok`, returning the result. * If the receiver is `Err`, returns the `Err` value of the receiver. */ andThen(op: (value: T) => IResult): IResult; /** * Unwraps the result, yielding the contents of the receiver if `Ok`. * Throws an exception if the receiver is an `Err`. */ unwrap(): T; /** * Unwraps the result, yielding the contents of the receiver if `Err`. * Throws an exception if the receiver is an `Ok`. */ unwrapErr(): E; /** * Unwraps a result, yielding the content if the receiver is `Ok`. * If the receiver is an `Err`, returns `outherwise`. */ unwrapOr(otherwise: T): T; /** * Unwraps a result, yielding the content if the receiver is `Ok`. * If the receiver is an `Err`, returns the result of invoking the parameter. */ unwrapOrElse(otherwise: (error: E) => T): T; } /** Contains the success value. */ export class Ok implements IResult { private value: T; constructor(v: T) { this.value = v; } ok(): T | undefined { return this.value; } isOk(): boolean { return true; } err(): E | undefined { return undefined; } isErr(): boolean { return false; } map(op: (value: T) => U): IResult { return new Ok( op(this.value) ); } mapErr(op: (error: E) => F): IResult { return new Ok( this.value ); } and(res: IResult): IResult { return res; } andThen(op: (value: T) => IResult): IResult { return op(this.value); } unwrap(): T { return this.value; } unwrapErr(): E { throw new Error('Attempting to unwrap error on Ok Result.'); } unwrapOr(otherwise: T): T { return this.value; } unwrapOrElse(otherwise: (error: E) => T): T { return this.value; } } /** Contains the error value. */ export class Err implements IResult { private value: E; constructor(v: E) { this.value = v; } ok(): T | undefined { return undefined; } isOk(): boolean { return false; } err(): E | undefined { return this.value; } isErr(): boolean { return true; } map(op: (value: T) => U): IResult { return new Err( this.value ); } mapErr(op: (error: E) => F): IResult { return new Err( op(this.value) ); } and(res: IResult): IResult { return new Err(this.value); } andThen(op: (value: T) => IResult): IResult { return new Err(this.value); } unwrap(): T { throw new Error('Attempting to unwrap Err Result.'); } unwrapErr(): E { return this.value; } unwrapOr(otherwise: T): T { return otherwise; } unwrapOrElse(otherwise: (error: E) => T): T { return otherwise(this.value); } }