-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
31 lines (26 loc) · 905 Bytes
/
index.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
29
30
31
import { ValidatorMap, ObjectTypeGuard, ValueMap, ValueTypeGuard, ArrayTypeGuard } from "./types";
const Guard = <T>(validators: ValidatorMap<T>): ObjectTypeGuard<T> => (
values: ValueMap<T>
): values is T => {
if (values == null) return false;
for (const key in validators) {
const test = validators[key];
const value = values[key];
if (!test(value)) return false;
else continue;
}
return true;
};
const GuardEach = <T>(
test: ValueTypeGuard<T>
): ArrayTypeGuard<T> => (values): values is T[] => {
if (!Array.isArray(values)) return false;
for (const value of values) {
if (!test(value)) return false;
else continue;
}
return true;
};
export { Guard, GuardEach };
export { isNumber, isString, isBoolean, isArray } from "./guards/guards";
export { compose, and, or, optional, nullable, erratic, customTest, test, unsafeTest } from "./operators/operators";