-
Notifications
You must be signed in to change notification settings - Fork 0
/
is.ts
1360 lines (1209 loc) · 44.2 KB
/
is.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Buffer, Buffer as DenoBuffer, NodeBuffer } from "./types.ts";
import type {
AccessorDescriptor,
Class,
DataDescriptor,
Falsy,
MapIterator,
Module,
NodeJS,
ObservableLike,
Predicate,
Primitive,
SetIterator,
TypedArray,
TypeName,
} from "./types.ts";
import {
assign,
deprecate,
freeze,
getObjectType,
isDomElement,
isObjectOfType,
isOfType,
isPrimitiveTypeName,
isTypedArrayName,
predicateOnArray,
} from "./_util.ts";
import {
type Assert,
type Assertions,
AssertionTypeDescription,
type AssertOptions,
} from "./assert.ts";
/**
* Determine the {@linkcode TypeName} of an arbitrary value of unknown type.
*
* @example
* ```ts
* import { is } from "https://deno.land/x/dis/mod.ts";
*
* is("🦕") // => "string"
* is(100n) // => "bigint"
* is({ foo: "bar" }) // => "Object"
* is(new Uint8Array()) // => "Uint8Array"
* ```
*/
function is(value: unknown): TypeName {
if (value === null) {
return "null";
}
switch (typeof value) {
case "undefined":
return "undefined";
case "string":
return "string";
case "number":
return Number.isNaN(value) ? "NaN" : "number";
case "boolean":
return "boolean";
case "function":
return "Function";
case "bigint":
return "bigint";
case "symbol":
return "symbol";
default:
}
if (is.observable(value)) {
return "Observable";
}
if (is.array(value)) {
return "Array";
}
if (is.nodeBuffer(value)) {
return "Buffer";
}
if (is.denoBuffer(value)) {
return "Buffer";
}
if (is.boxedPrimitive(value)) {
throw new TypeError("Please don't use object wrappers for primitive types");
}
return getObjectType(value) ?? "Object";
}
is.undefined = isOfType<undefined>("undefined");
is.string = isOfType<string>("string");
is.number = (value: unknown): value is number =>
isOfType<number>("number")(value) && !is.NaN(value);
is.bigint = isOfType<bigint>("bigint");
is.boolean = (value: unknown): value is boolean =>
value === true || value === false;
is.symbol = isOfType<symbol>("symbol");
is.array = <T = unknown>(
value: unknown,
assertion?: (value: T) => value is T,
): value is T[] => {
if (!Array.isArray(value)) return false;
if (!is.function(assertion)) return true;
return value.every(assertion);
};
/** Check if a value is `null`, using strict equality comparison. */
is.null = (value: unknown): value is null => value === null;
is.nullOrUndefined = (value: unknown): value is null | undefined =>
is.null(value) || is.undefined(value);
type nullish = null | undefined;
/** @see {@linkcode is.nullOrUndefined} */
is.nullish = (value: unknown): value is nullish => is.nullOrUndefined(value);
/**
* Check if a value is of the generic "object" type, and not null. This will
* also check true for Function and builtin objects like Map, Set, etc.
*
* If you need a higher level of precision, consider using typeguards such as
* {@linkcode is.plainObject}, {@linkcode is.nonEmptyObject}, {@linkcode is.}
*/
is.object = (value: unknown): value is object =>
!is.null(value) && (typeof value === "object" || is.function(value));
/**
* Tests if a value is either null or of a primitive type. Possible values:
* - `string`
* - `number`
* - `bigint`
* - `boolean`
* - `symbol`
* @param value The value to inspect.
* @returns `boolean`
*/
is.primitive = (value: unknown): value is Primitive =>
is.null(value) || isPrimitiveTypeName(typeof value);
is.boxedPrimitive = (value: unknown): boolean => {
if (!is.object(value)) return false;
const typeName = toString.call(value).slice(8, -1);
return (
((value as any) instanceof String && typeName === "String") ||
((value as any) instanceof Number && typeName === "Number") ||
((value as any) instanceof BigInt && typeName === "BigInt") ||
((value as any) instanceof Symbol && typeName === "Symbol") ||
((value as any) instanceof Boolean && typeName === "Boolean")
);
};
/**
* Check if a value is truthy, meaning it cannot be falsy (`false`, `0`, `""`,
* `undefined`, or `null`).
*
* @param value The value to inspect.
* @returns `boolean`
*
* @example ```ts
* is.truthy = (value: unknown): value is (not false | not 0 | not '' | not undefined | not null) => Boolean(value);
* ```
*/
is.truthy = <T>(value: T | Falsy): value is T => Boolean(value);
/**
* Check if a value is falsy: `false`, `0`, `""`, `undefined` or `null`.
*
* @param value The value to inspect.
* @returns `boolean`
*
* @example ```ts
* is.falsy = (value: unknown): value is
* (not true | 0 | '' | undefined | null) => Boolean(value);
* ```
*/
is.falsy = <T>(value: T | Falsy): value is Falsy => !value;
/**
* Alias for `is.falsy`.
* @see {@link is.falsy}
*/
is.falsey = <T>(value: T | Falsy): value is Falsy => !value;
is.function = isOfType<Function>("function");
is.iterable = <T = unknown>(value: unknown): value is Iterable<T> =>
is.function((value as Iterable<T>)?.[Symbol.iterator]);
is.generator = (value: unknown): value is Generator =>
is.iterable(value) && is.function((value as Generator)?.next) &&
is.function((value as Generator)?.throw);
is.generatorFunction = isObjectOfType<GeneratorFunction>("GeneratorFunction");
is.asyncFunction = <T = unknown>(
value: unknown,
): value is (...args: any[]) => Promise<T> =>
getObjectType(value) === "AsyncFunction";
is.asyncIterable = <T = unknown>(value: unknown): value is AsyncIterable<T> =>
is.function((value as AsyncIterable<T>)?.[Symbol.asyncIterator]);
is.asyncGenerator = (value: unknown): value is AsyncGenerator =>
is.asyncIterable(value) && is.function((value as AsyncGenerator).next) &&
is.function((value as AsyncGenerator).throw);
is.asyncGeneratorFunction = (
value: unknown,
): value is (...args: any[]) => Promise<unknown> =>
getObjectType(value) === "AsyncGeneratorFunction";
is.boundFunction = (value: unknown): value is Function =>
is.function(value) &&
!Object.prototype.hasOwnProperty.call(value, "prototype");
/**
* Check if a Function appears to be a constructable Class.
*/
is.class = <Proto = unknown>(value: unknown): value is Class<Proto> => {
if (!is.function(value)) return false;
if (!is.object(value.prototype)) return false;
return Function.prototype.toString.call(value).startsWith("class ");
};
/**
* Check if a value is a direct instance of a class.
* @param instance The value to inspect.
* @param class_ the class to check against
* @returns `boolean`
*/
is.directInstanceOf = <T>(instance: unknown, class_: Class<T>): instance is T =>
Object.getPrototypeOf(instance) === class_.prototype;
is.instanceOf = <T>(instance: unknown, class_: Class<T>): instance is T =>
(is.directInstanceOf(instance, class_)) || (instance instanceof class_);
is.promise = <T = unknown>(value: unknown): value is Promise<T> =>
is.nativePromise(value) || hasPromiseApi(value);
is.nativePromise = <T = unknown>(value: unknown): value is Promise<T> =>
isObjectOfType<Promise<T>>("Promise")(value);
const hasPromiseApi = <T = unknown>(value: unknown): value is Promise<T> =>
is.function((value as Promise<T>)?.then) &&
is.function((value as Promise<T>)?.catch);
is.regExp = isObjectOfType<RegExp>("RegExp");
is.regex = isObjectOfType<RegExp>("RegExp");
is.date = isObjectOfType<Date>("Date");
is.error = isObjectOfType<Error>("Error");
is.map = <Key = unknown, Value = unknown>(
value: unknown,
): value is Map<Key, Value> => isObjectOfType<Map<Key, Value>>("Map")(value);
is.set = <T = unknown>(value: unknown): value is Set<T> =>
isObjectOfType<Set<T>>("Set")(value);
is.weakMap = <Key extends object = object, Value = unknown>(
value: unknown,
): value is WeakMap<Key, Value> =>
isObjectOfType<WeakMap<Key, Value>>("WeakMap")(value);
is.weakSet = (value: unknown): value is WeakSet<object> =>
isObjectOfType<WeakSet<object>>("WeakSet")(value);
is.weakRef = (value: unknown): value is WeakRef<object> =>
isObjectOfType<WeakRef<object>>("WeakRef")(value);
is.mapIterator = isObjectOfType<MapIterator>("Map Iterator");
is.setIterator = isObjectOfType<SetIterator>("Set Iterator");
is.namespaceModule = isObjectOfType<Module>("Module");
/**
* Check if a value is a plain object, with extensive checks to ensure we
* aren't actually dealing with an array, function, or other object type.
*
* @param value The value to inspect.
* @returns `boolean`
*/
is.plainObject = <Value = unknown>(
value: unknown,
): value is Record<string, Value> => {
/**
* @see https://github.com/sindresorhus/is-plain-obj/blob/main/index.js
*/
if (typeof value !== "object" || value === null) {
return false;
}
const prototype = Object.getPrototypeOf(value);
return (prototype === null || prototype === Object.prototype ||
Object.getPrototypeOf(prototype) === null) &&
!(Symbol.toStringTag in value) && !(Symbol.iterator in value);
};
// ----------------------------------------------------------------- //
// Properties and Property Descriptors
// ----------------------------------------------------------------- //
/**
* `PropertyKey` is any value that is valid as an object key. Equivalent to
* `string | number | symbol`.
*/
is.propertyKey = (value: unknown): value is PropertyKey =>
is.any([is.string, is.number, is.symbol], value);
/**
* Checks if a value is a Property Descriptor, without discerning between the
* subtypes of Accessor Properties or Data Properties.
* @see {@linkcode is.accessorDescriptor} for the Accessor Descriptor check
* @see {@linkcode is.dataDescriptor} for the Data Descriptor check
*/
is.propertyDescriptor = <T = any>(
value: unknown,
): value is TypedPropertyDescriptor<T> => {
return is.nonEmptyObject<keyof PropertyDescriptor>(value) &&
(is.accessorDescriptor(value) || is.dataDescriptor(value));
};
/**
* Checks if a value is a valid Accessor Descriptor, meaning it does not have
* `value` or `writable` properties, and **does** have a getter (`get`) and/or
* a setter (`set`) property.
* @param value The value to inspect.
* @returns `true` if the value is an accessor descriptor, else `false`
* @see {@linkcode is.dataDescriptor} for the Data Descriptor check
* @see {@linkcode is.propertyDescriptor} for the generic Descriptor check
*/
is.accessorDescriptor = <T = any>(
value: unknown,
): value is AccessorDescriptor<T> => {
if (is.nonEmptyObject<keyof PropertyDescriptor>(value)) {
return is.any(is.function, value.get, value.set) &&
is.all(is.undefined, value.writable, value.value);
}
return false;
};
/**
* Checks if a value is a valid Data Property Descriptor, meaning it does not
* have a `get` or `set` property, but is defined with a `value` data property.
* @param value The value to inspect.
* @returns `true` if the value is a data property descriptor, else `false`
* @see {@linkcode is.accessorDescriptor} for the Accessor Descriptor check
* @see {@linkcode is.propertyDescriptor} for the generic Descriptor check
*/
is.dataDescriptor = <T = any>(
value: unknown,
): value is DataDescriptor<T> => {
if (is.nonEmptyObject<keyof PropertyDescriptor>(value)) {
return !is.undefined(value.value) &&
is.none(is.function, value.get, value.set);
}
return false;
};
/**
* Checks if a key is included in those defined on a target object or array..
* @param value The value to inspect.
* @param target The object or array to check against.
* @returns `true` if the value is included in the target's keys, else `false`.
*/
is.key = <T extends readonly unknown[] | Record<PropertyKey, unknown> = {}>(
value: unknown,
target: T,
): value is keyof T => Object.keys(target).includes(value as string);
/**
* Checks if a value is included in a target object or array.
* @param value The value to inspect.
* @param target The object or array to check against.
* @returns `true` if the value is included in the target values, else `false`.
*/
is.value = <T extends readonly unknown[] | Record<PropertyKey, unknown> = {}>(
value: unknown,
target: T,
): value is T[keyof T] => Object.values(target).includes(value as string);
/**
* Checks if a key is included in a target enum's keys.
* @param value The value to inspect.
* @param targetEnum The enum to check against.
* @returns `true` if the key exists in the enum, else `false`.
*/
is.enumKey = <T = unknown>(
value: unknown,
targetEnum: T,
): value is keyof T =>
Object.keys(targetEnum as object).includes(String(value));
/**
* Checks if a value is included in a target enum.
* @param value The value to inspect.
* @param targetEnum The enum to check against.
* @returns `true` if the value is included in the enum, else `false`.
*/
is.enumCase = <T = unknown>(
value: unknown,
targetEnum: T,
): value is T[keyof T] => Object.values(targetEnum as object).includes(value);
/**
* Checks if a value is `ArrayLike<T>`. An "array-like" object is simply an
* object that has a numeric length property and 0-indexed numeric keys.
*
* @param value The value to inspect.
* @returns `boolean`
*/
is.arrayLike = <T = unknown>(value: unknown): value is ArrayLike<T> =>
!is.nullOrUndefined(value) && !is.function(value) &&
isValidLength((value as ArrayLike<T>).length);
/**
* Checks if a value is a valid `[key, value]` entry in the form of a
* tuple pair with a fixed-length of 2.
* @see {@linkcode is.entries}
*/
is.entry = <K = unknown, V = unknown>(value: unknown): value is [K, V] =>
is.nonEmptyArray(value) && value.length === 2;
/**
* Checks if a value is a collection of valid `[key, value]` entries, each of
* which has the form of a tuple pair with a fixed-length of 2.
* @see {@linkcode is.entry}
*/
is.entries = <K = unknown, V = unknown>(value: unknown): value is [K, V][] =>
is.nonEmptyArray(value) && is.array(value, is.entry);
is.sparseArray = (value: unknown): value is unknown[] =>
is.array(value) && (value.length !== value.filter((v) => v).length);
/**
* Check if a value is a TypedArray.
*
* @param value The value to inspect.
* @returns `boolean`
*/
is.typedArray = (value: unknown): value is TypedArray =>
isTypedArrayName(getObjectType(value));
is.int8Array = isObjectOfType<Int8Array>("Int8Array");
is.uint8Array = isObjectOfType<Uint8Array>("Uint8Array");
is.uint8ClampedArray = isObjectOfType<Uint8ClampedArray>("Uint8ClampedArray");
is.int16Array = isObjectOfType<Int16Array>("Int16Array");
is.uint16Array = isObjectOfType<Uint16Array>("Uint16Array");
is.int32Array = isObjectOfType<Int32Array>("Int32Array");
is.uint32Array = isObjectOfType<Uint32Array>("Uint32Array");
is.float32Array = isObjectOfType<Float32Array>("Float32Array");
is.float64Array = isObjectOfType<Float64Array>("Float64Array");
is.bigInt64Array = isObjectOfType<BigInt64Array>("BigInt64Array");
is.bigUint64Array = isObjectOfType<BigUint64Array>("BigUint64Array");
is.arrayBuffer = isObjectOfType<ArrayBuffer>("ArrayBuffer");
/**
* Checks if the given value is a valid `SharedArrayBuffer`.
* @param value The value to inspect.
* @returns `true` if the value is a valid `SharedArrayBuffer`, else `false`.
* @example ```ts
* import { is } from "is";
* if (is.sharedArrayBuffer(new SharedArrayBuffer(1))) {
* console.log("SharedArrayBuffer");
* }
*/
is.sharedArrayBuffer = isObjectOfType<SharedArrayBuffer>("SharedArrayBuffer");
/**
* Checks if the given value is an instance of `DataView` or `ArrayBufferView`.
* @param value The value to inspect.
* @returns `true` if the value is an instance of `DataView` or `ArrayBufferView`, else `false`.
* @example ```ts
* import { is } from "is";
* if (is.arrayBufferView(new DataView(new ArrayBuffer(1)))) {
* console.log("DataView");
* }
*/
is.dataView = isObjectOfType<DataView>("DataView");
/**
* Checks if a given value is a valid Node.js Buffer, using the `.isBuffer()`
* method (static) from the Buffer constructor. This does not perform any other
* checks, and should not be relied upon for matching potential `ArrayBuffer` or
* `Deno.Buffer` instances.
*/
is.nodeBuffer = (value: unknown): value is NodeBuffer =>
((value as any)?.constructor as typeof NodeBuffer)?.isBuffer?.(value) ??
false;
is.denoBuffer = (value: unknown): value is DenoBuffer =>
DenoBuffer?.[Symbol.hasInstance]?.(value) || is.instanceOf(value, DenoBuffer);
is.buffer = (value: unknown): value is Buffer =>
is.arrayBuffer(value) || is.denoBuffer(value) || is.nodeBuffer(value);
// ----------------------------------------------------------------- //
// Browser / Web API Related Objects
// ----------------------------------------------------------------- //
is.blob = (value: unknown): value is Blob =>
isObjectOfType<Blob>("Blob")(value);
is.formData = (value: unknown): value is FormData =>
isObjectOfType<FormData>("FormData")(value);
is.headers = (value: unknown): value is Headers =>
isObjectOfType<Headers>("Headers")(value);
is.request = (value: unknown): value is Request =>
isObjectOfType<Request>("Request")(value);
is.response = (value: unknown): value is Response =>
isObjectOfType<Response>("Response")(value);
is.urlSearchParams = (value: unknown): value is URLSearchParams =>
isObjectOfType<URLSearchParams>("URLSearchParams")(value);
/**
* Check if an value is a valid instance of the `URL` class.
* @param value The value to inspect.
* @returns `boolean`
* @see https://mdn.io/URL
*/
is.urlInstance = (value: unknown): value is URL =>
isObjectOfType<URL>("URL")(value);
/**
* Check if an arbitrary string is a valid URL.
* @param value The value to inspect.
* @returns `boolean`
*/
is.urlString = (value: unknown): value is string => {
if (!is.string(value)) return false;
try {
new URL(value);
return true;
} catch {
return false;
}
};
is.url = (value: unknown): value is string | URL =>
is.urlString(value) || is.urlInstance(value);
/**
* Check if a value is a DOM element.
*
* @param value The value to inspect.
* @returns `true` if the value is a DOM node.
*
* @example ```ts
* const div = document.createElement("div");
* is.domElement(div); // true
* ```
* @example ```ts
* const myElement = document.querySelector("#my-element");
* is.domElement(myElement); // true
* ```
*
* @example ```ts
* const astNode = { tagName: "div", id: "my-element" };
* is.domElement(astNode); // false
* ```
*/
is.domElement = (value: unknown): value is HTMLElement =>
is.object(value) && !is.plainObject(value) && isDomElement(value);
/**
* Check if a value is `Observable` or `ObservableLike`.
*
* @note An "observable" is an object that has a `subscribe` method, and a `Symbol.observable` property (sometimes referred to as "@@observable").
*
* @param value The value to inspect.
* @returns `true` if the value is an `Observable` or `ObservableLike`.
*/
is.observable = (value: unknown): value is ObservableLike => {
if (!value) {
return false;
}
if (value === (value as any)[Symbol.observable]?.()) {
return true;
}
if (value === (value as any)["@@observable"]?.()) {
return true;
}
return false;
};
is.nodeStream = (value: unknown): value is NodeJS.Stream =>
is.object(value) && is.function((value as NodeJS.Stream).pipe) &&
!is.observable(value);
// ----------------------------------------------------------------- //
// Numbers / Numerics
// ----------------------------------------------------------------- //
is.numericString = (value: unknown): value is string =>
is.string(value) &&
!is.emptyStringOrWhitespace(value) &&
!Number.isNaN(Number(value));
/**
* Equivalent to the JavaScript builtin `Number.isNaN`.
*
* @param value The value to inspect.
* @returns `boolean`
*/
is.NaN = (value: unknown) => Number.isNaN(value as number);
/**
* Alias for `is.NaN`.
* @see {@linkcode is.NaN}
*/
is.nan = (value: unknown) => Number.isNaN(value as number);
/**
* Strong typed alias for the builtin `Number.isInteger`.
*
* @param value The value to inspect.
* @returns `boolean`
*/
is.integer = (value: unknown): value is number =>
Number.isInteger(value as number);
/**
* Strong-typed alias for the builtin `Number.isSafeInteger`.
* @param value The value to inspect.
* @returns `boolean`
*/
is.safeInteger = (value: unknown): value is number =>
Number.isSafeInteger(value as number);
/**
* Check if a value is of the valid length for its given type.
* @param value
* @returns `boolean`
*/
const isValidLength = (value: unknown): value is number =>
is.safeInteger(value) && value >= 0;
is.infinite = (value: unknown): value is number =>
value === Number.POSITIVE_INFINITY || value === Number.NEGATIVE_INFINITY;
const isAbsoluteMod2 =
(remainder: number) => (value: number): value is number =>
is.integer(value) && Math.abs(value % 2) === remainder;
is.evenInteger = isAbsoluteMod2.apply(this, [0]);
is.oddInteger = isAbsoluteMod2(1);
/**
* Check if a numeric value conforms to a given range.
* @param value The value to inspect.
* @param range - the range to check against.
* @returns `true` if the value is within the legal bounds of the range.
* Otherwise, throws a `TypeError` similar to a `RangeError`.
*
* @example ```ts
* is.inRange = (value: number, range: [number, number]): boolean => {
* return value >= range[0] && value <= range[1];
* }
* ```
*/
is.inRange = (value: number, range: number | number[]): value is number => {
if (is.number(range)) {
return value >= Math.min(0, range) && value <= Math.max(range, 0);
}
if (is.array(range) && range.length === 2) {
return value >= Math.min(...range) && value <= Math.max(...range);
}
throw new TypeError(`Invalid range: ${JSON.stringify(range)}`);
};
// ----------------------------------------------------------------- //
// Emptiness Checks
// ----------------------------------------------------------------- //
is.emptyArray = (value: unknown): value is never[] =>
is.array(value) && value.length === 0;
is.emptySet = (value: unknown): value is Set<never> =>
is.set(value) && value.size === 0;
is.emptyMap = (value: unknown): value is Map<never, never> =>
is.map(value) && value.size === 0;
is.emptyObject = <Key extends keyof any = string>(
value: unknown,
): value is Record<Key, never> =>
is.object(value) && !is.map(value) && !is.set(value) &&
Object.keys(value).length === 0;
is.emptyString = (value: unknown): value is "" =>
is.string(value) && value.length === 0;
is.whitespace = (value: unknown): value is string =>
is.string(value) && !/\S/.test(value);
is.emptyStringOrWhitespace = (value: unknown): value is string =>
is.emptyString(value) || is.whitespace(value);
// ----------------------------------------------------------------- //
// Non-Emptiness Checks
// ----------------------------------------------------------------- //
is.nonEmptyArray = (value: unknown): value is [unknown, ...unknown[]] =>
is.array(value) && value.length > 0;
is.nonEmptySet = <T = unknown>(value: unknown): value is Set<T> =>
is.set(value) && value.size > 0;
is.nonEmptyMap = <Key = unknown, Value = unknown>(
value: unknown,
): value is Map<Key, Value> => is.map(value) && value.size > 0;
/**
* TODO: Use `not` operator here to remove `Map` and `Set` from type guard:
* https://github.com/Microsoft/TypeScript/pull/29317
*/
is.nonEmptyObject = <Key extends keyof any = string, Value = unknown>(
value: unknown,
): value is Record<Key, Value> =>
is.object(value) && !is.map(value) && !is.set(value) &&
Object.keys(value).length > 0;
// TODO: Use `not ''` when the `not` operator is available.
is.nonEmptyString = (value: unknown): value is string =>
is.string(value) && value.length > 0;
// TODO: Use `not ''` when the `not` operator is available.
is.nonEmptyStringAndNotWhitespace = (value: unknown): value is string =>
is.string(value) && !is.emptyStringOrWhitespace(value);
is.any = (
predicate: Predicate | Predicate[],
...values: unknown[]
): boolean => {
const predicates = is.array(predicate) ? predicate : [predicate];
return predicates.some((singlePredicate) =>
predicateOnArray(Array.prototype.some, singlePredicate, values)
);
};
is.some = (
predicate: Predicate | Predicate[],
...values: unknown[]
): boolean => {
return is.any(predicate, ...values);
};
is.all = (
predicate: Predicate | Predicate[],
...values: unknown[]
): boolean => {
const predicates = is.array(predicate) ? predicate : [predicate];
return predicates.every((singlePredicate) =>
predicateOnArray(Array.prototype.every, singlePredicate, values)
);
};
is.every = (
predicate: Predicate | Predicate[],
...values: unknown[]
): boolean => {
return is.all(predicate, ...values);
};
is.none = (
predicate: Predicate | Predicate[],
...values: unknown[]
): boolean => {
const predicates = is.array(predicate) ? predicate : [predicate];
return predicates.some((singlePredicate) =>
predicateOnArray(Array.prototype.some, singlePredicate, values)
);
};
/** @deprecated use {@linkcode is.function} instead */
is.function_ = isOfType<Function>("function");
/** @deprecated use {@linkcode is.class} instead */
is.class_ = <T>(value: unknown): value is Class<T> => is.class(value);
/**
* @deprecated use {@linkcode is.null} instead
*/
is.null_ = (value: unknown): value is null => value === null;
/* Type Assertions */
const assertType = (
condition: boolean,
description: string,
value: unknown,
options: { multipleValues?: boolean } = {},
): asserts condition => {
if (!condition) {
const { multipleValues } = options;
const valuesMessage = multipleValues
? `received values of types ${
[
...new Set(
(value as any[]).map((singleValue) => `\`${is(singleValue)}\``),
),
].join(", ")
}`
: `received value of type \`${is(value)}\``;
throw new TypeError(
`Expected value which is \`${description}\`, ${valuesMessage}.`,
);
}
};
/**
* Type Assertions. If conditions are not as expected, throws a TypeError.
*/
const assert: Assert = Object.assign(
function assert(
condition: boolean,
value: unknown,
message?: unknown,
options?: AssertOptions,
): asserts condition {
if (!condition) {
if (is.undefined(message) && is.undefined(options)) {
if (is.string(value)) {
throw new TypeError(`Assertion Failure: ${String(value)}`);
}
}
const { multipleValues = false } = (options || {});
const values = [...new Set([value as any].flat())];
const msg = multipleValues
? `values of types ${
values.map((v, i) =>
`${i === values.length - 1 ? "and " : ""}\`${is(v)}\``
).join(", ")
}`
: `value of type \`${is(value)}\``;
throw new TypeError(
`Assertion Failure: Expected value of \`${message}\` but received ${msg}.`,
);
}
},
{
// Unknowns.
undefined: (value: unknown): asserts value is undefined =>
assertType(is.undefined(value), "undefined", value),
null: (value: unknown): asserts value is null =>
assertType(is.null(value), "null", value),
nullOrUndefined: (value: unknown): asserts value is null | undefined =>
assertType(
is.nullOrUndefined(value),
AssertionTypeDescription.nullOrUndefined,
value,
),
string: (value: unknown): asserts value is string =>
assertType(is.string(value), "string", value),
number: (value: unknown): asserts value is number =>
assertType(is.number(value), "number", value),
bigint: (value: unknown): asserts value is bigint =>
assertType(is.bigint(value), "bigint", value),
function: (value: unknown): asserts value is Function =>
assertType(is.function(value), "Function", value),
class: <P = unknown>(value: unknown): asserts value is Class<P> =>
assertType(is.class<P>(value), AssertionTypeDescription.class_, value),
nullish: (value: unknown): asserts value is null | undefined =>
assertType(
is.nullOrUndefined(value),
AssertionTypeDescription.nullOrUndefined,
value,
),
object: (value: unknown): asserts value is object =>
assertType(is.object(value), "Object", value),
boolean: (value: unknown): asserts value is boolean =>
assertType(is.boolean(value), "boolean", value),
symbol: (value: unknown): asserts value is symbol =>
assertType(is.symbol(value), "symbol", value),
truthy: (value: unknown): asserts value is unknown =>
assertType(is.truthy(value), AssertionTypeDescription.truthy, value),
falsy: (value: unknown): asserts value is unknown =>
assertType(is.falsy(value), AssertionTypeDescription.falsy, value),
primitive: (value: unknown): asserts value is Primitive =>
assertType(
is.primitive(value),
AssertionTypeDescription.primitive,
value,
),
array: <T = unknown>(
value: unknown,
assertion?: (element: unknown) => asserts element is T,
): asserts value is T[] => {
const assert: (
condition: boolean,
description: string,
value: unknown,
) => asserts condition = assertType;
assert(is.array(value), "Array", value);
if (assertion) {
value.forEach(assertion);
}
},
iterable: <T = unknown>(value: unknown): asserts value is Iterable<T> =>
assertType(is.iterable(value), AssertionTypeDescription.iterable, value),
asyncIterable: <T = unknown>(
value: unknown,
): asserts value is AsyncIterable<T> =>
assertType(
is.asyncIterable(value),
AssertionTypeDescription.asyncIterable,
value,
),
generator: (value: unknown): asserts value is Generator =>
assertType(is.generator(value), "Generator", value),
asyncGenerator: (value: unknown): asserts value is AsyncGenerator =>
assertType(is.asyncGenerator(value), "AsyncGenerator", value),
nativePromise: <T = unknown>(value: unknown): asserts value is Promise<T> =>
assertType(
is.nativePromise(value),
AssertionTypeDescription.nativePromise,
value,
),
promise: <T = unknown>(value: unknown): asserts value is Promise<T> =>
assertType(is.promise(value), "Promise", value),
generatorFunction: (value: unknown): asserts value is GeneratorFunction =>
assertType(is.generatorFunction(value), "GeneratorFunction", value),
asyncGeneratorFunction: (
value: unknown,
): asserts value is AsyncGeneratorFunction =>
assertType(
is.asyncGeneratorFunction(value),
"AsyncGeneratorFunction",
value,
),
asyncFunction: (value: unknown): asserts value is Function =>
assertType(is.asyncFunction(value), "AsyncFunction", value),
boundFunction: (value: unknown): asserts value is Function =>
assertType(is.boundFunction(value), "Function", value),
directInstanceOf: <T>(
instance: unknown,
class_: Class<T>,
): asserts instance is T =>
assertType(
is.directInstanceOf(instance, class_),
AssertionTypeDescription.directInstanceOf,
instance,
),
instanceOf: <T>(
instance: unknown,
class_: Class<T>,
): asserts instance is T =>
assertType(
is.instanceOf(instance, class_),
AssertionTypeDescription.directInstanceOf,
instance,
),
regExp: (value: unknown): asserts value is RegExp =>
assertType(is.regExp(value), "RegExp", value),
regex: (value: unknown): asserts value is RegExp =>
assertType(is.regExp(value), "RegExp", value),
date: (value: unknown): asserts value is Date =>
assertType(is.date(value), "Date", value),
error: (value: unknown): asserts value is Error =>
assertType(is.error(value), "Error", value),
map: <Key = unknown, Value = unknown>(
value: unknown,
): asserts value is Map<Key, Value> =>
assertType(is.map(value), "Map", value),
set: <T = unknown>(value: unknown): asserts value is Set<T> =>
assertType(is.set(value), "Set", value),
weakMap: <Key extends object = object, Value = unknown>(
value: unknown,