/** * The match module provides a best effort pattern runtime pattern matching * framework for ECMAScript. * * Example: * ```ts * * let r:string = match(window.global) * .caseOf(1, (_:number) => 'one') * .caseOf('one', (n:string) => n) * .orElse(()=> 'N/A') * .end(); * * ``` * This framework uses the data/type#test function to do the actual * pattern matching and attention must be paid to the rules of that * function to avoid unexpected errors. * * Great effort was made to try and make the `caseOf` methods as * type safe as possible however it is still possible to evade the compiler * especially when the first argument is a shape (object with keys describing * allowed types). * */ import { Constructor } from '../../data/type/constructor'; /** * Result is the sum of the UnMatched and Matched types. */ export type Result = UnMatched | Matched; /** * UnMatched represents a value yet to have a successful match. */ export declare class UnMatched { value: A; constructor(value: A); /** * caseOf test. */ caseOf(pattern: Constructor, f: (value: T) => B): Result; caseOf(pattern: String, f: (value: string) => B): Result; caseOf(pattern: Number, f: (value: number) => B): Result; caseOf(pattern: Boolean, f: (value: boolean) => B): Result; caseOf(pattern: T, f: (value: { [P in keyof T]: any; }) => B): Result; caseOf(pattern: T, f: (value: T) => B): Result; caseOf(pattern: T, f: (value: T) => B): Result; caseOf(pattern: T, f: (value: T) => B): Result; /** * orElse produces the alternative value since no cases have been matched yet. */ orElse(f: (a: A) => B): Matched; /** * end * * Calling end on an UnMatched is an error. */ end(): A; } /** * Matched represents a succefully matched case. */ export declare class Matched { value: A; constructor(value: A); /** * caseOf does nothing. */ caseOf(pattern: Constructor, f: (value: T) => B): Result; caseOf(pattern: String, f: (value: string) => B): Result; caseOf(pattern: Number, f: (value: number) => B): Result; caseOf(pattern: Boolean, f: (value: boolean) => B): Result; caseOf(pattern: T, f: (value: { [P in keyof T]: any; }) => B): Result; caseOf(pattern: T, f: (value: T) => B): Result; caseOf(pattern: T, f: (value: T) => B): Result; caseOf(pattern: T, f: (value: T) => B): Result; /** * orElse does nothing. */ orElse(_: (a: A) => B): Matched; /** * end produces the value the Matched was created with. */ end(): A; } /** * match wraps a value in an UnMatched so that case tests can be applied. */ export declare const match: (value: A) => Result;