Skip to content

๐Ÿงช 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 โ€‹

MethodDescription
isStringCheck if a value is a string.
isNumberCheck if a value is a number.
isArrayCheck if a value is an array.
isObjectCheck if a value is a plain object.
isFunctionCheck if a value is a function.
isDefinedCheck if a value is neither null nor undefined.
isNilCheck if a value is null or undefined.
isEmptyCheck if a value is an empty string, array, or object.

Comparison & Pattern Matching โ€‹

MethodDescription
isEqualPerform a deep equality comparison between two values.
isMatchCheck if an object matches a partial pattern or regex.
isMulti-purpose type and value checker.
isWithinCheck 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)

๐Ÿ”— All Typed Utilities โ€‹