-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert.ts
291 lines (267 loc) · 10.5 KB
/
assert.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import { Buffer, NodeJS } from "./types.ts";
import type {
AccessorDescriptor,
Class,
DataDescriptor,
MapIterator,
Module,
ObservableLike,
Predicate,
Primitive,
SetIterator,
TypedArray,
} from "./types.ts";
export enum AssertionTypeDescription {
class_ = "Class",
numericString = "string with a number",
nullOrUndefined = "null or undefined",
nullish = "nullish (null or undefined)",
iterable = "Iterable",
asyncIterable = "AsyncIterable",
nativePromise = "native Promise",
urlString = "string with a URL",
url = "URL instance or a string with a URL",
truthy = "truthy",
falsy = "falsy",
nan = "NaN",
primitive = "primitive",
integer = "integer",
safeInteger = "integer",
plainObject = "plain object",
arrayLike = "array-like",
typedArray = "TypedArray",
domElement = "HTMLElement",
nodeStream = "Node.js Stream",
infinite = "infinite number",
entry = "tuple-pair entry",
entries = "tuple-pair entries",
sparseArray = "sparse array",
emptyArray = "empty array",
nonEmptyArray = "non-empty array",
emptyString = "empty string",
whitespace = "whitespace string",
emptyStringOrWhitespace = "empty string or whitespace",
nonEmptyString = "non-empty string",
nonEmptyStringAndNotWhitespace = "non-empty string and not whitespace",
emptyObject = "empty object",
nonEmptyObject = "non-empty object",
emptySet = "empty set",
nonEmptySet = "non-empty set",
emptyMap = "empty map",
nonEmptyMap = "non-empty map",
evenInteger = "even integer",
oddInteger = "odd integer",
directInstanceOf = "T",
inRange = "in range",
any = "predicate returns truthy for any value",
all = "predicate returns truthy for all values",
none = "predicate returns falsy for all values / truthy for none",
some = any,
every = all,
}
/**
* Type assertions have to be declared with an explicit type.
*/
export interface Assertions {
string: (value: unknown) => asserts value is string;
number: (value: unknown) => asserts value is number;
bigint: (value: unknown) => asserts value is bigint;
boolean: (value: unknown) => asserts value is boolean;
symbol: (value: unknown) => asserts value is symbol;
numericString: (value: unknown) => asserts value is string;
array: <T = unknown>(
value: unknown,
assertion?: (element: unknown) => asserts element is T,
) => asserts value is T[];
// Unknowns.
undefined: (value: unknown) => asserts value is undefined;
nullOrUndefined: (value: unknown) => asserts value is null | undefined;
nullish: (value: unknown) => asserts value is null | undefined;
null: (value: unknown) => asserts value is null;
/** @deprecated use {@linkcode assert.null} instead */
null_: (value: unknown) => asserts value is null;
function: (value: unknown) => asserts value is Function;
/** @deprecated use {@linkcode assert.function} instead */
function_: (value: unknown) => asserts value is Function;
class: <P = unknown>(value: unknown) => asserts value is Class<P>;
/** @deprecated use {@linkcode assert.class} instead */
class_: <P = unknown>(value: unknown) => asserts value is Class<P>;
iterable: <T = unknown>(value: unknown) => asserts value is Iterable<T>;
asyncIterable: <T = unknown>(
value: unknown,
) => asserts value is AsyncIterable<T>;
generator: (value: unknown) => asserts value is Generator;
asyncGenerator: (value: unknown) => asserts value is AsyncGenerator;
nativePromise: <T = unknown>(value: unknown) => asserts value is Promise<T>;
promise: <T = unknown>(value: unknown) => asserts value is Promise<T>;
generatorFunction: (value: unknown) => asserts value is GeneratorFunction;
asyncGeneratorFunction: (
value: unknown,
) => asserts value is AsyncGeneratorFunction;
asyncFunction: (value: unknown) => asserts value is Function;
boundFunction: (value: unknown) => asserts value is Function;
regExp: (value: unknown) => asserts value is RegExp;
regex: (value: unknown) => asserts value is RegExp;
date: (value: unknown) => asserts value is Date;
error: (value: unknown) => asserts value is Error;
map: <Key = unknown, Value = unknown>(
value: unknown,
) => asserts value is Map<Key, Value>;
set: <T = unknown>(value: unknown) => asserts value is Set<T>;
weakMap: <Key extends object = object, Value = unknown>(
value: unknown,
) => asserts value is WeakMap<Key, Value>;
weakSet: <T extends object = object>(
value: unknown,
) => asserts value is WeakSet<T>;
weakRef: <T extends object = object>(
value: unknown,
) => asserts value is WeakRef<T>;
typedArray: (value: unknown) => asserts value is TypedArray;
int8Array: (value: unknown) => asserts value is Int8Array;
uint8Array: (value: unknown) => asserts value is Uint8Array;
uint8ClampedArray: (value: unknown) => asserts value is Uint8ClampedArray;
int16Array: (value: unknown) => asserts value is Int16Array;
uint16Array: (value: unknown) => asserts value is Uint16Array;
int32Array: (value: unknown) => asserts value is Int32Array;
uint32Array: (value: unknown) => asserts value is Uint32Array;
float32Array: (value: unknown) => asserts value is Float32Array;
float64Array: (value: unknown) => asserts value is Float64Array;
bigInt64Array: (value: unknown) => asserts value is BigInt64Array;
bigUint64Array: (value: unknown) => asserts value is BigUint64Array;
arrayBuffer: (value: unknown) => asserts value is ArrayBuffer;
sharedArrayBuffer: (value: unknown) => asserts value is SharedArrayBuffer;
dataView: (value: unknown) => asserts value is DataView;
buffer: (value: unknown) => asserts value is Buffer;
object: <Key extends keyof any = string, Value = unknown>(
value: unknown,
) => asserts value is Record<Key, Value>;
namespaceModule: (value: unknown) => asserts value is Module;
mapIterator: (value: unknown) => asserts value is MapIterator;
setIterator: (value: unknown) => asserts value is SetIterator;
propertyKey: (value: unknown) => asserts value is PropertyKey;
propertyDescriptor: <T = unknown>(
value: unknown,
) => asserts value is TypedPropertyDescriptor<T>;
accessorDescriptor: <T = unknown>(
value: unknown,
) => asserts value is AccessorDescriptor<T>;
dataDescriptor: <T = unknown>(
value: unknown,
) => asserts value is DataDescriptor<T>;
key: <T extends readonly unknown[] | Record<PropertyKey, unknown> = {}>(
value: unknown,
target: T,
) => asserts value is keyof T;
value: <T extends readonly unknown[] | Record<PropertyKey, unknown> = {}>(
value: unknown,
target: T,
) => asserts value is T[keyof T];
enumKey: <T = unknown>(
value: unknown,
targetEnum: T,
) => asserts value is keyof T;
enumCase: <T = unknown>(
value: unknown,
targetEnum: T,
) => asserts value is T[keyof T];
plainObject: <Value = unknown>(
value: unknown,
) => asserts value is Record<string, Value>;
arrayLike: <T = unknown>(value: unknown) => asserts value is ArrayLike<T>;
entry: <K = unknown, V = unknown>(
value: unknown,
) => asserts value is readonly [K, V];
entries: <K = unknown, V = unknown>(
value: unknown,
) => asserts value is readonly (readonly [K, V])[];
sparseArray: (value: unknown) => asserts value is unknown[];
emptyArray: (value: unknown) => asserts value is never[];
nonEmptyArray: (value: unknown) => asserts value is [unknown, ...unknown[]];
whitespace: (value: unknown) => asserts value is string;
emptyString: (value: unknown) => asserts value is "";
emptyStringOrWhitespace: (value: unknown) => asserts value is string;
nonEmptyString: (value: unknown) => asserts value is string;
nonEmptyStringAndNotWhitespace: (value: unknown) => asserts value is string;
emptyObject: <Key extends keyof any = string>(
value: unknown,
) => asserts value is Record<Key, never>;
nonEmptyObject: <Key extends keyof any = string, Value = unknown>(
value: unknown,
) => asserts value is Record<Key, Value>;
emptySet: (value: unknown) => asserts value is Set<never>;
nonEmptySet: <T = unknown>(value: unknown) => asserts value is Set<T>;
emptyMap: (value: unknown) => asserts value is Map<never, never>;
nonEmptyMap: <Key = unknown, Value = unknown>(
value: unknown,
) => asserts value is Map<Key, Value>;
domElement: (value: unknown) => asserts value is HTMLElement;
observable: (value: unknown) => asserts value is ObservableLike;
nodeStream: (value: unknown) => asserts value is NodeJS.Stream;
blob: (value: unknown) => asserts value is Blob;
formData: (value: unknown) => asserts value is FormData;
headers: (value: unknown) => asserts value is Headers;
request: (value: unknown) => asserts value is Request;
response: (value: unknown) => asserts value is Response;
urlSearchParams: (value: unknown) => asserts value is URLSearchParams;
urlInstance: (value: unknown) => asserts value is URL;
urlString: (value: unknown) => asserts value is string;
url: (value: unknown) => asserts value is string | URL;
// Numbers.
truthy: (value: unknown) => asserts value is unknown;
falsy: (value: unknown) => asserts value is unknown;
nan: (value: unknown) => asserts value is unknown;
primitive: (value: unknown) => asserts value is Primitive;
integer: (value: unknown) => asserts value is number;
safeInteger: (value: unknown) => asserts value is number;
evenInteger: (value: number) => asserts value is number;
oddInteger: (value: number) => asserts value is number;
inRange: (value: number, range: number | number[]) => asserts value is number;
infinite: (value: unknown) => asserts value is number;
directInstanceOf: <T>(
instance: unknown,
class_: Class<T>,
) => asserts instance is T;
instanceOf: <T>(
instance: unknown,
class_: Class<T>,
) => asserts instance is T;
// Variadic functions.
any: (
predicate: Predicate | Predicate[],
...values: unknown[]
) => void | never;
all: (
predicate: Predicate | Predicate[],
...values: unknown[]
) => void | never;
every: (
predicate: Predicate | Predicate[],
...values: unknown[]
) => void | never;
some: (
predicate: Predicate | Predicate[],
...values: unknown[]
) => void | never;
none: (
predicate: Predicate | Predicate[],
...values: unknown[]
) => void | never;
}
export interface AssertOptions {
multipleValues?: boolean;
}
export interface Assert extends Assertions {
(expression: boolean, message?: string): asserts expression;
(
expression: boolean,
value: unknown,
message: string,
): asserts expression;
(
expression: boolean,
received: unknown,
expected: string,
options?: AssertOptions,
): asserts expression;
}