-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
index.ts
134 lines (109 loc) · 3.96 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import callsites from 'callsites';
import {inferLabel} from './utils/infer-label';
import {Predicate} from './predicates/predicate';
import {BasePredicate, isPredicate} from './predicates/base-predicate';
import modifiers, {Modifiers} from './modifiers';
import predicates, {Predicates} from './predicates';
import test from './test';
/**
@hidden
*/
export type Main = <T>(value: T, label: string | Function, predicate: BasePredicate<T>) => void;
// Extends is only necessary for the generated documentation to be cleaner. The loaders below infer the correct type.
export interface Ow extends Modifiers, Predicates {
/**
Returns `true` if the value matches the predicate, otherwise returns `false`.
@param value - Value to test.
@param predicate - Predicate to test against.
*/
isValid: <T>(value: T, predicate: BasePredicate<T>) => value is T;
/**
Create a reusable validator.
@param predicate - Predicate used in the validator function.
*/
create: (<T>(predicate: BasePredicate<T>) => ReusableValidator<T>) & (<T>(label: string, predicate: BasePredicate<T>) => ReusableValidator<T>);
/**
Test if the value matches the predicate. Throws an `ArgumentError` if the test fails.
@param value - Value to test.
@param predicate - Predicate to test against.
*/
<T>(value: unknown, predicate: BasePredicate<T>): asserts value is T;
/**
Test if `value` matches the provided `predicate`. Throws an `ArgumentError` with the specified `label` if the test fails.
@param value - Value to test.
@param label - Label which should be used in error messages.
@param predicate - Predicate to test against.
*/
<T>(value: unknown, label: string, predicate: BasePredicate<T>): asserts value is T;
}
/**
A reusable validator.
*/
export interface ReusableValidator<T> {
/**
Test if the value matches the predicate. Throws an `ArgumentError` if the test fails.
@param value - Value to test.
@param label - Override the label which should be used in error messages.
*/
// eslint-disable-next-line @typescript-eslint/prefer-function-type
(value: T, label?: string): void;
}
const ow = <T>(value: T, labelOrPredicate: unknown, predicate?: BasePredicate<T>): void => {
if (!isPredicate(labelOrPredicate) && typeof labelOrPredicate !== 'string') {
throw new TypeError(`Expected second argument to be a predicate or a string, got \`${typeof labelOrPredicate}\``);
}
if (isPredicate(labelOrPredicate)) {
// If the second argument is a predicate, infer the label
const stackFrames = callsites();
test(value, () => inferLabel(stackFrames), labelOrPredicate);
return;
}
test(value, labelOrPredicate, predicate!);
};
Object.defineProperties(ow, {
isValid: {
value: <T>(value: T, predicate: BasePredicate<T>): boolean => {
try {
ow(value, predicate);
return true;
} catch {
return false;
}
}
},
create: {
value: <T>(labelOrPredicate: BasePredicate<T> | string | undefined, predicate?: BasePredicate<T>) => (value: T, label?: string): void => {
if (isPredicate(labelOrPredicate)) {
const stackFrames = callsites();
test(value, label ?? ((): string | void => inferLabel(stackFrames)), labelOrPredicate);
return;
}
test(value, label ?? (labelOrPredicate!), predicate!);
}
}
});
// Can't use `export default predicates(modifiers(ow)) as Ow` because the variable needs a type annotation to avoid a compiler error when used:
// Assertions require every name in the call target to be declared with an explicit type annotation.ts(2775)
// See https://github.com/microsoft/TypeScript/issues/36931 for more details.
const _ow: Ow = predicates(modifiers(ow)) as Ow;
export default _ow;
export {BasePredicate, Predicate};
export {
StringPredicate,
NumberPredicate,
BooleanPredicate,
ArrayPredicate,
ObjectPredicate,
DatePredicate,
ErrorPredicate,
MapPredicate,
WeakMapPredicate,
SetPredicate,
WeakSetPredicate,
TypedArrayPredicate,
ArrayBufferPredicate,
DataViewPredicate,
AnyPredicate,
Shape
} from './predicates';
export {ArgumentError} from './argument-error';