npm-lodash

Description
Comprehensive JavaScript utility library with 300+ methods for arrays, objects, strings, functions, and more.
Author
tessl
Last updated

How to use

npx @tessl/cli registry install tessl/npm-lodash@4.5.0

type-checking.md docs/

1
# Type Checking
2
3
Comprehensive type checking utilities for determining data types and validating values. These methods provide reliable type detection and comparison operations for JavaScript values.
4
5
## Capabilities
6
7
### Basic Type Checking
8
9
#### Primitive Types
10
Check for JavaScript primitive types.
11
12
```javascript { .api }
13
/**
14
* Checks if value is classified as a boolean primitive or object
15
* @param value - The value to check
16
* @returns Returns true if value is a boolean, else false
17
*/
18
function isBoolean(value);
19
20
/**
21
* Checks if value is classified as a Number primitive or object
22
* @param value - The value to check
23
* @returns Returns true if value is a number, else false
24
*/
25
function isNumber(value);
26
27
/**
28
* Checks if value is classified as a String primitive or object
29
* @param value - The value to check
30
* @returns Returns true if value is a string, else false
31
*/
32
function isString(value);
33
34
/**
35
* Checks if value is classified as a Symbol primitive or object
36
* @param value - The value to check
37
* @returns Returns true if value is a symbol, else false
38
*/
39
function isSymbol(value);
40
```
41
42
#### Null & Undefined
43
Check for null, undefined, and nil values.
44
45
```javascript { .api }
46
/**
47
* Checks if value is null
48
* @param value - The value to check
49
* @returns Returns true if value is null, else false
50
*/
51
function isNull(value);
52
53
/**
54
* Checks if value is undefined
55
* @param value - The value to check
56
* @returns Returns true if value is undefined, else false
57
*/
58
function isUndefined(value);
59
60
/**
61
* Checks if value is null or undefined
62
* @param value - The value to check
63
* @returns Returns true if value is nullish, else false
64
*/
65
function isNil(value);
66
```
67
68
### Object Type Checking
69
70
#### Object Detection
71
Check for various object types.
72
73
```javascript { .api }
74
/**
75
* Checks if value is the language type of Object
76
* @param value - The value to check
77
* @returns Returns true if value is an object, else false
78
*/
79
function isObject(value);
80
81
/**
82
* Checks if value is object-like (i.e. not null and typeof result of "object")
83
* @param value - The value to check
84
* @returns Returns true if value is object-like, else false
85
*/
86
function isObjectLike(value);
87
88
/**
89
* Checks if value is a plain object, that is, an object created by the Object constructor or one with a [[Prototype]] of null
90
* @param value - The value to check
91
* @returns Returns true if value is a plain object, else false
92
*/
93
function isPlainObject(value);
94
```
95
96
#### Array & Array-like
97
Check for arrays and array-like objects.
98
99
```javascript { .api }
100
/**
101
* Checks if value is classified as an Array object
102
* @param value - The value to check
103
* @returns Returns true if value is an array, else false
104
*/
105
function isArray(value);
106
107
/**
108
* Checks if value is array-like
109
* @param value - The value to check
110
* @returns Returns true if value is array-like, else false
111
*/
112
function isArrayLike(value);
113
114
/**
115
* Checks if value is array-like and not a function or constructor
116
* @param value - The value to check
117
* @returns Returns true if value is an array-like object, else false
118
*/
119
function isArrayLikeObject(value);
120
```
121
122
#### Function Detection
123
Check for various function types.
124
125
```javascript { .api }
126
/**
127
* Checks if value is classified as a Function object
128
* @param value - The value to check
129
* @returns Returns true if value is a function, else false
130
*/
131
function isFunction(value);
132
133
/**
134
* Checks if value is a pristine native function
135
* @param value - The value to check
136
* @returns Returns true if value is a native function, else false
137
*/
138
function isNative(value);
139
```
140
141
### Collection Type Checking
142
143
#### Built-in Collections
144
Check for built-in collection types.
145
146
```javascript { .api }
147
/**
148
* Checks if value is classified as a Map object
149
* @param value - The value to check
150
* @returns Returns true if value is a map, else false
151
*/
152
function isMap(value);
153
154
/**
155
* Checks if value is classified as a Set object
156
* @param value - The value to check
157
* @returns Returns true if value is a set, else false
158
*/
159
function isSet(value);
160
161
/**
162
* Checks if value is classified as a WeakMap object
163
* @param value - The value to check
164
* @returns Returns true if value is a weak map, else false
165
*/
166
function isWeakMap(value);
167
168
/**
169
* Checks if value is classified as a WeakSet object
170
* @param value - The value to check
171
* @returns Returns true if value is a weak set, else false
172
*/
173
function isWeakSet(value);
174
```
175
176
#### Typed Arrays
177
Check for typed array types.
178
179
```javascript { .api }
180
/**
181
* Checks if value is classified as a typed array
182
* @param value - The value to check
183
* @returns Returns true if value is a typed array, else false
184
*/
185
function isTypedArray(value);
186
187
/**
188
* Checks if value is classified as an ArrayBuffer object
189
* @param value - The value to check
190
* @returns Returns true if value is an array buffer, else false
191
*/
192
function isArrayBuffer(value);
193
```
194
195
### Special Object Types
196
197
#### Date & RegExp
198
Check for date and regular expression objects.
199
200
```javascript { .api }
201
/**
202
* Checks if value is classified as a Date object
203
* @param value - The value to check
204
* @returns Returns true if value is a date object, else false
205
*/
206
function isDate(value);
207
208
/**
209
* Checks if value is classified as a RegExp object
210
* @param value - The value to check
211
* @returns Returns true if value is a regexp, else false
212
*/
213
function isRegExp(value);
214
```
215
216
#### Error Objects
217
Check for error objects.
218
219
```javascript { .api }
220
/**
221
* Checks if value is an Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, or URIError object
222
* @param value - The value to check
223
* @returns Returns true if value is an error object, else false
224
*/
225
function isError(value);
226
```
227
228
#### DOM Elements
229
Check for DOM elements.
230
231
```javascript { .api }
232
/**
233
* Checks if value is likely a DOM element
234
* @param value - The value to check
235
* @returns Returns true if value is a DOM element, else false
236
*/
237
function isElement(value);
238
```
239
240
#### Arguments Object
241
Check for arguments objects.
242
243
```javascript { .api }
244
/**
245
* Checks if value is likely an arguments object
246
* @param value - The value to check
247
* @returns Returns true if value is an arguments object, else false
248
*/
249
function isArguments(value);
250
```
251
252
#### Buffer
253
Check for buffer objects.
254
255
```javascript { .api }
256
/**
257
* Checks if value is a buffer
258
* @param value - The value to check
259
* @returns Returns true if value is a buffer, else false
260
*/
261
function isBuffer(value);
262
```
263
264
### Number Type Checking
265
266
#### Number Validation
267
Check for various number conditions.
268
269
```javascript { .api }
270
/**
271
* Checks if value is a finite primitive number
272
* @param value - The value to check
273
* @returns Returns true if value is a finite number, else false
274
*/
275
function isFinite(value);
276
277
/**
278
* Checks if value is an integer
279
* @param value - The value to check
280
* @returns Returns true if value is an integer, else false
281
*/
282
function isInteger(value);
283
284
/**
285
* Checks if value is a safe integer
286
* @param value - The value to check
287
* @returns Returns true if value is a safe integer, else false
288
*/
289
function isSafeInteger(value);
290
291
/**
292
* Checks if value is NaN
293
* @param value - The value to check
294
* @returns Returns true if value is NaN, else false
295
*/
296
function isNaN(value);
297
```
298
299
#### Length Validation
300
Check for valid length values.
301
302
```javascript { .api }
303
/**
304
* Checks if value is a valid array-like length
305
* @param value - The value to check
306
* @returns Returns true if value is a valid length, else false
307
*/
308
function isLength(value);
309
```
310
311
### Value Testing
312
313
#### Emptiness
314
Check if values are empty.
315
316
```javascript { .api }
317
/**
318
* Checks if value is an empty object, collection, map, or set
319
* @param value - The value to check
320
* @returns Returns true if value is empty, else false
321
*/
322
function isEmpty(value);
323
```
324
325
### Value Comparison
326
327
#### Equality
328
Perform equality comparisons.
329
330
```javascript { .api }
331
/**
332
* Performs a SameValueZero comparison between two values to determine if they are equivalent
333
* @param value - The value to compare
334
* @param other - The other value to compare
335
* @returns Returns true if the values are equivalent, else false
336
*/
337
function eq(value, other);
338
339
/**
340
* Performs a deep comparison between two values to determine if they are equivalent
341
* @param value - The value to compare
342
* @param other - The other value to compare
343
* @returns Returns true if the values are equivalent, else false
344
*/
345
function isEqual(value, other);
346
347
/**
348
* Like isEqual except that it accepts customizer which is invoked to compare values
349
* @param value - The value to compare
350
* @param other - The other value to compare
351
* @param customizer - The function to customize comparisons
352
* @returns Returns true if the values are equivalent, else false
353
*/
354
function isEqualWith(value, other, customizer);
355
```
356
357
#### Matching
358
Check if objects match patterns.
359
360
```javascript { .api }
361
/**
362
* Performs a partial deep comparison between object and source to determine if object contains equivalent property values
363
* @param object - The object to inspect
364
* @param source - The object of property values to match
365
* @returns Returns true if object is a match, else false
366
*/
367
function isMatch(object, source);
368
369
/**
370
* Like isMatch except that it accepts customizer which is invoked to compare values
371
* @param object - The object to inspect
372
* @param source - The object of property values to match
373
* @param customizer - The function to customize comparisons
374
* @returns Returns true if object is a match, else false
375
*/
376
function isMatchWith(object, source, customizer);
377
```
378
379
#### Conformance
380
Check if objects conform to schemas.
381
382
```javascript { .api }
383
/**
384
* Checks if object conforms to source by invoking the predicate properties of source with the corresponding property values of object
385
* @param object - The object to inspect
386
* @param source - The object of property predicates to conform to
387
* @returns Returns true if object conforms, else false
388
*/
389
function conformsTo(object, source);
390
```
391
392
#### Relational Comparisons
393
Perform greater than and less than comparisons.
394
395
```javascript { .api }
396
/**
397
* Checks if value is greater than other
398
* @param value - The value to compare
399
* @param other - The other value to compare
400
* @returns Returns true if value is greater than other, else false
401
*/
402
function gt(value, other);
403
404
/**
405
* Checks if value is greater than or equal to other
406
* @param value - The value to compare
407
* @param other - The other value to compare
408
* @returns Returns true if value is greater than or equal to other, else false
409
*/
410
function gte(value, other);
411
412
/**
413
* Checks if value is less than other
414
* @param value - The value to compare
415
* @param other - The other value to compare
416
* @returns Returns true if value is less than other, else false
417
*/
418
function lt(value, other);
419
420
/**
421
* Checks if value is less than or equal to other
422
* @param value - The value to compare
423
* @param other - The other value to compare
424
* @returns Returns true if value is less than or equal to other, else false
425
*/
426
function lte(value, other);
427
```
428
429
### Type Conversion
430
431
#### Safe Conversion
432
Convert values to specific types safely.
433
434
```javascript { .api }
435
/**
436
* Converts value to an array
437
* @param value - The value to convert
438
* @returns Returns the converted array
439
*/
440
function toArray(value);
441
442
/**
443
* Converts value to a finite number
444
* @param value - The value to convert
445
* @returns Returns the converted number
446
*/
447
function toFinite(value);
448
449
/**
450
* Converts value to an integer
451
* @param value - The value to convert
452
* @returns Returns the converted integer
453
*/
454
function toInteger(value);
455
456
/**
457
* Converts value to an integer suitable for use as the length of an array-like object
458
* @param value - The value to convert
459
* @returns Returns the converted integer
460
*/
461
function toLength(value);
462
463
/**
464
* Converts value to a number
465
* @param value - The value to convert
466
* @returns Returns the converted number
467
*/
468
function toNumber(value);
469
470
/**
471
* Converts value to a plain object flattening inherited enumerable string keyed properties of value to own properties of the plain object
472
* @param value - The value to convert
473
* @returns Returns the converted plain object
474
*/
475
function toPlainObject(value);
476
477
/**
478
* Converts value to a safe integer
479
* @param value - The value to convert
480
* @returns Returns the converted integer
481
*/
482
function toSafeInteger(value);
483
484
/**
485
* Converts value to a string
486
* @param value - The value to convert
487
* @returns Returns the converted string
488
*/
489
function toString(value);
490
```
491
492
### Object Cloning
493
494
#### Deep & Shallow Cloning
495
Create copies of values.
496
497
```javascript { .api }
498
/**
499
* Creates a shallow clone of value
500
* @param value - The value to clone
501
* @returns Returns the cloned value
502
*/
503
function clone(value);
504
505
/**
506
* Creates a deep clone of value
507
* @param value - The value to clone
508
* @returns Returns the cloned value
509
*/
510
function cloneDeep(value);
511
512
/**
513
* Like clone except that it accepts customizer which is invoked to produce the cloned value
514
* @param value - The value to clone
515
* @param customizer - The function to customize cloning
516
* @returns Returns the cloned value
517
*/
518
function cloneWith(value, customizer);
519
520
/**
521
* Like cloneDeep except that it accepts customizer which is invoked to produce the cloned value
522
* @param value - The value to clone
523
* @param customizer - The function to customize cloning
524
* @returns Returns the cloned value
525
*/
526
function cloneDeepWith(value, customizer);
527
```
528
529
### Array Casting
530
531
#### Array Conversion
532
Convert values to arrays.
533
534
```javascript { .api }
535
/**
536
* Casts value as an array if it's not one
537
* @param value - The value to inspect
538
* @returns Returns the cast array
539
*/
540
function castArray(value);
541
```
542
543
## Usage Examples
544
545
```javascript
546
import {
547
isString, isNumber, isArray, isObject, isFunction,
548
isEmpty, isEqual, isMatch, gt, clone, cloneDeep,
549
isNil, isFinite, isInteger, toNumber, toString
550
} from "lodash";
551
552
// Basic type checking
553
console.log(isString('hello')); // true
554
console.log(isNumber(42)); // true
555
console.log(isArray([1, 2, 3])); // true
556
console.log(isObject({})); // true
557
console.log(isFunction(() => {})); // true
558
559
// Null/undefined checking
560
console.log(isNil(null)); // true
561
console.log(isNil(undefined)); // true
562
console.log(isNil(0)); // false
563
564
// Number validation
565
console.log(isFinite(42)); // true
566
console.log(isFinite(Infinity)); // false
567
console.log(isInteger(42.0)); // true
568
console.log(isInteger(42.5)); // false
569
570
// Emptiness checking
571
console.log(isEmpty([])); // true
572
console.log(isEmpty({})); // true
573
console.log(isEmpty('')); // true
574
console.log(isEmpty([1])); // false
575
576
// Deep equality
577
const obj1 = { a: 1, b: { c: 2 } };
578
const obj2 = { a: 1, b: { c: 2 } };
579
const obj3 = { a: 1, b: { c: 3 } };
580
581
console.log(obj1 === obj2); // false (reference equality)
582
console.log(isEqual(obj1, obj2)); // true (deep equality)
583
console.log(isEqual(obj1, obj3)); // false
584
585
// Pattern matching
586
const user = { name: 'John', age: 30, active: true };
587
console.log(isMatch(user, { name: 'John' })); // true
588
console.log(isMatch(user, { name: 'John', age: 30 })); // true
589
console.log(isMatch(user, { name: 'Jane' })); // false
590
591
// Relational comparisons
592
console.log(gt(3, 1)); // true
593
console.log(gt(1, 3)); // false
594
595
// Type-safe validation function
596
function validateUser(user) {
597
const errors = [];
598
599
if (!isObject(user)) {
600
errors.push('User must be an object');
601
return errors;
602
}
603
604
if (!isString(user.name) || isEmpty(user.name)) {
605
errors.push('Name is required and must be a string');
606
}
607
608
if (!isNumber(user.age) || !isInteger(user.age) || user.age < 0) {
609
errors.push('Age must be a positive integer');
610
}
611
612
if (!isNil(user.email) && !isString(user.email)) {
613
errors.push('Email must be a string if provided');
614
}
615
616
return errors;
617
}
618
619
// Usage
620
console.log(validateUser({ name: 'John', age: 30 })); // []
621
console.log(validateUser({ name: '', age: 'thirty' })); // ['Name is required...', 'Age must be...']
622
623
// Safe type conversion
624
const values = ['42', '3.14', 'hello', null, undefined];
625
const numbers = values.map(v => {
626
const num = toNumber(v);
627
return isFinite(num) ? num : 0;
628
});
629
console.log(numbers); // [42, 3.14, 0, 0, 0]
630
631
// Deep cloning for safe mutations
632
const original = {
633
name: 'John',
634
hobbies: ['reading', 'coding'],
635
profile: { age: 30 }
636
};
637
638
const copy = cloneDeep(original);
639
copy.hobbies.push('gaming');
640
copy.profile.age = 31;
641
642
console.log(original.hobbies); // ['reading', 'coding'] (unchanged)
643
console.log(copy.hobbies); // ['reading', 'coding', 'gaming']
644
645
// Custom type guards
646
const isNonEmptyString = (value) => isString(value) && !isEmpty(value);
647
const isPositiveNumber = (value) => isNumber(value) && isFinite(value) && value > 0;
648
const isValidEmail = (value) => isString(value) && /\S+@\S+\.\S+/.test(value);
649
650
// API response validation
651
function validateApiResponse(response) {
652
if (!isObject(response)) return false;
653
if (!isArray(response.data)) return false;
654
if (!isNumber(response.status)) return false;
655
656
return response.data.every(item =>
657
isObject(item) &&
658
isNonEmptyString(item.id) &&
659
isNonEmptyString(item.name)
660
);
661
}
662
663
// Runtime type checking utility
664
function createTypedFunction(paramTypes, returnType, fn) {
665
return function(...args) {
666
// Check parameter types
667
paramTypes.forEach((type, index) => {
668
if (!type(args[index])) {
669
throw new TypeError(`Parameter ${index} failed type check`);
670
}
671
});
672
673
const result = fn(...args);
674
675
// Check return type
676
if (!returnType(result)) {
677
throw new TypeError('Return value failed type check');
678
}
679
680
return result;
681
};
682
}
683
684
// Usage
685
const safeAdd = createTypedFunction(
686
[isNumber, isNumber], // parameters must be numbers
687
isNumber, // return value must be number
688
(a, b) => a + b
689
);
690
691
console.log(safeAdd(5, 3)); // 8
692
// safeAdd('5', 3); // TypeError: Parameter 0 failed type check
693
```