Skip to content

Commit

Permalink
add all
Browse files Browse the repository at this point in the history
  • Loading branch information
janstuemmel committed Jun 28, 2024
1 parent ef03446 commit d2ae313
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
"quoteStyle": "single"
}
},
"linter": {
"rules": {
"suspicious": {
"noExplicitAny": "off"
}
}
},
"files": {
"ignore": ["./dist/*"]
}
Expand Down
19 changes: 19 additions & 0 deletions lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ const _nah = <E>(err: E): Nah<E> => ({ok: false, err});
export const yep = <T>(v: T): Box<T, never> => Promise.resolve(_yep(v));
export const nah = <E>(err: E): Box<never, E> => Promise.resolve(_nah(err));

type GetYep<T extends Yep<any>> = T extends Yep<infer U> ? U : never;

type GetBoxYep<T extends Box<any, any>> = T extends Promise<infer U>
? U extends Yep<infer Y>
? Y
: never
: never;

type Pipe = {
<A>(a: A): A;
<A, B>(a: A, ab: (a: A) => B): B;
Expand Down Expand Up @@ -66,3 +74,14 @@ export const or =
<T, D, E, F>(fn: (e: E) => Box<D, F>) =>
(box: Box<T, E>): Box<T | D, F> =>
box.then((r): Box<T | D, F> => (r.ok ? yep(r.val) : fn(r.err)));

export const all = <const A extends Box<any, any>[]>(
a: A,
): Box<{[I in keyof A]: GetBoxYep<A[I]>}, never> =>
Promise.all(a).then((res) =>
_yep(
res.filter((v) => v.ok).map((v) => v.val) as {
[I in keyof A]: GetBoxYep<A[I]>;
},
),
);

0 comments on commit d2ae313

Please sign in to comment.