๐งช Typed Utilities โ
Typed utilities provide a comprehensive set of type guards and comparison helpers. These tools ensure your code remains type-safe by providing robust checks for all common JavaScript data types and values.
๐ Quick Reference โ
Type Guards โ
| Method | Description |
|---|---|
isString | Check if a value is a string. |
isNumber | Check if a value is a number. |
isArray | Check if a value is an array. |
isObject | Check if a value is a plain object. |
isFunction | Check if a value is a function. |
isDefined | Check if a value is neither null nor undefined. |
isNil | Check if a value is null or undefined. |
isEmpty | Check if a value is an empty string, array, or object. |
Comparison & Pattern Matching โ
| Method | Description |
|---|---|
isEqual | Perform a deep equality comparison between two values. |
isMatch | Check if an object matches a partial pattern or regex. |
is | Multi-purpose type and value checker. |
isWithin | Check if a number is within a given range. |
๐ก Practical Examples โ
Robust Type Checking โ
ts
import { isString, isArray, isNil, isDefined } from '@vielzeug/toolkit';
function process(data: unknown) {
if (isString(data)) {
// data is inferred as string
return data.toUpperCase();
}
if (isArray(data)) {
// data is inferred as any[]
return data.length;
}
if (isNil(data)) {
return 'N/A';
}
}Deep Equality & Pattern Matching โ
ts
import { isEqual, isMatch } from '@vielzeug/toolkit';
const user = { id: 1, name: 'Alice', settings: { theme: 'dark' } };
// Deep equality
isEqual(user, { id: 1, name: 'Alice', settings: { theme: 'dark' } }); // true
// Pattern match
isMatch(user, { settings: { theme: 'dark' } }); // true
isMatch(user, { name: /^A/ }); // true (regex support)