/** * @fileoverview Utility functions and classes for the modal system. * This module provides helper functions, error handling, and the PromisedValue class. * * Key exports: * - `PromisedValue` - Promise-like class that can be manually resolved/rejected * - `uuid()` - Generates unique identifiers for modals * - `after()` - Creates delays for animations and timing * - `fail()` - Error throwing utility * - Type utilities for TypeScript * * @module utils */ /** * Generic constructor type for classes. * @template T - The type that the constructor creates (defaults to object) */ export type Constructor = new (...args: any[]) => T; /** * Type alias for function arguments array. * Used in dynamic function calls and constructor parameters. */ export type ClosureArgs = any[]; /** * Gets the global object in a cross-platform way. * Works in both Node.js (globalThis) and browser (window) environments. * * @template T - The expected type of the global object * @returns {T} The global object cast to the specified type */ export declare function runtimeGlobal(): T; /** * Options interface for JSError constructor. * @interface JSErrorOptions * @property {unknown} [cause] - The underlying cause of the error */ export interface JSErrorOptions { cause?: unknown; } /** * Reference to the global Error class from the runtime environment. * @type {typeof Error} */ export declare const RuntimeErrorClass: ErrorConstructor; /** * Custom error class that properly extends the native Error class. * Fixes common inheritance issues with Error in TypeScript/JavaScript. * * @class JSError * @extends {RuntimeErrorClass} * * @example * ```typescript * throw new JSError('Something went wrong', { cause: originalError }); * ``` */ export default class JSError extends RuntimeErrorClass { name: string; message: string; stack?: string; cause?: unknown; /** * Creates a new JSError instance. * @param {string} [message] - The error message * @param {JSErrorOptions} [options] - Additional error options */ constructor(message?: string, options?: JSErrorOptions); } /** * Throws an error with the given message. * @template T - The return type (never actually returns) * @param {string} [message] - The error message * @returns {T} Never returns, always throws * @throws {Error} Always throws an Error with the given message */ export declare function fail(message?: string): T; /** * Throws an error using a custom error class. * @template T - The return type (never actually returns) * @template E - The error class type * @param {Constructor} [errorClass] - The error class to instantiate * @param {...ConstructorParameters>} params - Parameters for the error constructor * @returns {T} Never returns, always throws * @throws {E} Always throws an instance of the specified error class */ export declare function fail(errorClass?: Constructor, ...params: ConstructorParameters>): T; /** * Throws a JSError with custom parameters. * @template T - The return type (never actually returns) * @template E - The JSError class type * @param {Constructor} [errorClass] - The JSError class to instantiate * @param {...ConstructorParameters>} params - Parameters for the error constructor * @returns {T} Never returns, always throws * @throws {E} Always throws an instance of the specified JSError class */ export declare function fail(errorClass?: Constructor, ...params: ConstructorParameters>): T; /** * Options for the after function timeout configuration. * @interface AfterOptions * @property {number} [ms] - Milliseconds to wait * @property {number} [milliseconds] - Alias for ms * @property {number} [s] - Seconds to wait * @property {number} [seconds] - Alias for s */ export interface AfterOptions { ms?: number; milliseconds?: number; s?: number; seconds?: number; } /** * Wait for a specified number of milliseconds. * @param {number} ms - Number of milliseconds to wait * @returns {Promise} Promise that resolves after the specified time */ export declare function after(ms: number): Promise; /** * Wait for a specified time using configuration options. * @param {AfterOptions} [options] - Time configuration options * @returns {Promise} Promise that resolves after the specified time */ export declare function after(options?: AfterOptions): Promise; /** * Type alias for a generic object with string keys and unknown values. * @typedef {Record} Obj */ export type Obj = Record; /** * Checks if a value is an object (not null, not array, not function). * @param {unknown} value - The value to check * @returns {value is Obj} True if the value is an object * * @example * ```typescript * isObject({}); // true * isObject(null); // false * isObject([]); // true (arrays are objects) * isObject('string'); // false * ``` */ export declare function isObject(value: unknown): value is Obj; /** * Checks if a value is a plain object (created with {} or new Object()). * Excludes arrays, functions, dates, and other special objects. * * @param {unknown} value - The value to check * @returns {value is object} True if the value is a plain object * * @example * ```typescript * isPlainObject({}); // true * isPlainObject(new Object()); // true * isPlainObject([]); // false * isPlainObject(new Date()); // false * isPlainObject(null); // false * ``` */ export declare function isPlainObject(value: unknown): value is object; /** * Type-level utility to check if a type is a plain object. * Returns true for plain objects, false for functions, arrays, dates, etc. * * @template T - The type to check * @typedef {boolean} IsPlainObject */ export type IsPlainObject = T extends object ? T extends (...args: any[]) => any ? false : T extends any[] ? false : T extends Date ? false : T extends RegExp ? false : true : false; /** * Generates a UUID v4 string. * Creates a universally unique identifier suitable for use as modal IDs. * * @returns {string} A UUID v4 string in the format xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx * * @example * ```typescript * const id = uuid(); // '550e8400-e29b-41d4-a716-446655440000' * const modal = createModal(MyComponent); * console.log(modal.id); // Uses uuid() internally * ``` */ export declare function uuid(): string; /** * Type utility that creates a "prettier" version of a type by expanding it. * Useful for making complex intersection types more readable in IDE tooltips. * * @template T - The type to make prettier * @typedef {Object} Pretty */ export type Pretty = T extends infer U ? { [K in keyof U]: U[K]; } : never; /** * Type utility to check if a type is `any`. * Returns true if T is the `any` type, false otherwise. * * @template T - The type to check * @typedef {boolean} IsAny */ export type IsAny = { __: "never"; } extends T ? true : false; /** * Listener function type for when a PromisedValue resolves. * @template T - The type of the resolved value * @callback PromisedValueResolveListener * @param {T} result - The resolved value * @returns {unknown} Optional return value */ export type PromisedValueResolveListener = (result: T) => unknown; /** * Listener function type for when a PromisedValue rejects. * @callback PromisedValueRejectListener * @param {Error} [reason] - The rejection reason * @returns {unknown} Optional return value */ export type PromisedValueRejectListener = (reason?: Error) => unknown; /** * Listener function type for when a PromisedValue resets. * @callback PromisedValueResetListener * @returns {unknown} Optional return value */ export type PromisedValueResetListener = () => unknown; /** * A Promise-like class that can be manually resolved or rejected. * Provides additional functionality like listeners, reset capability, and value access. * Used internally by the modal system to handle modal responses. * * @template T - The type of value this promise will resolve to * @implements {Promise} * * @example * ```typescript * const promisedValue = new PromisedValue(); * * // Set up listeners * promisedValue.onResolve(value => console.log('Resolved:', value)); * * // Resolve later * promisedValue.resolve('Hello World'); * * // Use as a regular promise * const result = await promisedValue; * ``` */ export declare class PromisedValue implements Promise { /** * Creates a PromisedValue from an existing Promise. * The PromisedValue will resolve or reject when the source promise does. * * @template T - The type of value the promise resolves to * @param {Promise} promise - The source promise * @returns {PromisedValue} A new PromisedValue that mirrors the source promise * * @example * ```typescript * const fetchPromise = fetch('/api/data'); * const promisedValue = PromisedValue.from(fetchPromise); * ``` */ static from(promise: Promise): PromisedValue; private _value?; private promise; private _resolve; private _reject; private readonly resolveListeners; private readonly rejectListeners; private readonly resetListeners; /** * Creates a new PromisedValue instance. * The promise starts in a pending state and can be resolved or rejected later. */ constructor(); [Symbol.toStringTag]: string; /** * Gets the resolved value if the promise has been resolved. * @returns {T | null} The resolved value or null if not yet resolved */ get value(): T | null; /** * Type guard to check if the promise has been resolved. * @returns {boolean} True if the promise has been resolved */ isResolved(): this is { value: T; }; /** * Attaches callbacks for the resolution and/or rejection of the Promise. * @template TResult1 - The type of the fulfilled value * @template TResult2 - The type of the rejected value * @param {Function} [onFulfilled] - Callback for when promise is fulfilled * @param {Function} [onRejected] - Callback for when promise is rejected * @returns {Promise} A Promise for the completion of the callbacks */ then(onFulfilled?: ((value: T) => TResult1 | PromiseLike) | undefined | null, onRejected?: ((reason: Error) => TResult2 | PromiseLike) | undefined | null): Promise; /** * Attaches a callback for only the rejection of the Promise. * @template TResult - The type of the rejected value * @param {Function} [onRejected] - Callback for when promise is rejected * @returns {Promise} A Promise for the completion of the callback */ catch(onRejected?: ((reason: Error) => TResult | PromiseLike) | undefined | null): Promise; /** * Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). * @param {Function} [onFinally] - Callback to run when promise settles * @returns {Promise} A Promise for the completion of the callback */ finally(onFinally?: (() => void) | null): Promise; /** * Resolves the promise with the given value. * If already resolved, resets and resolves again. * * @param {T} value - The value to resolve with */ resolve(value: T): void; /** * Rejects the promise with the given reason. * If already resolved, resets and rejects. * * @param {Error} [reason] - The reason for rejection */ reject(reason?: Error): void; /** * Adds a listener for when the promise resolves. * * @param {PromisedValueResolveListener} listener - Function to call when resolved * @returns {Function} Function to remove the listener */ onResolve(listener: PromisedValueResolveListener): () => void; /** * Adds a listener for when the promise rejects. * * @param {PromisedValueRejectListener} listener - Function to call when rejected * @returns {Function} Function to remove the listener */ onReject(listener: PromisedValueRejectListener): () => void; /** * Adds a listener for when the promise is reset. * * @param {PromisedValueResetListener} listener - Function to call when reset * @returns {Function} Function to remove the listener */ onReset(listener: PromisedValueResetListener): () => void; /** * Resets the promise to a pending state. * Creates a new internal promise and notifies reset listeners. */ reset(): void; /** * Sets up the internal promise and resolve/reject functions. * @private */ private setPromiseValues; } /** * Removes an item from an array by reference. * Modifies the original array in place. * * @template T - The type of items in the array * @param {T[]} items - The array to remove from * @param {T} item - The item to remove * @returns {boolean} True if the item was found and removed, false otherwise * * @example * ```typescript * const arr = [1, 2, 3]; * const removed = arrayRemove(arr, 2); // true * console.log(arr); // [1, 3] * * const notRemoved = arrayRemove(arr, 5); // false * ``` */ export declare function arrayRemove(items: T[], item: T): boolean;