-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
predicates.ts
347 lines (307 loc) · 10.6 KB
/
predicates.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import type {Buffer} from 'node:buffer';
import {TypedArray} from 'type-fest';
import {StringPredicate} from './predicates/string.js';
import {NumberPredicate} from './predicates/number.js';
import {BigIntPredicate} from './predicates/bigint.js';
import {BooleanPredicate} from './predicates/boolean.js';
import {Predicate, PredicateOptions} from './predicates/predicate.js';
import {ArrayPredicate} from './predicates/array.js';
import {ObjectPredicate} from './predicates/object.js';
import {DatePredicate} from './predicates/date.js';
import {ErrorPredicate} from './predicates/error.js';
import {MapPredicate} from './predicates/map.js';
import {WeakMapPredicate} from './predicates/weak-map.js';
import {SetPredicate} from './predicates/set.js';
import {WeakSetPredicate} from './predicates/weak-set.js';
import {TypedArrayPredicate} from './predicates/typed-array.js';
import {ArrayBufferPredicate} from './predicates/array-buffer.js';
import {DataViewPredicate} from './predicates/data-view.js';
import {BasePredicate} from './predicates/base-predicate.js';
import {AnyPredicate} from './predicates/any.js';
export interface Predicates {
/**
Test the value to be a string.
*/
readonly string: StringPredicate;
/**
Test the value to be a number.
*/
readonly number: NumberPredicate;
/**
Test the value to be an bigint.
*/
readonly bigint: BigIntPredicate;
/**
Test the value to be a boolean.
*/
readonly boolean: BooleanPredicate;
/**
Test the value to be undefined.
*/
readonly undefined: Predicate<undefined>;
/**
Test the value to be null.
*/
readonly null: Predicate<null>;
/**
Test the value to be null or undefined.
*/
readonly nullOrUndefined: Predicate<null | undefined>;
/**
Test the value to be not a number.
*/
readonly nan: Predicate<number>;
/**
Test the value to be a Symbol.
*/
readonly symbol: Predicate<symbol>;
/**
Test the value to be an array.
*/
readonly array: ArrayPredicate;
/**
Test the value to be an object.
*/
readonly object: ObjectPredicate;
/**
Test the value to be a Date.
*/
readonly date: DatePredicate;
/**
Test the value to be an Error.
*/
readonly error: ErrorPredicate;
/**
Test the value to be a Map.
*/
readonly map: MapPredicate;
/**
Test the value to be a WeakMap.
*/
readonly weakMap: WeakMapPredicate;
/**
Test the value to be a Set.
*/
readonly set: SetPredicate;
/**
Test the value to be a WeakSet.
*/
readonly weakSet: WeakSetPredicate;
/**
Test the value to be a Function.
*/
readonly function: Predicate<Function>;
/**
Test the value to be a Buffer.
*/
readonly buffer: Predicate<Buffer>;
/**
Test the value to be a RegExp.
*/
readonly regExp: Predicate<RegExp>;
/**
Test the value to be a Promise.
*/
readonly promise: Predicate<Promise<unknown>>;
/**
Test the value to be a typed array.
*/
readonly typedArray: TypedArrayPredicate<TypedArray>;
/**
Test the value to be a Int8Array.
*/
readonly int8Array: TypedArrayPredicate<Int8Array>;
/**
Test the value to be a Uint8Array.
*/
readonly uint8Array: TypedArrayPredicate<Uint8Array>;
/**
Test the value to be a Uint8ClampedArray.
*/
readonly uint8ClampedArray: TypedArrayPredicate<Uint8ClampedArray>;
/**
Test the value to be a Int16Array.
*/
readonly int16Array: TypedArrayPredicate<Int16Array>;
/**
Test the value to be a Uint16Array.
*/
readonly uint16Array: TypedArrayPredicate<Uint16Array>;
/**
Test the value to be a Int32Array.
*/
readonly int32Array: TypedArrayPredicate<Int32Array>;
/**
Test the value to be a Uint32Array.
*/
readonly uint32Array: TypedArrayPredicate<Uint32Array>;
/**
Test the value to be a Float32Array.
*/
readonly float32Array: TypedArrayPredicate<Float32Array>;
/**
Test the value to be a Float64Array.
*/
readonly float64Array: TypedArrayPredicate<Float64Array>;
/**
Test the value to be a ArrayBuffer.
*/
readonly arrayBuffer: ArrayBufferPredicate<ArrayBuffer>;
/**
Test the value to be a SharedArrayBuffer.
*/
readonly sharedArrayBuffer: ArrayBufferPredicate<SharedArrayBuffer>;
/**
Test the value to be a DataView.
*/
readonly dataView: DataViewPredicate;
/**
Test the value to be Iterable.
*/
readonly iterable: Predicate<Iterable<unknown>>;
/**
Test that the value matches at least one of the given predicates.
*/
any: (<T1>(p1: BasePredicate<T1>) => AnyPredicate<T1>)
& (<T1, T2>(p1: BasePredicate<T1>, p2: BasePredicate<T2>) => AnyPredicate<T1 | T2>)
& (<T1, T2, T3>(p1: BasePredicate<T1>, p2: BasePredicate<T2>, p3: BasePredicate<T3>) => AnyPredicate<T1 | T2 | T3>)
& (<T1, T2, T3, T4>(p1: BasePredicate<T1>, p2: BasePredicate<T2>, p3: BasePredicate<T3>, p4: BasePredicate<T4>) => AnyPredicate<T1 | T2 | T3 | T4>)
& (<T1, T2, T3, T4, T5>(p1: BasePredicate<T1>, p2: BasePredicate<T2>, p3: BasePredicate<T3>, p4: BasePredicate<T4>, p5: BasePredicate<T5>) => AnyPredicate<T1 | T2 | T3 | T4 | T5>)
& (<T1, T2, T3, T4, T5, T6>(p1: BasePredicate<T1>, p2: BasePredicate<T2>, p3: BasePredicate<T3>, p4: BasePredicate<T4>, p5: BasePredicate<T5>, p6: BasePredicate<T6>) => AnyPredicate<T1 | T2 | T3 | T4 | T5 | T6>)
& (<T1, T2, T3, T4, T5, T6, T7>(p1: BasePredicate<T1>, p2: BasePredicate<T2>, p3: BasePredicate<T3>, p4: BasePredicate<T4>, p5: BasePredicate<T5>, p6: BasePredicate<T6>, p7: BasePredicate<T7>) => AnyPredicate<T1 | T2 | T3 | T4 | T5 | T6 | T7>)
& (<T1, T2, T3, T4, T5, T6, T7, T8>(p1: BasePredicate<T1>, p2: BasePredicate<T2>, p3: BasePredicate<T3>, p4: BasePredicate<T4>, p5: BasePredicate<T5>, p6: BasePredicate<T6>, p7: BasePredicate<T7>, p8: BasePredicate<T8>) => AnyPredicate<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8>)
& (<T1, T2, T3, T4, T5, T6, T7, T8, T9>(p1: BasePredicate<T1>, p2: BasePredicate<T2>, p3: BasePredicate<T3>, p4: BasePredicate<T4>, p5: BasePredicate<T5>, p6: BasePredicate<T6>, p7: BasePredicate<T7>, p8: BasePredicate<T8>, p9: BasePredicate<T9>) => AnyPredicate<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9>)
& (<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(p1: BasePredicate<T1>, p2: BasePredicate<T2>, p3: BasePredicate<T3>, p4: BasePredicate<T4>, p5: BasePredicate<T5>, p6: BasePredicate<T6>, p7: BasePredicate<T7>, p8: BasePredicate<T8>, p9: BasePredicate<T9>, p10: BasePredicate<T10>) => AnyPredicate<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9 | T10>)
& ((...predicate: BasePredicate[]) => AnyPredicate);
}
const predicates = <T>(object: T, options?: PredicateOptions): T & Predicates => {
Object.defineProperties(object, {
string: {
get: (): StringPredicate => new StringPredicate(options),
},
number: {
get: (): NumberPredicate => new NumberPredicate(options),
},
bigint: {
get: (): BigIntPredicate => new BigIntPredicate(options),
},
boolean: {
get: (): BooleanPredicate => new BooleanPredicate(options),
},
undefined: {
get: (): Predicate => new Predicate('undefined', options),
},
null: {
get: (): Predicate => new Predicate('null', options),
},
nullOrUndefined: {
get: (): Predicate => new Predicate('nullOrUndefined', options),
},
nan: {
get: (): Predicate => new Predicate('nan', options),
},
symbol: {
get: (): Predicate => new Predicate('symbol', options),
},
array: {
get: (): ArrayPredicate => new ArrayPredicate(options),
},
object: {
get: (): ObjectPredicate => new ObjectPredicate(options),
},
date: {
get: (): DatePredicate => new DatePredicate(options),
},
error: {
get: (): ErrorPredicate => new ErrorPredicate(options),
},
map: {
get: (): MapPredicate => new MapPredicate(options),
},
weakMap: {
get: (): WeakMapPredicate => new WeakMapPredicate(options),
},
set: {
get: (): SetPredicate => new SetPredicate(options),
},
weakSet: {
get: (): WeakSetPredicate => new WeakSetPredicate(options),
},
function: {
get: (): Predicate => new Predicate('Function', options),
},
buffer: {
get: (): Predicate => new Predicate('Buffer', options),
},
regExp: {
get: (): Predicate<RegExp> => new Predicate('RegExp', options),
},
promise: {
get: (): Predicate => new Predicate('Promise', options),
},
typedArray: {
get: (): TypedArrayPredicate<TypedArray> => new TypedArrayPredicate('TypedArray', options),
},
int8Array: {
get: (): TypedArrayPredicate<Int8Array> => new TypedArrayPredicate('Int8Array', options),
},
uint8Array: {
get: (): TypedArrayPredicate<Uint8Array> => new TypedArrayPredicate('Uint8Array', options),
},
uint8ClampedArray: {
get: (): TypedArrayPredicate<Uint8Array> => new TypedArrayPredicate('Uint8ClampedArray', options),
},
int16Array: {
get: (): TypedArrayPredicate<Int16Array> => new TypedArrayPredicate('Int16Array', options),
},
uint16Array: {
get: (): TypedArrayPredicate<Uint16Array> => new TypedArrayPredicate('Uint16Array', options),
},
int32Array: {
get: (): TypedArrayPredicate<Int32Array> => new TypedArrayPredicate('Int32Array', options),
},
uint32Array: {
get: (): TypedArrayPredicate<Uint32Array> => new TypedArrayPredicate('Uint32Array', options),
},
float32Array: {
get: (): TypedArrayPredicate<Float32Array> => new TypedArrayPredicate('Float32Array', options),
},
float64Array: {
get: (): TypedArrayPredicate<Float64Array> => new TypedArrayPredicate('Float64Array', options),
},
arrayBuffer: {
get: (): ArrayBufferPredicate<ArrayBuffer> => new ArrayBufferPredicate('ArrayBuffer', options),
},
sharedArrayBuffer: {
get: (): ArrayBufferPredicate<SharedArrayBuffer> => new ArrayBufferPredicate('SharedArrayBuffer', options),
},
dataView: {
get: (): DataViewPredicate => new DataViewPredicate(options),
},
iterable: {
get: (): Predicate => new Predicate('Iterable', options),
},
any: {
value: (...predicates: BasePredicate[]): AnyPredicate => new AnyPredicate(predicates, options),
},
});
return object as T & Predicates;
};
export default predicates;
export {ObjectPredicate} from './predicates/object.js';
export type {Shape} from './predicates/object.js';
export {StringPredicate} from './predicates/string.js';
export {NumberPredicate} from './predicates/number.js';
export {BigIntPredicate} from './predicates/bigint.js';
export {BooleanPredicate} from './predicates/boolean.js';
export {ArrayPredicate} from './predicates/array.js';
export {DatePredicate} from './predicates/date.js';
export {ErrorPredicate} from './predicates/error.js';
export {MapPredicate} from './predicates/map.js';
export {WeakMapPredicate} from './predicates/weak-map.js';
export {SetPredicate} from './predicates/set.js';
export {WeakSetPredicate} from './predicates/weak-set.js';
export {TypedArrayPredicate} from './predicates/typed-array.js';
export {ArrayBufferPredicate} from './predicates/array-buffer.js';
export {DataViewPredicate} from './predicates/data-view.js';
export {AnyPredicate} from './predicates/any.js';