Skip to content

Commit 567ae05

Browse files
committed
Add def (like sum-type)
1 parent 94e2e41 commit 567ae05

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

lib/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ export type API<N extends string, D extends Definition> = {
3535
instance: InternalInstance<N, D, keyof D>,
3636
options: MatchOptions<D, T>
3737
) => T;
38+
def: <T>(
39+
fn: (x: InternalInstance<N, D, keyof D>) => T,
40+
) => (x: InternalInstance<N, D, keyof D>) => T
3841
};
3942

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

95+
function def(fn: any): any {
96+
return (x: any) => fn(x)
97+
}
98+
9299
function otherwise(tags:string[]) {
93100
return (fn: any) =>
94101
Object.fromEntries(
@@ -233,6 +240,7 @@ export function type<N extends string, D extends Definition>(
233240
fromPathSafe,
234241
matchOr,
235242
match,
243+
def,
236244
otherwise: (...args: any[]) => {
237245
if (args.length === 0) {
238246
return otherwise(Object.keys(routes))

0 commit comments

Comments
 (0)