-
Notifications
You must be signed in to change notification settings - Fork 3
/
every-parser.ts
executable file
·28 lines (27 loc) · 1.01 KB
/
every-parser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// deno-lint-ignore-file no-explicit-any
import { any, Parser } from "./index.ts";
import { AndParser, EnsureParser, IParser } from "./interfaces.ts";
// prettier-ignore
// deno-fmt-ignore
export type EveryParser<T> =
T extends [] | readonly [] ? IParser<unknown, any>
: T extends [infer A] | readonly [infer A] ? EnsureParser<A>
: T extends [infer A, ...infer B] | readonly [infer A, ...infer B] ? AndParser<A, EveryParser<B>>
: never
/**
* Intersection is a good tool to make sure that the validated value
* is in the intersection of all the validators passed in. Basically an `and`
* operator for validators
*/
export function every<RestParsers extends Parser<unknown, unknown>[]>(
...parsers: RestParsers
): EveryParser<RestParsers> {
const filteredParsers = parsers.filter((x) => x !== any);
if (filteredParsers.length <= 0) {
return any as any;
}
const first = filteredParsers.splice(0, 1)[0];
return filteredParsers.reduce((left, right) => {
return left.concat(right);
}, first) as any;
}