import { any, arrayOf, boolean, deferred, dictionary, every, guard, instanceOf, isArray, isFunction, isNill, literal, literals, natural, number, object, Parser, partial, recursive, regex, shape, some, string, tuple, ValidatorError, } from "./parsers/index"; import { parserName } from "./parsers/named"; import { unknown } from "./parsers/simple-parsers"; export type { IParser, ParserNames } from "./parsers/interfaces"; export { Parser as Validator }; export type { ValidatorError }; export type Fn = (a: A) => B; export type ValueOrFunction = ((a: In) => Out) | Out; export type ParserOrLiteral = ExtendsSimple | Parser; export type Primative = string | number | boolean | null | undefined; export type ExtendsSimple = A extends string | number | boolean | null | undefined ? A : never; export type WhenArgsExclude = A extends [ValueOrFunction] ? T extends unknown ? any : T : A extends ( [...ParserOrLiteral[], ValueOrFunction] ) ? In : never; type _ = A; type ExcludePrimative = Exclude>>; type ValidWhenArg = | Primative | Parser | ValueOrFunction; export type WhenArgs = | [ValueOrFunction] | [...ParserOrLiteral[], ValueOrFunction]; interface _WhenFn { ( ...args: WhenArgs ): _, Out | B>>; } export type WhenFn = [In] extends [never] ? never : _WhenFn; export type WhenArgsOutput = A extends [ValueOrFunction] ? V : A extends ( [...ParserOrLiteral[], ValueOrFunction] ) ? Out : never; export type UnwrapFn = [In] extends [never] ? () => OutcomeType : never; export interface ChainMatches { when: WhenFn; defaultTo(value: B): B | OutcomeType; defaultToLazy(getValue: () => B): B | OutcomeType; unwrap: UnwrapFn; } class Matched implements ChainMatches { constructor(private value: OutcomeType) {} when: WhenFn = ((..._args: unknown[]) => { return this as unknown as any; }) as any; defaultTo(_defaultValue: B) { return this.value; } defaultToLazy(_getValue: () => B) { return this.value; } unwrap: UnwrapFn = ((): OutcomeType => { return this.value; }) as any; } class MatchMore implements ChainMatches { constructor(private a: unknown) {} when: WhenFn = ((...args: unknown[]) => { const [outcome, ...matchers] = args.reverse(); const me = this; const P = Parser; const parserAsString = Parser.parserAsString; const parser = matches.some( ...matchers.map((matcher) => Parser.isParser(matcher) ? matcher : literal(matcher as any), ), ); const result = parser.enumParsed(this.a); if ("error" in result) { return me as any; } const { value } = result; if (outcome instanceof Function) { return new Matched(outcome(value)) as any; } return new Matched(outcome) as any; }) as any; defaultTo(value: B) { return value; } defaultToLazy(getValue: () => B) { return getValue(); } unwrap: UnwrapFn = ((): OutcomeType => { throw new Error("Expecting that value is matched"); }) as any; } const array: typeof arrayOf & Parser = Object.assign( function arrayOfWrapper(...args: any) { return (arrayOf as any)(...args); }, isArray, ) as any; /** * Want to be able to bring in the declarative nature that a functional programming * language feature of the pattern matching and the switch statement. With the destructors * the only thing left was to find the correct structure then move move forward. * Using a structure in chainable fashion allows for a syntax that works with typescript * while looking similar to matches statements in other languages * * Use: matches('a value').when(matches.isNumber, (aNumber) => aNumber + 4).defaultTo('fallback value') */ export const matches = Object.assign( function matchesFn(value: Ins) { return new MatchMore(value); }, { array, arrayOf, some, tuple, regex, number, natural, isFunction, object, string, shape, partial, literal, every, guard, unknown, any, boolean, dictionary, literals, nill: isNill, instanceOf, Parse: Parser, parserName, recursive, deferred, }, ); const nill = isNill; const Parse = Parser; const oneOf = some; const anyOf = some; const allOf = every; export { allOf, any, anyOf, array, arrayOf, boolean, deferred, dictionary, every, guard, instanceOf, isFunction, literal, literals, natural, nill, number, object, oneOf, Parse, Parser, parserName, partial, recursive, regex, shape, some, string, tuple, unknown, }; export default matches;