0
# Type Checking & Validation
1
2
Comprehensive type checking utilities for runtime validation and type guards. Includes checks for all JavaScript primitive types, built-in objects, DOM elements, and web API types. Essential for robust error handling and input validation.
3
4
## Capabilities
5
6
### Primitive Type Checks
7
8
Type checking functions for JavaScript primitive types with TypeScript type guards.
9
10
```javascript { .api }
11
/**
12
* Check if value is an array
13
* @param val - Value to check
14
* @returns True if value is array
15
*/
16
function isArray(val: any): val is any[];
17
18
/**
19
* Check if value is a string
20
* @param val - Value to check
21
* @returns True if value is string
22
*/
23
function isString(val: any): val is string;
24
25
/**
26
* Check if value is a number (excluding NaN)
27
* @param val - Value to check
28
* @returns True if value is number
29
*/
30
function isNumber(val: any): val is number;
31
32
/**
33
* Check if value is a boolean
34
* @param val - Value to check
35
* @returns True if value is boolean
36
*/
37
function isBoolean(val: any): val is boolean;
38
39
/**
40
* Check if value is a function
41
* @param val - Value to check
42
* @returns True if value is function
43
*/
44
function isFunction(val: any): val is Function;
45
46
/**
47
* Check if value is a symbol
48
* @param val - Value to check
49
* @returns True if value is symbol
50
*/
51
function isSymbol(val: any): val is symbol;
52
```
53
54
### Object Type Checks
55
56
Type checking for various object types and complex structures.
57
58
```javascript { .api }
59
/**
60
* Check if value is an object (excluding null and arrays)
61
* @param val - Value to check
62
* @returns True if value is object
63
*/
64
function isObject(val: any): val is object;
65
66
/**
67
* Check if value is a plain object (created by {} or new Object())
68
* @param val - Value to check
69
* @returns True if value is plain object
70
*/
71
function isPlainObject(val: any): val is object;
72
73
/**
74
* Check if value is a Date object
75
* @param val - Value to check
76
* @returns True if value is Date
77
*/
78
function isDate(val: any): val is Date;
79
80
/**
81
* Check if value is a RegExp object
82
* @param val - Value to check
83
* @returns True if value is RegExp
84
*/
85
function isRegExp(val: any): val is RegExp;
86
87
/**
88
* Check if value is an Error object
89
* @param val - Value to check
90
* @returns True if value is Error
91
*/
92
function isError(val: any): val is Error;
93
94
/**
95
* Check if value is a TypeError object
96
* @param val - Value to check
97
* @returns True if value is TypeError
98
*/
99
function isTypeError(val: any): val is TypeError;
100
```
101
102
### Collection Type Checks
103
104
Type checking for ES6+ collection types.
105
106
```javascript { .api }
107
/**
108
* Check if value is a Map object
109
* @param val - Value to check
110
* @returns True if value is Map
111
*/
112
function isMap(val: any): val is Map<any, any>;
113
114
/**
115
* Check if value is a WeakMap object
116
* @param val - Value to check
117
* @returns True if value is WeakMap
118
*/
119
function isWeakMap(val: any): val is WeakMap<any, any>;
120
121
/**
122
* Check if value is a Set object
123
* @param val - Value to check
124
* @returns True if value is Set
125
*/
126
function isSet(val: any): val is Set<any>;
127
128
/**
129
* Check if value is a WeakSet object
130
* @param val - Value to check
131
* @returns True if value is WeakSet
132
*/
133
function isWeakSet(val: any): val is WeakSet<any>;
134
```
135
136
### Null/Undefined Checks
137
138
Specialized checks for null, undefined, and related states.
139
140
```javascript { .api }
141
/**
142
* Check if value is null
143
* @param val - Value to check
144
* @returns True if value is null
145
*/
146
function isNull(val: any): val is null;
147
148
/**
149
* Check if value is undefined
150
* @param val - Value to check
151
* @returns True if value is undefined
152
*/
153
function isUndefined(val: any): val is undefined;
154
155
/**
156
* Check if value is NaN
157
* @param val - Value to check
158
* @returns True if value is NaN
159
*/
160
function isNaN(val: any): boolean;
161
162
/**
163
* Check if value is null or undefined
164
* @param val1 - First value
165
* @param val2 - Second value (optional)
166
* @returns True if both values are null/undefined or equal
167
*/
168
function eqNull(val1: any, val2?: any): boolean;
169
```
170
171
### Number Type Checks
172
173
Specialized number type checking including finite, integer, and float checks.
174
175
```javascript { .api }
176
/**
177
* Check if value is a finite number
178
* @param val - Value to check
179
* @returns True if value is finite number
180
*/
181
function isFinite(val: any): boolean;
182
183
/**
184
* Check if value is an integer
185
* @param val - Value to check
186
* @returns True if value is integer
187
*/
188
function isInteger(val: any): boolean;
189
190
/**
191
* Check if value is a float (non-integer number)
192
* @param val - Value to check
193
* @returns True if value is float
194
*/
195
function isFloat(val: any): boolean;
196
```
197
198
### DOM & Web API Checks
199
200
Type checking for browser and DOM-related objects.
201
202
```javascript { .api }
203
/**
204
* Check if value is a DOM element
205
* @param val - Value to check
206
* @returns True if value is DOM element
207
*/
208
function isElement(val: any): val is Element;
209
210
/**
211
* Check if value is a Document object
212
* @param val - Value to check
213
* @returns True if value is Document
214
*/
215
function isDocument(val: any): val is Document;
216
217
/**
218
* Check if value is a Window object
219
* @param val - Value to check
220
* @returns True if value is Window
221
*/
222
function isWindow(val: any): val is Window;
223
224
/**
225
* Check if value is a FormData object
226
* @param val - Value to check
227
* @returns True if value is FormData
228
*/
229
function isFormData(val: any): val is FormData;
230
231
/**
232
* Check if value is an Arguments object
233
* @param val - Value to check
234
* @returns True if value is Arguments
235
*/
236
function isArguments(val: any): val is IArguments;
237
```
238
239
### Value State Checks
240
241
Utilities for checking value states and emptiness.
242
243
```javascript { .api }
244
/**
245
* Check if value is empty (null, undefined, empty string, empty array, empty object)
246
* @param val - Value to check
247
* @returns True if value is considered empty
248
*/
249
function isEmpty(val: any): boolean;
250
251
/**
252
* Check if date is a leap year
253
* @param date - Date to check
254
* @returns True if year is leap year
255
*/
256
function isLeapYear(date: Date | number): boolean;
257
258
/**
259
* Check if date is valid
260
* @param date - Date to validate
261
* @returns True if date is valid
262
*/
263
function isValidDate(date: any): boolean;
264
```
265
266
### Deep Comparison
267
268
Advanced comparison utilities for deep equality checking.
269
270
```javascript { .api }
271
/**
272
* Deep equality comparison
273
* @param val1 - First value
274
* @param val2 - Second value
275
* @returns True if values are deeply equal
276
*/
277
function isEqual(val1: any, val2: any): boolean;
278
279
/**
280
* Deep equality comparison with custom comparator
281
* @param val1 - First value
282
* @param val2 - Second value
283
* @param callback - Custom comparison function
284
* @returns True if values are equal according to callback
285
*/
286
function isEqualWith(
287
val1: any,
288
val2: any,
289
callback: (value1: any, value2: any, key?: string) => boolean | undefined
290
): boolean;
291
292
/**
293
* Check if object matches source properties
294
* @param obj - Object to check
295
* @param source - Source object with properties to match
296
* @returns True if object contains all source properties with same values
297
*/
298
function isMatch(obj: any, source: any): boolean;
299
```
300
301
### Type Information
302
303
Utilities for getting detailed type information.
304
305
```javascript { .api }
306
/**
307
* Get detailed type of value
308
* @param val - Value to analyze
309
* @returns Detailed type string
310
*/
311
function getType(val: any): string;
312
```
313
314
**Usage Examples:**
315
316
```javascript
317
import {
318
isArray, isString, isNumber, isObject, isEmpty,
319
isEqual, isValidDate, getType
320
} from 'xe-utils';
321
322
// Basic type checking
323
const data = ['hello', 42, { name: 'Alice' }, null];
324
325
data.forEach(item => {
326
if (isString(item)) {
327
console.log(`String: ${item.toUpperCase()}`);
328
} else if (isNumber(item)) {
329
console.log(`Number: ${item * 2}`);
330
} else if (isObject(item)) {
331
console.log(`Object:`, item);
332
}
333
});
334
335
// Validation
336
function validateUser(user) {
337
if (isEmpty(user)) {
338
throw new Error('User data is required');
339
}
340
341
if (!isString(user.name) || isEmpty(user.name)) {
342
throw new Error('Valid name is required');
343
}
344
345
if (!isNumber(user.age) || user.age < 0) {
346
throw new Error('Valid age is required');
347
}
348
349
return true;
350
}
351
352
// Deep comparison
353
const obj1 = { user: { name: 'Alice', preferences: { theme: 'dark' } } };
354
const obj2 = { user: { name: 'Alice', preferences: { theme: 'dark' } } };
355
356
console.log(isEqual(obj1, obj2)); // true
357
358
// Type information
359
console.log(getType([])); // 'Array'
360
console.log(getType(new Date())); // 'Date'
361
console.log(getType(/regex/)); // 'RegExp'
362
```