or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

editor-core.mdindex.mdplugin-system.mdreact-components.mdreact-hooks.mdslate-operations.mdutility-functions.md
tile.json

utility-functions.mddocs/

Utility Functions

General utilities for object manipulation, type operations, function composition, and common development patterns used throughout the Plate ecosystem.

Capabilities

Object Utilities

Utilities for manipulating and working with JavaScript objects.

/**
 * Pick specific properties from an object
 * @param object - Source object
 * @param keys - Array of keys to pick
 * @returns New object with only the specified keys
 */
function pick<T, K extends keyof T>(
  object: T, 
  keys: K[]
): Pick<T, K>;

/**
 * Omit specific properties from an object
 * @param object - Source object
 * @param keys - Array of keys to omit
 * @returns New object without the specified keys
 */
function omit<T, K extends keyof T>(
  object: T, 
  keys: K[]
): Omit<T, K>;

/**
 * Deep merge multiple objects
 * @param target - Target object
 * @param sources - Source objects to merge
 * @returns Merged object
 */
function merge<T>(target: T, ...sources: Partial<T>[]): T;

/**
 * Clone an object deeply
 * @param obj - Object to clone
 * @returns Deep clone of the object
 */
function cloneDeep<T>(obj: T): T;

/**
 * Check if two objects are deeply equal
 * @param a - First object
 * @param b - Second object
 * @returns True if objects are deeply equal
 */
function isEqual<T>(a: T, b: T): boolean;

Usage Examples:

import { pick, omit, merge } from "@udecode/plate-common";

// Pick specific properties
const user = { id: 1, name: 'John', age: 30, email: 'john@example.com' };
const publicUser = pick(user, ['id', 'name']);
// Result: { id: 1, name: 'John' }

// Omit sensitive properties
const safeUser = omit(user, ['email']);
// Result: { id: 1, name: 'John', age: 30 }

// Merge objects
const defaults = { theme: 'light', size: 'medium' };
const userPrefs = { theme: 'dark' };
const settings = merge(defaults, userPrefs);
// Result: { theme: 'dark', size: 'medium' }

Function Utilities

Utilities for function composition, currying, and functional programming patterns.

/**
 * Compose functions from right to left
 * @param functions - Functions to compose
 * @returns Composed function
 */
function pipe<T>(...functions: Array<(arg: T) => T>): (arg: T) => T;

/**
 * Curry a function to allow partial application
 * @param fn - Function to curry
 * @returns Curried function
 */
function curry<T extends (...args: any[]) => any>(
  fn: T
): CurriedFunction<T>;

/**
 * Debounce a function to limit execution frequency
 * @param fn - Function to debounce
 * @param delay - Delay in milliseconds
 * @returns Debounced function
 */
function debounce<T extends (...args: any[]) => any>(
  fn: T,
  delay: number
): DebouncedFunction<T>;

/**
 * Throttle a function to limit execution rate
 * @param fn - Function to throttle
 * @param limit - Time limit in milliseconds
 * @returns Throttled function
 */
function throttle<T extends (...args: any[]) => any>(
  fn: T,
  limit: number
): ThrottledFunction<T>;

/**
 * Memoize a function to cache results
 * @param fn - Function to memoize
 * @param keyGenerator - Custom key generator function
 * @returns Memoized function
 */
function memoize<T extends (...args: any[]) => any>(
  fn: T,
  keyGenerator?: (...args: Parameters<T>) => string
): MemoizedFunction<T>;

Array Utilities

Utilities for working with arrays and collections.

/**
 * Remove duplicates from array
 * @param array - Input array
 * @param keyFn - Optional key function for complex objects
 * @returns Array with duplicates removed
 */
function unique<T>(
  array: T[],
  keyFn?: (item: T) => any
): T[];

/**
 * Flatten nested arrays
 * @param array - Nested array to flatten
 * @param depth - Maximum depth to flatten
 * @returns Flattened array
 */
function flatten<T>(array: T[][], depth?: number): T[];

/**
 * Group array items by key
 * @param array - Array to group
 * @param keyFn - Function to generate grouping key
 * @returns Object with grouped items
 */
function groupBy<T, K extends string | number | symbol>(
  array: T[],
  keyFn: (item: T) => K
): Record<K, T[]>;

/**
 * Sort array by multiple criteria
 * @param array - Array to sort
 * @param sortFns - Array of sort functions
 * @returns Sorted array
 */
function sortBy<T>(
  array: T[],
  ...sortFns: Array<(item: T) => any>
): T[];

String Utilities

Utilities for string manipulation and formatting.

/**
 * Convert string to camelCase
 * @param str - Input string
 * @returns camelCase string
 */
function camelCase(str: string): string;

/**
 * Convert string to kebab-case
 * @param str - Input string
 * @returns kebab-case string
 */
function kebabCase(str: string): string;

/**
 * Convert string to PascalCase
 * @param str - Input string
 * @returns PascalCase string
 */
