import { Either } from "./either"; /** * The Option abstract class. * @deprecated */ export abstract class Option { abstract getOrElse (f: () => T): T; abstract getOrElseGet (value: T): T; abstract getOrThrow (err: Error): T; abstract orElse (o: () => Option): Option; abstract toObject (): {some: T | null}; /** * Returns that this is None. * returns {boolean} */ public isDefined (): boolean { return false; } /** * Returns that this is Some. * @returns {boolean} */ public isEmpty (): boolean { return false; } /** * Throws a ReferenceError. */ public get (): T { throw new ReferenceError("This is option is None.") } /** * Returns a Either.Left. * @returns {Either.Left} */ public toEither (): Either { return Either.left(new ReferenceError("This either is Left.")); } /** * Tests equality of Options. * @param {Option} other - the other Option to test * @returns {boolean} */ public equals (other: Option): boolean { if (! other || other instanceof Option === false) return false; if (this === other) return true; if (this.isDefined() === false && other.isDefined() === false) return true; return this.isDefined() === other.isDefined() && this.get() === other.get(); } /** * Returns the Option as a JSON object. * @returns {{some: T | null}} */ public toJSON (): {some: T | null} { return this.toObject(); } /** * Returns the Option as a string. * @returns {string} '{"some": T | null}' */ public toString (): string { return JSON.stringify(this.toJSON()); } } /** * The Option namespace. * @deprecated */ export namespace Option { /** * Returns a new Option.Some instance. * @returns {Option.some} */ export function some (value: T) { return new Some(value); } /** * Returns a new Option.None instance. * @returns {Option.None} */ export function none () { return new None(); } /** * Returns the singleton instance of Option.None. * @returns {Option.None} */ export function nothing () { return nothingOption; } /** * Lifts the given partialFunction into a total function that returns an Option result. * Basically, wraps the function in try/catch and returns Option.Some() or Option.None(). * @param {(...args: any[]) => T} partialFunction - the function to lift * @returns (...args: any[]) => Option */ export function lift (partialFunction: (...args: any[]) => T): (...args: any[]) => Option { return (...args: any[]) => { try { return Option.some(partialFunction.apply(partialFunction, args) as T); } catch (err) { return Option.none(); } }; } /** * The Option.None class. * @deprecated */ export class None extends Option { constructor () { super(); } /** * Returns that this option is empty. * @returns {boolean} */ public isEmpty () { return true; } /** * Returns the evaluated given function. * @param {() => T} f - the or else function to evaluate * @returns {T} */ public getOrElse (f: () => T): T { return f(); } /** * Returns the T value. * @param {T} value - the or else value * @returns {T} */ public getOrElseGet (value: T): T { return value; } /** * Throws a ReferenceError or the given Error. * @param {Error} [err] - the optional Error to throw * @returns {T} */ public getOrThrow (err?: Error): T { throw err || new ReferenceError("This option is None.") } /** * Returns the evaluated function. * @param {() => Option} f - the or else function to evaluate * @returns {Option} */ public orElse (f: () => Option): Option { return f(); } /** * Returns an Either.Left. */ public toEither () { return Either.left(new ReferenceError("This either is Left.")); } /** * Returns the Option as a plain-old JS object. * @returns {{some: null}} */ public toObject (): {some: null} { return { some: null }; } } /** * The Option.Some class. * @deprecated */ export class Some extends Option { constructor (private value: T) { super(); } /** * Returns that this option is Option.Some. * @returns {boolean} */ public isDefined () { return true; } /** * Returns the Some value. * @returns {T} */ public get (): T { return this.value; } /** * Returns the Some value. * @param {() => T} f - the function to evaluate if Left * @returns {T} */ public getOrElse (value: () => T): T { return this.value; } /** * Returns the Some value. * @param {T} value - the value to return if None * @return {T} */ public getOrElseGet (value: T): T { return this.value; } /** * Returns the Some value. * @returns {T} */ public getOrThrow (err: Error) { return this.value; } /** * Returns this Some value. * @param {Option} f - the function to evaluate if Left */ public orElse (o: () => Option): Option { return this; } /** * Returns an Either.Right. * @returns {Either.Right} */ public toEither () { return Either.right(this.value); } /** * Returns the Option as a plain-old JS object. * @returns {{some: T}} */ public toObject (): {some: T} { return { some: this.value }; } } const nothingOption = new None(); }