@orkestrel/contract 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/core/combinators.d.ts +3 -3
- package/dist/src/core/compilers.d.ts +29 -1
- package/dist/src/core/constants.d.ts +1 -1
- package/dist/src/core/helpers.d.ts +1 -1
- package/dist/src/core/index.cjs +2722 -0
- package/dist/src/core/index.cjs.map +1 -0
- package/dist/src/core/index.d.cts +8 -0
- package/dist/src/core/index.js +609 -51
- package/dist/src/core/index.js.map +1 -1
- package/dist/src/core/parsers.d.ts +82 -2
- package/dist/src/core/shapers.d.ts +38 -1
- package/dist/src/core/types.d.ts +11 -3
- package/dist/src/core/validators.d.ts +464 -52
- package/package.json +25 -15
package/dist/src/core/index.js
CHANGED
|
@@ -28,15 +28,37 @@ var JSON_SCHEMA_TYPES = Object.freeze([
|
|
|
28
28
|
]);
|
|
29
29
|
//#endregion
|
|
30
30
|
//#region src/core/validators.ts
|
|
31
|
-
/** Determine whether a value is `null`.
|
|
31
|
+
/** Determine whether a value is `null`.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* isNull(null) // true
|
|
36
|
+
* isNull(undefined) // false
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
32
39
|
function isNull(value) {
|
|
33
40
|
return value === null;
|
|
34
41
|
}
|
|
35
|
-
/** Determine whether a value is `undefined`.
|
|
42
|
+
/** Determine whether a value is `undefined`.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* isUndefined(undefined) // true
|
|
47
|
+
* isUndefined(null) // false
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
36
50
|
function isUndefined(value) {
|
|
37
51
|
return value === void 0;
|
|
38
52
|
}
|
|
39
|
-
/** Determine whether a value is defined (neither `null` nor `undefined`).
|
|
53
|
+
/** Determine whether a value is defined (neither `null` nor `undefined`).
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* isDefined('hi') // true
|
|
58
|
+
* isDefined(null) // false
|
|
59
|
+
* isDefined(undefined) // false
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
40
62
|
function isDefined(value) {
|
|
41
63
|
return value !== null && value !== void 0;
|
|
42
64
|
}
|
|
@@ -57,59 +79,160 @@ function isNumber(value) {
|
|
|
57
79
|
function isFiniteNumber(value) {
|
|
58
80
|
return typeof value === "number" && Number.isFinite(value);
|
|
59
81
|
}
|
|
60
|
-
/** Determine whether a value is a finite integer (excludes `NaN`, `±Infinity`, and fractional numbers).
|
|
82
|
+
/** Determine whether a value is a finite integer (excludes `NaN`, `±Infinity`, and fractional numbers).
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* isInteger(3) // true
|
|
87
|
+
* isInteger(3.5) // false
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
61
90
|
function isInteger(value) {
|
|
62
91
|
return Number.isInteger(value);
|
|
63
92
|
}
|
|
64
|
-
/** Determine whether a value is a boolean.
|
|
93
|
+
/** Determine whether a value is a boolean.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* isBoolean(true) // true
|
|
98
|
+
* isBoolean(1) // false
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
65
101
|
function isBoolean(value) {
|
|
66
102
|
return typeof value === "boolean";
|
|
67
103
|
}
|
|
68
|
-
/** Determine whether a value is exactly `true`.
|
|
104
|
+
/** Determine whether a value is exactly `true`.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* isTrue(true) // true
|
|
109
|
+
* isTrue(false) // false
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
69
112
|
function isTrue(value) {
|
|
70
113
|
return value === true;
|
|
71
114
|
}
|
|
72
|
-
/** Determine whether a value is exactly `false`.
|
|
115
|
+
/** Determine whether a value is exactly `false`.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* isFalse(false) // true
|
|
120
|
+
* isFalse(true) // false
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
73
123
|
function isFalse(value) {
|
|
74
124
|
return value === false;
|
|
75
125
|
}
|
|
76
|
-
/** Determine whether a value is a bigint.
|
|
126
|
+
/** Determine whether a value is a bigint.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* isBigInt(1n) // true
|
|
131
|
+
* isBigInt(1) // false
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
77
134
|
function isBigInt(value) {
|
|
78
135
|
return typeof value === "bigint";
|
|
79
136
|
}
|
|
80
|
-
/** Determine whether a value is a symbol.
|
|
137
|
+
/** Determine whether a value is a symbol.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```ts
|
|
141
|
+
* isSymbol(Symbol('x')) // true
|
|
142
|
+
* isSymbol('x') // false
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
81
145
|
function isSymbol(value) {
|
|
82
146
|
return typeof value === "symbol";
|
|
83
147
|
}
|
|
84
|
-
/** Determine whether a value is callable.
|
|
148
|
+
/** Determine whether a value is callable.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* isFunction(() => {}) // true
|
|
153
|
+
* isFunction({}) // false
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
85
156
|
function isFunction(value) {
|
|
86
157
|
return typeof value === "function";
|
|
87
158
|
}
|
|
88
|
-
/** Determine whether a value is a string or `null`.
|
|
159
|
+
/** Determine whether a value is a string or `null`.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* isNullableString('hi') // true
|
|
164
|
+
* isNullableString(null) // true
|
|
165
|
+
* isNullableString(42) // false
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
89
168
|
function isNullableString(value) {
|
|
90
169
|
return value === null || isString(value);
|
|
91
170
|
}
|
|
92
|
-
/** Determine whether a value is a number or `null` (the number may be `NaN` / `±Infinity`).
|
|
171
|
+
/** Determine whether a value is a number or `null` (the number may be `NaN` / `±Infinity`).
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* isNullableNumber(42) // true
|
|
176
|
+
* isNullableNumber(null) // true
|
|
177
|
+
* isNullableNumber('hi') // false
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
93
180
|
function isNullableNumber(value) {
|
|
94
181
|
return value === null || isNumber(value);
|
|
95
182
|
}
|
|
96
|
-
/** Determine whether a value is a boolean or `null`.
|
|
183
|
+
/** Determine whether a value is a boolean or `null`.
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* isNullableBoolean(true) // true
|
|
188
|
+
* isNullableBoolean(null) // true
|
|
189
|
+
* isNullableBoolean(1) // false
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
97
192
|
function isNullableBoolean(value) {
|
|
98
193
|
return value === null || isBoolean(value);
|
|
99
194
|
}
|
|
100
|
-
/** Determine whether a value is a `Date`.
|
|
195
|
+
/** Determine whether a value is a `Date`.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```ts
|
|
199
|
+
* isDate(new Date()) // true
|
|
200
|
+
* isDate('2024-01-01') // false
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
101
203
|
function isDate(value) {
|
|
102
204
|
return value instanceof Date;
|
|
103
205
|
}
|
|
104
|
-
/** Determine whether a value is a `RegExp`.
|
|
206
|
+
/** Determine whether a value is a `RegExp`.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```ts
|
|
210
|
+
* isRegExp(/a/) // true
|
|
211
|
+
* isRegExp('a') // false
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
105
214
|
function isRegExp(value) {
|
|
106
215
|
return value instanceof RegExp;
|
|
107
216
|
}
|
|
108
|
-
/** Determine whether a value is an `Error`.
|
|
217
|
+
/** Determine whether a value is an `Error`.
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```ts
|
|
221
|
+
* isError(new Error('boom')) // true
|
|
222
|
+
* isError('boom') // false
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
109
225
|
function isError(value) {
|
|
110
226
|
return value instanceof Error;
|
|
111
227
|
}
|
|
112
|
-
/** Determine whether a value is a native `Promise` (use {@link isPromiseLike} for any thenable).
|
|
228
|
+
/** Determine whether a value is a native `Promise` (use {@link isPromiseLike} for any thenable).
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```ts
|
|
232
|
+
* isPromise(Promise.resolve()) // true
|
|
233
|
+
* isPromise({ then() {} }) // false
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
113
236
|
function isPromise(value) {
|
|
114
237
|
return value instanceof Promise;
|
|
115
238
|
}
|
|
@@ -120,6 +243,13 @@ function isPromise(value) {
|
|
|
120
243
|
* @remarks
|
|
121
244
|
* Accepts any object with all three methods, not only native `Promise`
|
|
122
245
|
* instances. Use {@link isPromise} when you specifically need `instanceof Promise`.
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```ts
|
|
249
|
+
* isPromiseLike(Promise.resolve()) // true
|
|
250
|
+
* isPromiseLike({ then() {}, catch() {}, finally() {} }) // true
|
|
251
|
+
* isPromiseLike({ then() {} }) // false
|
|
252
|
+
* ```
|
|
123
253
|
*/
|
|
124
254
|
function isPromiseLike(value) {
|
|
125
255
|
if (!isObject(value)) return false;
|
|
@@ -131,7 +261,14 @@ function isPromiseLike(value) {
|
|
|
131
261
|
});
|
|
132
262
|
return outcome.success && outcome.value;
|
|
133
263
|
}
|
|
134
|
-
/** Determine whether a value is an `ArrayBuffer`.
|
|
264
|
+
/** Determine whether a value is an `ArrayBuffer`.
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```ts
|
|
268
|
+
* isArrayBuffer(new ArrayBuffer(8)) // true
|
|
269
|
+
* isArrayBuffer([]) // false
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
135
272
|
function isArrayBuffer(value) {
|
|
136
273
|
return value instanceof ArrayBuffer;
|
|
137
274
|
}
|
|
@@ -141,6 +278,12 @@ function isArrayBuffer(value) {
|
|
|
141
278
|
* @remarks
|
|
142
279
|
* Guards the global existence of `SharedArrayBuffer` first — safe where it is
|
|
143
280
|
* absent or disabled (e.g. a context that is not cross-origin isolated).
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```ts
|
|
284
|
+
* isSharedArrayBuffer(new SharedArrayBuffer(8)) // true
|
|
285
|
+
* isSharedArrayBuffer(new ArrayBuffer(8)) // false
|
|
286
|
+
* ```
|
|
144
287
|
*/
|
|
145
288
|
function isSharedArrayBuffer(value) {
|
|
146
289
|
return typeof SharedArrayBuffer !== "undefined" && value instanceof SharedArrayBuffer;
|
|
@@ -151,6 +294,13 @@ function isSharedArrayBuffer(value) {
|
|
|
151
294
|
* @remarks
|
|
152
295
|
* Strings are explicitly included: a string has a callable `Symbol.iterator`
|
|
153
296
|
* but is not an object, so the generic object path alone would miss it.
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```ts
|
|
300
|
+
* isIterable([1, 2]) // true
|
|
301
|
+
* isIterable('abc') // true
|
|
302
|
+
* isIterable({ a: 1 }) // false
|
|
303
|
+
* ```
|
|
154
304
|
*/
|
|
155
305
|
function isIterable(value) {
|
|
156
306
|
if (isString(value)) return true;
|
|
@@ -158,7 +308,14 @@ function isIterable(value) {
|
|
|
158
308
|
const outcome = attempt(() => isFunction(Reflect.get(value, Symbol.iterator)));
|
|
159
309
|
return outcome.success && outcome.value;
|
|
160
310
|
}
|
|
161
|
-
/** Determine whether a value implements the async iterable protocol (`Symbol.asyncIterator`).
|
|
311
|
+
/** Determine whether a value implements the async iterable protocol (`Symbol.asyncIterator`).
|
|
312
|
+
*
|
|
313
|
+
* @example
|
|
314
|
+
* ```ts
|
|
315
|
+
* isAsyncIterable({ [Symbol.asyncIterator]() {} }) // true
|
|
316
|
+
* isAsyncIterable([1, 2]) // false
|
|
317
|
+
* ```
|
|
318
|
+
*/
|
|
162
319
|
function isAsyncIterable(value) {
|
|
163
320
|
if (!isObject(value)) return false;
|
|
164
321
|
const outcome = attempt(() => isFunction(Reflect.get(value, Symbol.asyncIterator)));
|
|
@@ -170,6 +327,13 @@ function isAsyncIterable(value) {
|
|
|
170
327
|
* @remarks
|
|
171
328
|
* `true` for arrays, class instances, plain objects, `Map`, `Set`, etc. — use
|
|
172
329
|
* {@link isRecord} when you need a plain-record check.
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* ```ts
|
|
333
|
+
* isObject({}) // true
|
|
334
|
+
* isObject([]) // true
|
|
335
|
+
* isObject(null) // false
|
|
336
|
+
* ```
|
|
173
337
|
*/
|
|
174
338
|
function isObject(value) {
|
|
175
339
|
return typeof value === "object" && value !== null;
|
|
@@ -200,67 +364,179 @@ function isRecord(value) {
|
|
|
200
364
|
});
|
|
201
365
|
return outcome.success && outcome.value;
|
|
202
366
|
}
|
|
203
|
-
/** Determine whether a value is a `Map`.
|
|
367
|
+
/** Determine whether a value is a `Map`.
|
|
368
|
+
*
|
|
369
|
+
* @example
|
|
370
|
+
* ```ts
|
|
371
|
+
* isMap(new Map()) // true
|
|
372
|
+
* isMap({}) // false
|
|
373
|
+
* ```
|
|
374
|
+
*/
|
|
204
375
|
function isMap(value) {
|
|
205
376
|
return value instanceof Map;
|
|
206
377
|
}
|
|
207
|
-
/** Determine whether a value is a `Set`.
|
|
378
|
+
/** Determine whether a value is a `Set`.
|
|
379
|
+
*
|
|
380
|
+
* @example
|
|
381
|
+
* ```ts
|
|
382
|
+
* isSet(new Set()) // true
|
|
383
|
+
* isSet([]) // false
|
|
384
|
+
* ```
|
|
385
|
+
*/
|
|
208
386
|
function isSet(value) {
|
|
209
387
|
return value instanceof Set;
|
|
210
388
|
}
|
|
211
|
-
/** Determine whether a value is a `WeakMap`.
|
|
389
|
+
/** Determine whether a value is a `WeakMap`.
|
|
390
|
+
*
|
|
391
|
+
* @example
|
|
392
|
+
* ```ts
|
|
393
|
+
* isWeakMap(new WeakMap()) // true
|
|
394
|
+
* isWeakMap({}) // false
|
|
395
|
+
* ```
|
|
396
|
+
*/
|
|
212
397
|
function isWeakMap(value) {
|
|
213
398
|
return value instanceof WeakMap;
|
|
214
399
|
}
|
|
215
|
-
/** Determine whether a value is a `WeakSet`.
|
|
400
|
+
/** Determine whether a value is a `WeakSet`.
|
|
401
|
+
*
|
|
402
|
+
* @example
|
|
403
|
+
* ```ts
|
|
404
|
+
* isWeakSet(new WeakSet()) // true
|
|
405
|
+
* isWeakSet({}) // false
|
|
406
|
+
* ```
|
|
407
|
+
*/
|
|
216
408
|
function isWeakSet(value) {
|
|
217
409
|
return value instanceof WeakSet;
|
|
218
410
|
}
|
|
219
|
-
/** Determine whether a value is an array.
|
|
411
|
+
/** Determine whether a value is an array.
|
|
412
|
+
*
|
|
413
|
+
* @example
|
|
414
|
+
* ```ts
|
|
415
|
+
* isArray([1, 2]) // true
|
|
416
|
+
* isArray('12') // false
|
|
417
|
+
* ```
|
|
418
|
+
*/
|
|
220
419
|
function isArray(value) {
|
|
221
420
|
return Array.isArray(value);
|
|
222
421
|
}
|
|
223
|
-
/** Determine whether a value is a `DataView`.
|
|
422
|
+
/** Determine whether a value is a `DataView`.
|
|
423
|
+
*
|
|
424
|
+
* @example
|
|
425
|
+
* ```ts
|
|
426
|
+
* isDataView(new DataView(new ArrayBuffer(8))) // true
|
|
427
|
+
* isDataView(new ArrayBuffer(8)) // false
|
|
428
|
+
* ```
|
|
429
|
+
*/
|
|
224
430
|
function isDataView(value) {
|
|
225
431
|
return value instanceof DataView;
|
|
226
432
|
}
|
|
227
|
-
/** Determine whether a value is an `ArrayBufferView` (any typed array or `DataView`).
|
|
433
|
+
/** Determine whether a value is an `ArrayBufferView` (any typed array or `DataView`).
|
|
434
|
+
*
|
|
435
|
+
* @example
|
|
436
|
+
* ```ts
|
|
437
|
+
* isArrayBufferView(new Uint8Array(4)) // true
|
|
438
|
+
* isArrayBufferView([1, 2, 3, 4]) // false
|
|
439
|
+
* ```
|
|
440
|
+
*/
|
|
228
441
|
function isArrayBufferView(value) {
|
|
229
442
|
return ArrayBuffer.isView(value);
|
|
230
443
|
}
|
|
231
|
-
/** Determine whether a value is an `Int8Array`.
|
|
444
|
+
/** Determine whether a value is an `Int8Array`.
|
|
445
|
+
*
|
|
446
|
+
* @example
|
|
447
|
+
* ```ts
|
|
448
|
+
* isInt8Array(new Int8Array(2)) // true
|
|
449
|
+
* isInt8Array(new Uint8Array(2)) // false
|
|
450
|
+
* ```
|
|
451
|
+
*/
|
|
232
452
|
function isInt8Array(value) {
|
|
233
453
|
return value instanceof Int8Array;
|
|
234
454
|
}
|
|
235
|
-
/** Determine whether a value is a `Uint8Array`.
|
|
455
|
+
/** Determine whether a value is a `Uint8Array`.
|
|
456
|
+
*
|
|
457
|
+
* @example
|
|
458
|
+
* ```ts
|
|
459
|
+
* isUint8Array(new Uint8Array(2)) // true
|
|
460
|
+
* isUint8Array(new Int8Array(2)) // false
|
|
461
|
+
* ```
|
|
462
|
+
*/
|
|
236
463
|
function isUint8Array(value) {
|
|
237
464
|
return value instanceof Uint8Array;
|
|
238
465
|
}
|
|
239
|
-
/** Determine whether a value is a `Uint8ClampedArray`.
|
|
466
|
+
/** Determine whether a value is a `Uint8ClampedArray`.
|
|
467
|
+
*
|
|
468
|
+
* @example
|
|
469
|
+
* ```ts
|
|
470
|
+
* isUint8ClampedArray(new Uint8ClampedArray(2)) // true
|
|
471
|
+
* isUint8ClampedArray(new Uint8Array(2)) // false
|
|
472
|
+
* ```
|
|
473
|
+
*/
|
|
240
474
|
function isUint8ClampedArray(value) {
|
|
241
475
|
return value instanceof Uint8ClampedArray;
|
|
242
476
|
}
|
|
243
|
-
/** Determine whether a value is an `Int16Array`.
|
|
477
|
+
/** Determine whether a value is an `Int16Array`.
|
|
478
|
+
*
|
|
479
|
+
* @example
|
|
480
|
+
* ```ts
|
|
481
|
+
* isInt16Array(new Int16Array(2)) // true
|
|
482
|
+
* isInt16Array(new Int8Array(2)) // false
|
|
483
|
+
* ```
|
|
484
|
+
*/
|
|
244
485
|
function isInt16Array(value) {
|
|
245
486
|
return value instanceof Int16Array;
|
|
246
487
|
}
|
|
247
|
-
/** Determine whether a value is a `Uint16Array`.
|
|
488
|
+
/** Determine whether a value is a `Uint16Array`.
|
|
489
|
+
*
|
|
490
|
+
* @example
|
|
491
|
+
* ```ts
|
|
492
|
+
* isUint16Array(new Uint16Array(2)) // true
|
|
493
|
+
* isUint16Array(new Int16Array(2)) // false
|
|
494
|
+
* ```
|
|
495
|
+
*/
|
|
248
496
|
function isUint16Array(value) {
|
|
249
497
|
return value instanceof Uint16Array;
|
|
250
498
|
}
|
|
251
|
-
/** Determine whether a value is an `Int32Array`.
|
|
499
|
+
/** Determine whether a value is an `Int32Array`.
|
|
500
|
+
*
|
|
501
|
+
* @example
|
|
502
|
+
* ```ts
|
|
503
|
+
* isInt32Array(new Int32Array(2)) // true
|
|
504
|
+
* isInt32Array(new Int16Array(2)) // false
|
|
505
|
+
* ```
|
|
506
|
+
*/
|
|
252
507
|
function isInt32Array(value) {
|
|
253
508
|
return value instanceof Int32Array;
|
|
254
509
|
}
|
|
255
|
-
/** Determine whether a value is a `Uint32Array`.
|
|
510
|
+
/** Determine whether a value is a `Uint32Array`.
|
|
511
|
+
*
|
|
512
|
+
* @example
|
|
513
|
+
* ```ts
|
|
514
|
+
* isUint32Array(new Uint32Array(2)) // true
|
|
515
|
+
* isUint32Array(new Int32Array(2)) // false
|
|
516
|
+
* ```
|
|
517
|
+
*/
|
|
256
518
|
function isUint32Array(value) {
|
|
257
519
|
return value instanceof Uint32Array;
|
|
258
520
|
}
|
|
259
|
-
/** Determine whether a value is a `Float32Array`.
|
|
521
|
+
/** Determine whether a value is a `Float32Array`.
|
|
522
|
+
*
|
|
523
|
+
* @example
|
|
524
|
+
* ```ts
|
|
525
|
+
* isFloat32Array(new Float32Array(2)) // true
|
|
526
|
+
* isFloat32Array(new Float64Array(2)) // false
|
|
527
|
+
* ```
|
|
528
|
+
*/
|
|
260
529
|
function isFloat32Array(value) {
|
|
261
530
|
return value instanceof Float32Array;
|
|
262
531
|
}
|
|
263
|
-
/** Determine whether a value is a `Float64Array`.
|
|
532
|
+
/** Determine whether a value is a `Float64Array`.
|
|
533
|
+
*
|
|
534
|
+
* @example
|
|
535
|
+
* ```ts
|
|
536
|
+
* isFloat64Array(new Float64Array(2)) // true
|
|
537
|
+
* isFloat64Array(new Float32Array(2)) // false
|
|
538
|
+
* ```
|
|
539
|
+
*/
|
|
264
540
|
function isFloat64Array(value) {
|
|
265
541
|
return value instanceof Float64Array;
|
|
266
542
|
}
|
|
@@ -270,6 +546,12 @@ function isFloat64Array(value) {
|
|
|
270
546
|
* @remarks
|
|
271
547
|
* Guards the global existence of `BigInt64Array` first — safe in environments
|
|
272
548
|
* that pre-date the BigInt typed-array additions.
|
|
549
|
+
*
|
|
550
|
+
* @example
|
|
551
|
+
* ```ts
|
|
552
|
+
* isBigInt64Array(new BigInt64Array(2)) // true
|
|
553
|
+
* isBigInt64Array(new Float64Array(2)) // false
|
|
554
|
+
* ```
|
|
273
555
|
*/
|
|
274
556
|
function isBigInt64Array(value) {
|
|
275
557
|
return typeof BigInt64Array !== "undefined" && value instanceof BigInt64Array;
|
|
@@ -280,53 +562,136 @@ function isBigInt64Array(value) {
|
|
|
280
562
|
* @remarks
|
|
281
563
|
* Guards the global existence of `BigUint64Array` first — safe in environments
|
|
282
564
|
* that pre-date the BigInt typed-array additions.
|
|
565
|
+
*
|
|
566
|
+
* @example
|
|
567
|
+
* ```ts
|
|
568
|
+
* isBigUint64Array(new BigUint64Array(2)) // true
|
|
569
|
+
* isBigUint64Array(new BigInt64Array(2)) // false
|
|
570
|
+
* ```
|
|
283
571
|
*/
|
|
284
572
|
function isBigUint64Array(value) {
|
|
285
573
|
return typeof BigUint64Array !== "undefined" && value instanceof BigUint64Array;
|
|
286
574
|
}
|
|
287
|
-
/** Determine whether a value is the empty string `''`.
|
|
575
|
+
/** Determine whether a value is the empty string `''`.
|
|
576
|
+
*
|
|
577
|
+
* @example
|
|
578
|
+
* ```ts
|
|
579
|
+
* isEmptyString('') // true
|
|
580
|
+
* isEmptyString('a') // false
|
|
581
|
+
* ```
|
|
582
|
+
*/
|
|
288
583
|
function isEmptyString(value) {
|
|
289
584
|
return isString(value) && value.length === 0;
|
|
290
585
|
}
|
|
291
|
-
/** Determine whether a value is an empty array.
|
|
586
|
+
/** Determine whether a value is an empty array.
|
|
587
|
+
*
|
|
588
|
+
* @example
|
|
589
|
+
* ```ts
|
|
590
|
+
* isEmptyArray([]) // true
|
|
591
|
+
* isEmptyArray([1]) // false
|
|
592
|
+
* ```
|
|
593
|
+
*/
|
|
292
594
|
function isEmptyArray(value) {
|
|
293
595
|
return isArray(value) && value.length === 0;
|
|
294
596
|
}
|
|
295
|
-
/** Determine whether a value is an empty plain object (no own string or enumerable symbol keys).
|
|
597
|
+
/** Determine whether a value is an empty plain object (no own string or enumerable symbol keys).
|
|
598
|
+
*
|
|
599
|
+
* @example
|
|
600
|
+
* ```ts
|
|
601
|
+
* isEmptyObject({}) // true
|
|
602
|
+
* isEmptyObject({ a: 1 }) // false
|
|
603
|
+
* ```
|
|
604
|
+
*/
|
|
296
605
|
function isEmptyObject(value) {
|
|
297
606
|
if (!isRecord(value)) return false;
|
|
298
607
|
return Object.keys(value).length === 0 && enumerableSymbolCount(value) === 0;
|
|
299
608
|
}
|
|
300
|
-
/** Determine whether a value is an empty `Map`.
|
|
609
|
+
/** Determine whether a value is an empty `Map`.
|
|
610
|
+
*
|
|
611
|
+
* @example
|
|
612
|
+
* ```ts
|
|
613
|
+
* isEmptyMap(new Map()) // true
|
|
614
|
+
* isEmptyMap(new Map([['a', 1]])) // false
|
|
615
|
+
* ```
|
|
616
|
+
*/
|
|
301
617
|
function isEmptyMap(value) {
|
|
302
618
|
return value instanceof Map && value.size === 0;
|
|
303
619
|
}
|
|
304
|
-
/** Determine whether a value is an empty `Set`.
|
|
620
|
+
/** Determine whether a value is an empty `Set`.
|
|
621
|
+
*
|
|
622
|
+
* @example
|
|
623
|
+
* ```ts
|
|
624
|
+
* isEmptySet(new Set()) // true
|
|
625
|
+
* isEmptySet(new Set([1])) // false
|
|
626
|
+
* ```
|
|
627
|
+
*/
|
|
305
628
|
function isEmptySet(value) {
|
|
306
629
|
return value instanceof Set && value.size === 0;
|
|
307
630
|
}
|
|
308
|
-
/** Determine whether a value is a non-empty string (at least one character).
|
|
631
|
+
/** Determine whether a value is a non-empty string (at least one character).
|
|
632
|
+
*
|
|
633
|
+
* @example
|
|
634
|
+
* ```ts
|
|
635
|
+
* isNonEmptyString('a') // true
|
|
636
|
+
* isNonEmptyString('') // false
|
|
637
|
+
* ```
|
|
638
|
+
*/
|
|
309
639
|
function isNonEmptyString(value) {
|
|
310
640
|
return isString(value) && value.length > 0;
|
|
311
641
|
}
|
|
312
|
-
/** Determine whether a value is a non-empty array (at least one element).
|
|
642
|
+
/** Determine whether a value is a non-empty array (at least one element).
|
|
643
|
+
*
|
|
644
|
+
* @example
|
|
645
|
+
* ```ts
|
|
646
|
+
* isNonEmptyArray([1]) // true
|
|
647
|
+
* isNonEmptyArray([]) // false
|
|
648
|
+
* ```
|
|
649
|
+
*/
|
|
313
650
|
function isNonEmptyArray(value) {
|
|
314
651
|
return isArray(value) && value.length > 0;
|
|
315
652
|
}
|
|
316
|
-
/** Determine whether a value is a non-empty plain object (at least one own string or enumerable symbol key).
|
|
653
|
+
/** Determine whether a value is a non-empty plain object (at least one own string or enumerable symbol key).
|
|
654
|
+
*
|
|
655
|
+
* @example
|
|
656
|
+
* ```ts
|
|
657
|
+
* isNonEmptyObject({ a: 1 }) // true
|
|
658
|
+
* isNonEmptyObject({}) // false
|
|
659
|
+
* ```
|
|
660
|
+
*/
|
|
317
661
|
function isNonEmptyObject(value) {
|
|
318
662
|
if (!isRecord(value)) return false;
|
|
319
663
|
return Object.keys(value).length > 0 || enumerableSymbolCount(value) > 0;
|
|
320
664
|
}
|
|
321
|
-
/** Determine whether a value is a non-empty `Map` (at least one entry).
|
|
665
|
+
/** Determine whether a value is a non-empty `Map` (at least one entry).
|
|
666
|
+
*
|
|
667
|
+
* @example
|
|
668
|
+
* ```ts
|
|
669
|
+
* isNonEmptyMap(new Map([['a', 1]])) // true
|
|
670
|
+
* isNonEmptyMap(new Map()) // false
|
|
671
|
+
* ```
|
|
672
|
+
*/
|
|
322
673
|
function isNonEmptyMap(value) {
|
|
323
674
|
return value instanceof Map && value.size > 0;
|
|
324
675
|
}
|
|
325
|
-
/** Determine whether a value is a non-empty `Set` (at least one element).
|
|
676
|
+
/** Determine whether a value is a non-empty `Set` (at least one element).
|
|
677
|
+
*
|
|
678
|
+
* @example
|
|
679
|
+
* ```ts
|
|
680
|
+
* isNonEmptySet(new Set([1])) // true
|
|
681
|
+
* isNonEmptySet(new Set()) // false
|
|
682
|
+
* ```
|
|
683
|
+
*/
|
|
326
684
|
function isNonEmptySet(value) {
|
|
327
685
|
return value instanceof Set && value.size > 0;
|
|
328
686
|
}
|
|
329
|
-
/** Determine whether a value is a function that declares zero parameters (`Function.length === 0`).
|
|
687
|
+
/** Determine whether a value is a function that declares zero parameters (`Function.length === 0`).
|
|
688
|
+
*
|
|
689
|
+
* @example
|
|
690
|
+
* ```ts
|
|
691
|
+
* isZeroArg(() => {}) // true
|
|
692
|
+
* isZeroArg((a) => a) // false
|
|
693
|
+
* ```
|
|
694
|
+
*/
|
|
330
695
|
function isZeroArg(value) {
|
|
331
696
|
return isFunction(value) && value.length === 0;
|
|
332
697
|
}
|
|
@@ -337,27 +702,68 @@ function isZeroArg(value) {
|
|
|
337
702
|
* Uses `constructor.name === 'AsyncFunction'` — not `instanceof`, which is
|
|
338
703
|
* unreliable across realms. The `?.` keeps the guard total (§14): a function
|
|
339
704
|
* whose `constructor` was nulled yields `undefined`, never a thrown `null.name`.
|
|
705
|
+
*
|
|
706
|
+
* @example
|
|
707
|
+
* ```ts
|
|
708
|
+
* isAsyncFunction(async () => {}) // true
|
|
709
|
+
* isAsyncFunction(() => {}) // false
|
|
710
|
+
* ```
|
|
340
711
|
*/
|
|
341
712
|
function isAsyncFunction(value) {
|
|
342
713
|
return isFunction(value) && value.constructor?.name === "AsyncFunction";
|
|
343
714
|
}
|
|
344
|
-
/** Determine whether a value is a generator function (`function*`).
|
|
715
|
+
/** Determine whether a value is a generator function (`function*`).
|
|
716
|
+
*
|
|
717
|
+
* @example
|
|
718
|
+
* ```ts
|
|
719
|
+
* isGeneratorFunction(function* () {}) // true
|
|
720
|
+
* isGeneratorFunction(() => {}) // false
|
|
721
|
+
* ```
|
|
722
|
+
*/
|
|
345
723
|
function isGeneratorFunction(value) {
|
|
346
724
|
return isFunction(value) && value.constructor?.name === "GeneratorFunction";
|
|
347
725
|
}
|
|
348
|
-
/** Determine whether a value is an async generator function (`async function*`).
|
|
726
|
+
/** Determine whether a value is an async generator function (`async function*`).
|
|
727
|
+
*
|
|
728
|
+
* @example
|
|
729
|
+
* ```ts
|
|
730
|
+
* isAsyncGeneratorFunction(async function* () {}) // true
|
|
731
|
+
* isAsyncGeneratorFunction(function* () {}) // false
|
|
732
|
+
* ```
|
|
733
|
+
*/
|
|
349
734
|
function isAsyncGeneratorFunction(value) {
|
|
350
735
|
return isFunction(value) && value.constructor?.name === "AsyncGeneratorFunction";
|
|
351
736
|
}
|
|
352
|
-
/** Determine whether a value is a zero-argument async function.
|
|
737
|
+
/** Determine whether a value is a zero-argument async function.
|
|
738
|
+
*
|
|
739
|
+
* @example
|
|
740
|
+
* ```ts
|
|
741
|
+
* isZeroArgAsync(async () => {}) // true
|
|
742
|
+
* isZeroArgAsync(async (a) => a) // false
|
|
743
|
+
* ```
|
|
744
|
+
*/
|
|
353
745
|
function isZeroArgAsync(value) {
|
|
354
746
|
return isZeroArg(value) && isAsyncFunction(value);
|
|
355
747
|
}
|
|
356
|
-
/** Determine whether a value is a zero-argument generator function.
|
|
748
|
+
/** Determine whether a value is a zero-argument generator function.
|
|
749
|
+
*
|
|
750
|
+
* @example
|
|
751
|
+
* ```ts
|
|
752
|
+
* isZeroArgGenerator(function* () {}) // true
|
|
753
|
+
* isZeroArgGenerator(function* (a) {}) // false
|
|
754
|
+
* ```
|
|
755
|
+
*/
|
|
357
756
|
function isZeroArgGenerator(value) {
|
|
358
757
|
return isZeroArg(value) && isGeneratorFunction(value);
|
|
359
758
|
}
|
|
360
|
-
/** Determine whether a value is a zero-argument async generator function.
|
|
759
|
+
/** Determine whether a value is a zero-argument async generator function.
|
|
760
|
+
*
|
|
761
|
+
* @example
|
|
762
|
+
* ```ts
|
|
763
|
+
* isZeroArgAsyncGenerator(async function* () {}) // true
|
|
764
|
+
* isZeroArgAsyncGenerator(async function* (a) {}) // false
|
|
765
|
+
* ```
|
|
766
|
+
*/
|
|
361
767
|
function isZeroArgAsyncGenerator(value) {
|
|
362
768
|
return isZeroArg(value) && isAsyncGeneratorFunction(value);
|
|
363
769
|
}
|
|
@@ -368,6 +774,12 @@ function isZeroArgAsyncGenerator(value) {
|
|
|
368
774
|
* Probes with `Reflect.construct(String, [], value)`: a real constructor
|
|
369
775
|
* succeeds, while arrow functions, plain functions, and non-functions throw
|
|
370
776
|
* and yield `false`. Never throws. Backs the `instanceOf` combinator.
|
|
777
|
+
*
|
|
778
|
+
* @example
|
|
779
|
+
* ```ts
|
|
780
|
+
* isConstructor(class X {}) // true
|
|
781
|
+
* isConstructor(() => {}) // false
|
|
782
|
+
* ```
|
|
371
783
|
*/
|
|
372
784
|
function isConstructor(value) {
|
|
373
785
|
if (!isFunction(value)) return false;
|
|
@@ -1004,6 +1416,13 @@ function optionalOf(guard) {
|
|
|
1004
1416
|
*
|
|
1005
1417
|
* @param value - The value to parse
|
|
1006
1418
|
* @returns A string, or `undefined`
|
|
1419
|
+
*
|
|
1420
|
+
* @example
|
|
1421
|
+
* ```ts
|
|
1422
|
+
* parseString('hi') // 'hi'
|
|
1423
|
+
* parseString(42) // '42'
|
|
1424
|
+
* parseString(true) // undefined
|
|
1425
|
+
* ```
|
|
1007
1426
|
*/
|
|
1008
1427
|
function parseString(value) {
|
|
1009
1428
|
if (isString(value)) return value;
|
|
@@ -1019,6 +1438,13 @@ function parseString(value) {
|
|
|
1019
1438
|
*
|
|
1020
1439
|
* @param value - The value to parse
|
|
1021
1440
|
* @returns A finite number, or `undefined`
|
|
1441
|
+
*
|
|
1442
|
+
* @example
|
|
1443
|
+
* ```ts
|
|
1444
|
+
* parseNumber(42) // 42
|
|
1445
|
+
* parseNumber('42') // 42
|
|
1446
|
+
* parseNumber('abc') // undefined
|
|
1447
|
+
* ```
|
|
1022
1448
|
*/
|
|
1023
1449
|
function parseNumber(value) {
|
|
1024
1450
|
if (typeof value === "number") return Number.isFinite(value) ? value : void 0;
|
|
@@ -1037,6 +1463,12 @@ function parseNumber(value) {
|
|
|
1037
1463
|
*
|
|
1038
1464
|
* @param value - The value to parse
|
|
1039
1465
|
* @returns A finite integer, or `undefined`
|
|
1466
|
+
*
|
|
1467
|
+
* @example
|
|
1468
|
+
* ```ts
|
|
1469
|
+
* parseInteger(42) // 42
|
|
1470
|
+
* parseInteger(3.14) // undefined
|
|
1471
|
+
* ```
|
|
1040
1472
|
*/
|
|
1041
1473
|
function parseInteger(value) {
|
|
1042
1474
|
const parsed = parseNumber(value);
|
|
@@ -1053,6 +1485,13 @@ function parseInteger(value) {
|
|
|
1053
1485
|
*
|
|
1054
1486
|
* @param value - The value to parse
|
|
1055
1487
|
* @returns A boolean, or `undefined`
|
|
1488
|
+
*
|
|
1489
|
+
* @example
|
|
1490
|
+
* ```ts
|
|
1491
|
+
* parseBoolean(true) // true
|
|
1492
|
+
* parseBoolean('1') // true
|
|
1493
|
+
* parseBoolean('nope') // undefined
|
|
1494
|
+
* ```
|
|
1056
1495
|
*/
|
|
1057
1496
|
function parseBoolean(value) {
|
|
1058
1497
|
if (typeof value === "boolean") return value;
|
|
@@ -1069,6 +1508,12 @@ function parseBoolean(value) {
|
|
|
1069
1508
|
*
|
|
1070
1509
|
* @param value - The value to parse
|
|
1071
1510
|
* @returns `null` on a successful parse, or `undefined`
|
|
1511
|
+
*
|
|
1512
|
+
* @example
|
|
1513
|
+
* ```ts
|
|
1514
|
+
* parseNull(null) // null
|
|
1515
|
+
* parseNull(undefined) // undefined
|
|
1516
|
+
* ```
|
|
1072
1517
|
*/
|
|
1073
1518
|
function parseNull(value) {
|
|
1074
1519
|
return isNull(value) ? value : void 0;
|
|
@@ -1093,6 +1538,12 @@ function parseRecord(value) {
|
|
|
1093
1538
|
* @param value - The value to parse
|
|
1094
1539
|
* @param guard - Optional element guard
|
|
1095
1540
|
* @returns The array, or `undefined`
|
|
1541
|
+
*
|
|
1542
|
+
* @example
|
|
1543
|
+
* ```ts
|
|
1544
|
+
* parseArray([1, 2]) // [1, 2]
|
|
1545
|
+
* parseArray([1, 'x'], isNumber) // undefined
|
|
1546
|
+
* ```
|
|
1096
1547
|
*/
|
|
1097
1548
|
function parseArray(value, guard) {
|
|
1098
1549
|
if (!isArray(value)) return void 0;
|
|
@@ -1113,6 +1564,12 @@ function parseArray(value, guard) {
|
|
|
1113
1564
|
*
|
|
1114
1565
|
* @param value - The value to parse
|
|
1115
1566
|
* @returns The value, or `undefined` when it is not a valid JSON value
|
|
1567
|
+
*
|
|
1568
|
+
* @example
|
|
1569
|
+
* ```ts
|
|
1570
|
+
* parseJSONValue({ a: 1 }) // { a: 1 }
|
|
1571
|
+
* parseJSONValue(Number.NaN) // undefined
|
|
1572
|
+
* ```
|
|
1116
1573
|
*/
|
|
1117
1574
|
function parseJSONValue(value) {
|
|
1118
1575
|
return isJSONValue(value) ? value : void 0;
|
|
@@ -1129,6 +1586,12 @@ function parseJSONValue(value) {
|
|
|
1129
1586
|
* @param value - The value to parse
|
|
1130
1587
|
* @param allowed - The permitted literal values
|
|
1131
1588
|
* @returns The matched literal (by identity), or `undefined`
|
|
1589
|
+
*
|
|
1590
|
+
* @example
|
|
1591
|
+
* ```ts
|
|
1592
|
+
* parseEnum('b', ['a', 'b', 'c']) // 'b'
|
|
1593
|
+
* parseEnum('z', ['a', 'b', 'c']) // undefined
|
|
1594
|
+
* ```
|
|
1132
1595
|
*/
|
|
1133
1596
|
function parseEnum(value, allowed) {
|
|
1134
1597
|
for (const option of allowed) if (Object.is(value, option)) return option;
|
|
@@ -1149,6 +1612,12 @@ function parseStringField(record, path) {
|
|
|
1149
1612
|
* @param record - The source record
|
|
1150
1613
|
* @param path - A property key, or a key path descending into nested objects
|
|
1151
1614
|
* @returns A finite number, or `undefined`
|
|
1615
|
+
*
|
|
1616
|
+
* @example
|
|
1617
|
+
* ```ts
|
|
1618
|
+
* parseNumberField({ age: '42' }, 'age') // 42
|
|
1619
|
+
* parseNumberField({}, 'age') // undefined
|
|
1620
|
+
* ```
|
|
1152
1621
|
*/
|
|
1153
1622
|
function parseNumberField(record, path) {
|
|
1154
1623
|
return parseNumber(resolveField(record, path));
|
|
@@ -1169,6 +1638,12 @@ function parseIntegerField(record, path) {
|
|
|
1169
1638
|
* @param record - The source record
|
|
1170
1639
|
* @param path - A property key, or a key path descending into nested objects
|
|
1171
1640
|
* @returns A boolean, or `undefined`
|
|
1641
|
+
*
|
|
1642
|
+
* @example
|
|
1643
|
+
* ```ts
|
|
1644
|
+
* parseBooleanField({ on: 'true' }, 'on') // true
|
|
1645
|
+
* parseBooleanField({}, 'on') // undefined
|
|
1646
|
+
* ```
|
|
1172
1647
|
*/
|
|
1173
1648
|
function parseBooleanField(record, path) {
|
|
1174
1649
|
return parseBoolean(resolveField(record, path));
|
|
@@ -1183,6 +1658,12 @@ function parseBooleanField(record, path) {
|
|
|
1183
1658
|
* @param record - The source record
|
|
1184
1659
|
* @param path - A property key, or a key path descending into nested objects
|
|
1185
1660
|
* @returns `null` on a successful parse, or `undefined`
|
|
1661
|
+
*
|
|
1662
|
+
* @example
|
|
1663
|
+
* ```ts
|
|
1664
|
+
* parseNullField({ value: null }, 'value') // null
|
|
1665
|
+
* parseNullField({}, 'value') // undefined
|
|
1666
|
+
* ```
|
|
1186
1667
|
*/
|
|
1187
1668
|
function parseNullField(record, path) {
|
|
1188
1669
|
return parseNull(resolveField(record, path));
|
|
@@ -1205,6 +1686,12 @@ function parseRecordField(record, path) {
|
|
|
1205
1686
|
* @param path - A property key, or a key path descending into nested objects
|
|
1206
1687
|
* @param guard - Optional element guard
|
|
1207
1688
|
* @returns An array, or `undefined`
|
|
1689
|
+
*
|
|
1690
|
+
* @example
|
|
1691
|
+
* ```ts
|
|
1692
|
+
* parseArrayField({ tags: [1, 2] }, 'tags') // [1, 2]
|
|
1693
|
+
* parseArrayField({}, 'tags') // undefined
|
|
1694
|
+
* ```
|
|
1208
1695
|
*/
|
|
1209
1696
|
function parseArrayField(record, path, guard) {
|
|
1210
1697
|
return parseArray(resolveField(record, path), guard);
|
|
@@ -1231,6 +1718,12 @@ function parseEnumField(record, path, allowed) {
|
|
|
1231
1718
|
* @param record - The source record
|
|
1232
1719
|
* @param path - A property key, or a key path descending into nested objects
|
|
1233
1720
|
* @returns The value, or `undefined`
|
|
1721
|
+
*
|
|
1722
|
+
* @example
|
|
1723
|
+
* ```ts
|
|
1724
|
+
* parseJSONValueField({ data: { a: 1 } }, 'data') // { a: 1 }
|
|
1725
|
+
* parseJSONValueField({}, 'data') // undefined
|
|
1726
|
+
* ```
|
|
1234
1727
|
*/
|
|
1235
1728
|
function parseJSONValueField(record, path) {
|
|
1236
1729
|
return parseJSONValue(resolveField(record, path));
|
|
@@ -1311,6 +1804,12 @@ function parseJSONAs(value, guard) {
|
|
|
1311
1804
|
*
|
|
1312
1805
|
* @param shape - The shape to validate
|
|
1313
1806
|
* @throws {Error} When the shape is malformed
|
|
1807
|
+
*
|
|
1808
|
+
* @example
|
|
1809
|
+
* ```ts
|
|
1810
|
+
* validateShape(stringShape({ min: 1, max: 10 })) // does not throw
|
|
1811
|
+
* validateShape(stringShape({ min: 10, max: 1 })) // throws
|
|
1812
|
+
* ```
|
|
1314
1813
|
*/
|
|
1315
1814
|
function validateShape(shape) {
|
|
1316
1815
|
switch (shape.type) {
|
|
@@ -1365,6 +1864,11 @@ function validateShape(shape) {
|
|
|
1365
1864
|
*
|
|
1366
1865
|
* @param shape - The shape to compile
|
|
1367
1866
|
* @returns The emitted JSON Schema
|
|
1867
|
+
*
|
|
1868
|
+
* @example
|
|
1869
|
+
* ```ts
|
|
1870
|
+
* compileSchema(stringShape({ min: 1 })) // { type: 'string', minLength: 1 }
|
|
1871
|
+
* ```
|
|
1368
1872
|
*/
|
|
1369
1873
|
function compileSchema(shape) {
|
|
1370
1874
|
switch (shape.type) {
|
|
@@ -1440,6 +1944,12 @@ function compileSchema(shape) {
|
|
|
1440
1944
|
*
|
|
1441
1945
|
* @param shape - The shape to compile
|
|
1442
1946
|
* @returns A guard narrowing to the shape's inferred type
|
|
1947
|
+
*
|
|
1948
|
+
* @example
|
|
1949
|
+
* ```ts
|
|
1950
|
+
* const isUser = compileGuard(objectShape({ name: stringShape() }))
|
|
1951
|
+
* isUser({ name: 'Ada' }) // true
|
|
1952
|
+
* ```
|
|
1443
1953
|
*/
|
|
1444
1954
|
function compileGuard(shape) {
|
|
1445
1955
|
switch (shape.type) {
|
|
@@ -1517,6 +2027,12 @@ function compileGuard(shape) {
|
|
|
1517
2027
|
*
|
|
1518
2028
|
* @param shape - The shape to compile
|
|
1519
2029
|
* @returns A parser yielding the shape's inferred type or `undefined`
|
|
2030
|
+
*
|
|
2031
|
+
* @example
|
|
2032
|
+
* ```ts
|
|
2033
|
+
* const parseUser = compileParser(objectShape({ name: stringShape() }))
|
|
2034
|
+
* parseUser({ name: 'Ada' }) // { name: 'Ada' }
|
|
2035
|
+
* ```
|
|
1520
2036
|
*/
|
|
1521
2037
|
function compileParser(shape) {
|
|
1522
2038
|
switch (shape.type) {
|
|
@@ -1656,6 +2172,11 @@ function compileParser(shape) {
|
|
|
1656
2172
|
* @param shape - The shape to generate from
|
|
1657
2173
|
* @param random - A seeded random source (defaults to `seededRandom(Date.now())`)
|
|
1658
2174
|
* @returns A value matching the shape
|
|
2175
|
+
*
|
|
2176
|
+
* @example
|
|
2177
|
+
* ```ts
|
|
2178
|
+
* compileGenerator(stringShape({ min: 1, max: 4 })) // a random string of 1-4 characters (seed a RandomFunction for determinism)
|
|
2179
|
+
* ```
|
|
1659
2180
|
*/
|
|
1660
2181
|
function compileGenerator(shape, random = seededRandom(Date.now())) {
|
|
1661
2182
|
switch (shape.type) {
|
|
@@ -1775,6 +2296,11 @@ function stringShape(options) {
|
|
|
1775
2296
|
*
|
|
1776
2297
|
* @param options - Optional bounds (`min` / `max`), `integer`, and `description`
|
|
1777
2298
|
* @returns A number shape
|
|
2299
|
+
*
|
|
2300
|
+
* @example
|
|
2301
|
+
* ```ts
|
|
2302
|
+
* const age = numberShape({ min: 0, max: 120 })
|
|
2303
|
+
* ```
|
|
1778
2304
|
*/
|
|
1779
2305
|
function numberShape(options) {
|
|
1780
2306
|
return {
|
|
@@ -1809,6 +2335,11 @@ function integerShape(options) {
|
|
|
1809
2335
|
*
|
|
1810
2336
|
* @param options - Optional `description`
|
|
1811
2337
|
* @returns A boolean shape
|
|
2338
|
+
*
|
|
2339
|
+
* @example
|
|
2340
|
+
* ```ts
|
|
2341
|
+
* const active = booleanShape({ description: 'Whether the record is active' })
|
|
2342
|
+
* ```
|
|
1812
2343
|
*/
|
|
1813
2344
|
function booleanShape(options) {
|
|
1814
2345
|
return {
|
|
@@ -1821,6 +2352,11 @@ function booleanShape(options) {
|
|
|
1821
2352
|
*
|
|
1822
2353
|
* @param options - Optional `description`
|
|
1823
2354
|
* @returns A null shape
|
|
2355
|
+
*
|
|
2356
|
+
* @example
|
|
2357
|
+
* ```ts
|
|
2358
|
+
* const empty = nullShape()
|
|
2359
|
+
* ```
|
|
1824
2360
|
*/
|
|
1825
2361
|
function nullShape(options) {
|
|
1826
2362
|
return {
|
|
@@ -1951,6 +2487,12 @@ function unionShape(...variants) {
|
|
|
1951
2487
|
*
|
|
1952
2488
|
* @param variants - The candidate shapes
|
|
1953
2489
|
* @returns A union shape with `mode: 'oneOf'`
|
|
2490
|
+
*
|
|
2491
|
+
* @example
|
|
2492
|
+
* ```ts
|
|
2493
|
+
* const id = oneOfShape(stringShape(), integerShape())
|
|
2494
|
+
* // Infer<typeof id> = string | number
|
|
2495
|
+
* ```
|
|
1954
2496
|
*/
|
|
1955
2497
|
function oneOfShape(...variants) {
|
|
1956
2498
|
return {
|
|
@@ -1980,6 +2522,12 @@ function optionalShape(inner) {
|
|
|
1980
2522
|
*
|
|
1981
2523
|
* @param inner - The wrapped shape
|
|
1982
2524
|
* @returns A nullable shape
|
|
2525
|
+
*
|
|
2526
|
+
* @example
|
|
2527
|
+
* ```ts
|
|
2528
|
+
* const bio = nullableShape(stringShape())
|
|
2529
|
+
* // Infer<typeof bio> = string | null
|
|
2530
|
+
* ```
|
|
1983
2531
|
*/
|
|
1984
2532
|
function nullableShape(inner) {
|
|
1985
2533
|
return {
|
|
@@ -1997,6 +2545,11 @@ function nullableShape(inner) {
|
|
|
1997
2545
|
*
|
|
1998
2546
|
* @param options - Optional `description`
|
|
1999
2547
|
* @returns A JSON passthrough shape
|
|
2548
|
+
*
|
|
2549
|
+
* @example
|
|
2550
|
+
* ```ts
|
|
2551
|
+
* const payload = jsonShape({ description: 'Arbitrary JSON payload' })
|
|
2552
|
+
* ```
|
|
2000
2553
|
*/
|
|
2001
2554
|
function jsonShape(options) {
|
|
2002
2555
|
return {
|
|
@@ -2013,6 +2566,11 @@ function jsonShape(options) {
|
|
|
2013
2566
|
*
|
|
2014
2567
|
* @param schema - The JSON Schema fragment to embed
|
|
2015
2568
|
* @returns A raw shape
|
|
2569
|
+
*
|
|
2570
|
+
* @example
|
|
2571
|
+
* ```ts
|
|
2572
|
+
* const custom = rawShape({ type: 'string', format: 'uuid' })
|
|
2573
|
+
* ```
|
|
2016
2574
|
*/
|
|
2017
2575
|
function rawShape(schema) {
|
|
2018
2576
|
return {
|