function pascalCase(str: string): string;

/**
 * Convert string to snake_case
 * @param str - Input string
 * @returns snake_case string
 */
function snakeCase(str: string): string;

/**
 * Capitalize first letter of string
 * @param str - Input string
 * @returns Capitalized string
 */
function capitalize(str: string): string;

/**
 * Truncate string to specified length
 * @param str - Input string
 * @param length - Maximum length
 * @param suffix - Suffix to append when truncated
 * @returns Truncated string
 */
function truncate(str: string, length: number, suffix?: string): string;

Type Utilities

Advanced TypeScript utilities for type manipulation and inference.

/**
 * Convert union type to intersection type
 */
type UnionToIntersection<U> = 
  (U extends any ? (k: U) => void : never) extends 
  ((k: infer I) => void) ? I : never;

/**
 * Simplify complex types for better IntelliSense
 */
type Simplify<T> = {
  [K in keyof T]: T[K];
} & {};

/**
 * Override properties in type T with properties from type U
 */
type Override<T, U> = Omit<T, keyof U> & U;

/**
 * Make specific properties optional
 */
type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;

/**
 * Make specific properties required
 */
type RequiredBy<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;

/**
 * Get function parameter types
 */
type Parameters<T extends (...args: any) => any> = 
  T extends (...args: infer P) => any ? P : never;

/**
 * Get function return type
 */
type ReturnType<T extends (...args: any) => any> = 
  T extends (...args: any) => infer R ? R : any;

Validation Utilities

Utilities for runtime type checking and validation.

/**
 * Check if value is defined (not null or undefined)
 * @param value - Value to check
 * @returns True if value is defined
 */
function isDefined<T>(value: T | null | undefined): value is T;

/**
 * Check if value is a non-empty string
 * @param value - Value to check
 * @returns True if value is a non-empty string
 */
function isNonEmptyString(value: any): value is string;

/**
 * Check if value is a plain object
 * @param value - Value to check
 * @returns True if value is a plain object
 */
function isPlainObject(value: any): value is Record<string, any>;

/**
 * Check if value is an array
 * @param value - Value to check
 * @returns True if value is an array
 */
function isArray<T>(value: any): value is T[];

/**
 * Check if value is a function
 * @param value - Value to check
 * @returns True if value is a function
 */
function isFunction(value: any): value is Function;

/**
 * Assert that a condition is true, throw error otherwise
 * @param condition - Condition to assert
 * @param message - Error message
 */
function assert(condition: any, message?: string): asserts condition;

Math Utilities

Mathematical utility functions.

/**
 * Clamp a number between min and max values
 * @param value - Value to clamp
 * @param min - Minimum value
 * @param max - Maximum value
 * @returns Clamped value
 */
function clamp(value: number, min: number, max: number): number;

/**
 * Linear interpolation between two values
 * @param start - Start value
 * @param end - End value
 * @param t - Interpolation factor (0-1)
 * @returns Interpolated value
 */
function lerp(start: number, end: number, t: number): number;

/**
 * Map a value from one range to another
 * @param value - Input value
 * @param inMin - Input range minimum
 * @param inMax - Input range maximum
 * @param outMin - Output range minimum
 * @param outMax - Output range maximum
 * @returns Mapped value
 */
function mapRange(
  value: number,
  inMin: number,
  inMax: number,
  outMin: number,
  outMax: number
): number;

/**
 * Round a number to specified decimal places
 * @param value - Value to round
 * @param decimals - Number of decimal places
 * @returns Rounded value
 */
function roundTo(value: number, decimals: number): number;

Utility Types and Interfaces

/**
 * Curried function type
 */
type CurriedFunction<T extends (...args: any[]) => any> = 
  T extends (arg: infer A, ...rest: infer R) => infer Return
    ? R extends []
      ? (arg: A) => Return
      : (arg: A) => CurriedFunction<(...args: R) => Return>
    : never;

/**
 * Debounced function type
 */
interface DebouncedFunction<T extends (...args: any[]) => any> {
  (...args: Parameters<T>): void;
  cancel(): void;
  flush(): ReturnType<T> | undefined;
  pending(): boolean;
}

/**
 * Throttled function type
 */
interface ThrottledFunction<T extends (...args: any[]) => any> {
  (...args: Parameters<T>): ReturnType<T> | undefined;
  cancel(): void;
  flush(): ReturnType<T> | undefined;
}

/**
 * Memoized function type
 */
interface MemoizedFunction<T extends (...args: any[]) => any> {
  (...args: Parameters<T>): ReturnType<T>;
  clear(): void;
  has(...args: Parameters<T>): boolean;
  delete(...args: Parameters<T>): boolean;
}

/**
 * Deep partial type
 */
type DeepPartial<T> = {
  [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
};

/**
 * Deep required type
 */
type DeepRequired<T> = {
  [P in keyof T]-?: T[P] extends object ? DeepRequired<T[P]> : T[P];
};