Standard Toolkit
Packages@accelint/predicates

Overview

Type-safe predicate functions for runtime validation and array filtering

Installation

pnpm add @accelint/predicates

Categories

Type Guards

Core type checking predicates:

Boolean-like Values

Parse and validate truthy/falsy string representations:

  • is-noyes - Yes/no, on/off, true/false predicates
    • isTrue, isFalse, isYes, isNo, isOn, isOff
    • isAnyTruthy, isAnyFalsy

String Patterns

Test strings for prefixes, suffixes, and patterns:

  • does-start-with - String prefix/suffix predicates
    • doesStartWith, doesEndWith
    • doesNotStartWith, doesNotEndWith
  • is-like - Pattern matching with RegExp
    • isLike, isNotLike

Comparison

Numeric and lexicographic comparisons:

  • is-equal - Comparison predicates
    • isEqual, isNotEqual
    • isGreater, isGreaterEqual
    • isLesser, isLesserEqual
  • is-between - Range validation
    • isBetween, isNotBetween

Array Membership

Test if values exist in arrays:

  • is-in - Array membership predicates
    • isIn, isNotIn

Colors

Validate color format representations:

  • is-hex-color - Color format predicates
    • isHexColor - Hex color strings
    • isCssRgbaString - CSS rgb/rgba strings
    • isCssRgbaObject - React Aria RGBA objects
    • isRgba255Tuple - deck.gl RGBA tuples
    • isValid255Channel - Channel validation

Geographic Coordinates

Validate latitude, longitude, and bounding boxes:

  • is-latitude - Geographic predicates
    • isLatitude - Latitude validation (-90 to 90)
    • isLongitude - Longitude validation (-180 to 180)
    • isBbox - Bounding box validation

Web APIs

Browser API instance checks:

  • is-worker - Web Worker predicates
    • isWorker - Worker instance check
    • isSharedWorker - SharedWorker instance check

Usage Patterns

Basic Usage

import { isString, isNumber } from '@accelint/predicates';

isString('hello'); // true
isNumber(123);     // true

Array Filtering

import { isString, isNothing } from '@accelint/predicates';

const mixed = ['hello', null, 42, 'world', undefined];
const strings = mixed.filter(isString);
// ['hello', 'world']

const defined = mixed.filter(v => !isNothing(v));
// ['hello', 42, 'world']

Curried Predicates

import { isGreater, isIn } from '@accelint/predicates';

const isAdult = isGreater Equal(18);
isAdult(21); // true
isAdult(16); // false

const isValidStatus = isIn(['pending', 'approved', 'rejected']);
isValidStatus('pending'); // true
isValidStatus('unknown'); // false

Type Narrowing

import { isNumber } from '@accelint/predicates';

function processValue(val: unknown) {
  if (isNumber(val)) {
    // TypeScript knows val is number here
    return val.toFixed(2);
  }
  throw new Error('Expected number');
}

Functional Composition

import { isGreaterEqual, isLesserEqual } from '@accelint/predicates';
import { andFn } from '@accelint/core';

const isValidScore = andFn(
  isGreaterEqual(0),
  isLesserEqual(100)
);

isValidScore(50);  // true
isValidScore(150); // false

Design Principles

Pure Functions

All predicates are pure functions with no side effects:

const isEven = (n: number) => n % 2 === 0;
isEven(4); // true (always returns same result for same input)

Curried by Default

Most predicates are curried for partial application:

import { isGreater } from '@accelint/predicates';

// Create specialized predicates
const isAdult = isGreaterEqual(18);
const isRetired = isGreaterEqual(65);

ages.filter(isAdult);
ages.filter(isRetired);

Type Safety

Predicates provide runtime validation and TypeScript type guards:

import { isNumber } from '@accelint/predicates';

function double(val: unknown): number {
  if (!isNumber(val)) {
    throw new Error('Expected number');
  }
  return val * 2; // TypeScript knows val is number
}

On this page