import { type PrimativeKey, type ReadKeyFunction } from '../key'; import { type MapFunction } from '../value/map'; import { type Maybe } from '../value/maybe.type'; /** * Maps the values of the input array to a Map, keyed by the result of a key function. * Optionally transforms each value using a value function. * * @param values - source array of items to map * @param keyFn - function to extract a key from each item * @param valueFn - optional function to transform each item into the desired map value * @returns a Map of keys to values derived from the input array */ export declare function arrayToMap(values: T[], keyFn: ReadKeyFunction, valueFn: (t: T) => V): Map, V>; export declare function arrayToMap(values: T[], keyFn: ReadKeyFunction, valueFn: (t: T) => T): Map, T>; export declare function arrayToMap(values: T[], keyFn: ReadKeyFunction): Map, T>; /** * Builds the {@link Error} thrown by {@link arrayToObject} when two values share a key, given the key and the later value. */ export type ArrayToObjectDuplicateKeyErrorFactory = (key: K, value: T) => Error; /** * Config-object form of {@link arrayToObject}. */ export interface ArrayToObjectConfig { /** * Reads the key of each value. Values with a null/undefined key are left out of the record. */ readonly readKey: ReadKeyFunction; /** * Maps each value to the value stored in the record. * * Defaults to the value itself. */ readonly readValue?: Maybe>; /** * Whether two values sharing a key throws an error. * * - unset/false: the last value wins, the same as the positional form. * - true: throws `arrayToObject(): duplicate key "".` * - a function: throws the {@link Error} it returns, so a registry can keep its own message. * * Duplicates are detected with an own-property check, so keys such as `toString` or `constructor` are not false duplicates. */ readonly throwOnDuplicateKey?: Maybe>; } /** * Maps the values of the input array to a Record object, keyed by the result of a key function. * Items with undefined keys are omitted. Optionally transforms each value using a value function. * * Also accepts an {@link ArrayToObjectConfig} as the second argument, which can throw when two values share a key instead of letting the last value win. * * @param values - source array of items to map * @param keyFn - function to extract a key from each item, or an {@link ArrayToObjectConfig} * @param valueFn - optional function to transform each item into the desired record value * @returns a Record of keys to values derived from the input array * * @example * ```ts * const byId = arrayToObject([{ id: 'a' }, { id: 'b' }], (x) => x.id); * // { a: { id: 'a' }, b: { id: 'b' } } * * const registry = arrayToObject(configs, { * readKey: (x) => x.type, * throwOnDuplicateKey: (type) => new Error(`duplicate type in registry: ${type}`) * }); * ``` * * @dbxUtil * @dbxUtilCategory array * @dbxUtilTags array, object, record, index, key, lookup, duplicate, unique, registry * @dbxUtilRelated is-unique-keyed-function, key-value-map-factory */ export declare function arrayToObject(values: ReadonlyArray, keyFn: ReadKeyFunction, valueFn: (t: T) => V): Record; export declare function arrayToObject(values: ReadonlyArray, keyFn: ReadKeyFunction, valueFn: (t: T) => T): Record; export declare function arrayToObject(values: ReadonlyArray, keyFn: ReadKeyFunction): Record; export declare function arrayToObject(values: ReadonlyArray, config: ArrayToObjectConfig): Record; /** * Returns values for each key, reusing existing items when available and generating new ones for missing keys. * * @param keys - The keys to resolve values for. * @param existing - Array of pre-existing items to check against. * @param readKey - Function to extract a key from an existing item. * @param generateFn - Function to create a new item for a key not found in existing items. * @returns Items aligned to `keys` — reused when present in `existing`, generated otherwise. */ export declare function generateIfDoesNotExist(keys: K[], existing: T[], readKey: ReadKeyFunction, generateFn: (key: K) => T): T[];