Skip to content

Commit

Permalink
Add def (like sum-type)
Browse files Browse the repository at this point in the history
  • Loading branch information
JAForbes committed Feb 17, 2024
1 parent 94e2e41 commit 567ae05
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/index.ts
Expand Up @@ -35,6 +35,9 @@ export type API<N extends string, D extends Definition> = {
instance: InternalInstance<N, D, keyof D>,
options: MatchOptions<D, T>
) => T;
def: <T>(
fn: (x: InternalInstance<N, D, keyof D>) => T,
) => (x: InternalInstance<N, D, keyof D>) => T
};

export type Is<R extends string> = `is${R}`;
Expand Down Expand Up @@ -89,6 +92,10 @@ function match(instance: any, options: any): any {
return options[instance.tag](instance.value);
}

function def(fn: any): any {
return (x: any) => fn(x)
}

function otherwise(tags:string[]) {
return (fn: any) =>
Object.fromEntries(
Expand Down Expand Up @@ -233,6 +240,7 @@ export function type<N extends string, D extends Definition>(
fromPathSafe,
matchOr,
match,
def,
otherwise: (...args: any[]) => {
if (args.length === 0) {
return otherwise(Object.keys(routes))
Expand Down

0 comments on commit 567ae05

Please sign in to comment.