import { type ArrayOrValue } from '../array'; import { type Maybe } from './maybe.type'; /** * String key that uniquely identifies a modifier within a {@link ModifierMap}. * * @semanticType * @semanticTopic string * @semanticTopic identifier */ export type ModifierKey = string; /** * Function that mutates the input value in place. */ export type ModifierFunction = (input: T) => void; /** * Holds a reference to a {@link ModifierFunction}. */ export interface ModifierFunctionRef { readonly modify: ModifierFunction; } /** * A keyed modifier that pairs a unique {@link ModifierKey} with a {@link ModifierFunction}. * * The key allows modifiers to be added, replaced, or removed from a {@link ModifierMap} by identity. */ export interface Modifier extends ModifierFunctionRef { /** * Unique key identifying this modifier. */ readonly key: ModifierKey; } /** * Creates a {@link Modifier} with the given key and modify function. * * @param key - Unique identifier for the modifier. * @param modify - Function that mutates the target value. * @returns A new {@link Modifier} pairing the key with the modify function. * * @dbxUtil * @dbxUtilCategory value * @dbxUtilTags modifier, mutate, transform, key, factory * @dbxUtilRelated apply-modifiers, modifier-function * * @example * ```ts * const uppercaseName = modifier<{ name: string }>('uppercase', (x) => { x.name = x.name.toUpperCase(); }); * const obj = { name: 'alice' }; * uppercaseName.modify(obj); * // obj.name === 'ALICE' * ``` */ export declare function modifier(key: string, modify: ModifierFunction): Modifier; /** * A no-operation modifier that does nothing to the input. Useful as a default/fallback. * * @returns Undefined (no mutation is performed) */ export declare const NOOP_MODIFIER: ModifierFunction; /** * Map of {@link Modifier} instances keyed by their {@link ModifierKey}, enabling lookup, replacement, and removal by key. */ export type ModifierMap = Map>; /** * Adds one or more modifiers to the map, creating a new map if none is provided. * * If a modifier with the same key already exists, it is replaced. * * @param modifiers - Modifier(s) to add. * @param map - Existing map to add to, or undefined to create a new one. * @returns The modifier map with the new modifiers added. * * @example * ```ts * const mod = modifier<{ x: number }>('double', (o) => { o.x *= 2; }); * const map = addModifiers(mod); * map.has('double'); // true * ``` */ export declare function addModifiers(modifiers: ArrayOrValue>, map?: Maybe>): ModifierMap; /** * Removes one or more modifiers from the map by key. Returns an empty map if no map is provided. * * @param modifiers - Modifier(s) whose keys should be removed. * @param map - The map to remove from. * @returns The modifier map with the specified modifiers removed. * * @example * ```ts * const mod = modifier<{ x: number }>('double', (o) => { o.x *= 2; }); * const map = addModifiers(mod); * const result = removeModifiers(mod, map); * result.has('double'); // false * ``` */ export declare function removeModifiers(modifiers: ArrayOrValue>, map: Maybe>): ModifierMap; /** * Converts a {@link ModifierMap} to a single {@link ModifierFunction} that applies all modifiers in sequence. * * Returns {@link NOOP_MODIFIER} if the map is nullish or empty. * * @param map - The modifier map to convert. * @returns A single modifier function that applies all mapped modifiers, or {@link NOOP_MODIFIER} if empty. * * @dbxUtil * @dbxUtilCategory value * @dbxUtilTags modifier, map, factory, compose * @dbxUtilRelated maybe-modifier-map-to-function, modifier * * @example * ```ts * const mod = modifier<{ x: number }>('inc', (o) => { o.x += 1; }); * const map = addModifiers(mod); * const fn = modifierMapToFunction(map); * const obj = { x: 0 }; * fn(obj); * // obj.x === 1 * ``` * * @__NO_SIDE_EFFECTS__ */ export declare function modifierMapToFunction(map: Maybe>): ModifierFunction; /** * Converts a {@link ModifierMap} to a single {@link ModifierFunction} if the map is non-null. * * Returns undefined if no map is provided, allowing callers to distinguish "no modifiers" from "empty modifiers". * * @param map - The modifier map to convert. * @returns A composed modifier function, or `undefined` if no map is provided. * * @dbxUtil * @dbxUtilCategory value * @dbxUtilTags modifier, map, factory, compose, optional * @dbxUtilRelated modifier-map-to-function, modifier * * @__NO_SIDE_EFFECTS__ */ export declare function maybeModifierMapToFunction(map: Maybe>): Maybe>; /** * Merges an array of {@link ModifierFunction} values into a single function that applies them all in order. * * Returns {@link NOOP_MODIFIER} if the array is empty or nullish. * * @param modifiers - Array of modifier functions to merge. * @returns A single modifier function that applies all provided modifiers, or {@link NOOP_MODIFIER} if empty. * * @example * ```ts * const add1 = (o: { x: number }) => { o.x += 1; }; * const double = (o: { x: number }) => { o.x *= 2; }; * const merged = mergeModifiers([add1, double]); * const obj = { x: 3 }; * merged(obj); * // obj.x === 8 (3 + 1 = 4, then 4 * 2 = 8) * ``` */ export declare function mergeModifiers(modifiers: ModifierFunction[]): ModifierFunction; /** * Merges an array of {@link ModifierFunction} values into a single function. Returns undefined if the input is nullish. * * If only one modifier is provided, returns it directly without wrapping. * * @param modifiers - Array of modifier functions to merge, or undefined. * @returns A composed modifier function, the single modifier if only one provided, or `undefined` if input is nullish. */ export declare function maybeMergeModifiers(modifiers: Maybe[]>): Maybe>;