import type { Key, StringRecord, Entry, Entries, FromEntries, Paths, PickPaths, Flat } from '@toolbox-ts/types/defs/object'; /** * Callback function for iterating over object key-value pairs. * * @important Keys are enumerable strings * - Numeric keys like `0` become string literals `'0'` * - String keys remain as-is: `'name'` * - Symbol keys are excluded (use `symbolEntries` for those) * * @example * ```ts * const obj = { 0: 'a', 1: 'b', name: 'c' }; * * forEach(obj, (value, key) => { * // ✅ Works - key is properly typed for indexing * console.log(obj[key]); * * // ✅ Works - string comparison for numeric keys * if (key === '0') // true for numeric key 0 * * // ✅ Works - string comparison for string keys * if (key === 'name') * }); * ``` */ export type KeyValueCb = (value: T[keyof T], key: Key.Enumerable) => R; /** * Executes a callback for each key-value pair in an object * * @template T - Object type * @example * ```ts * forEach({ a: 1, b: 2 }, (value, key) => { * console.log(`${key}: ${value}`) * }) * ``` */ export declare const forEach: (obj: T, cb: KeyValueCb) => void; export type ReduceCb = (acc: R, value: T[keyof T], key: Key.Enumerable) => R; /** * Reduces an object to a single value by applying a reducer function to each key-value pair. * * @template T - Plain object type `({})` * @template R - Accumulator type * @example * ```ts * const sum = reduce( * { a: 1, b: 2, c: 3 }, * (acc, value) => acc + value, * 0 * ) // 6 * ``` */ export declare const reduce: (obj: T, cb: ReduceCb, initial?: R) => R; /** * Creates a new object by applying a callback to each key-value pair of the original object. * * @template T - Original object type * @template R - Resulting value type * @example * ```ts * const mapped = map( * { a: 1, b: 2, c: 3 }, * (key, value) => value * 2 * ) // { a: 2, b: 4, c: 6 } * ``` */ export declare const map: (obj: T, cb: KeyValueCb) => { [K in Key.Enumerable]: R; }; export interface FilterFunction { (obj: T, predicate: FilterInPredicate): { [K in keyof T as T[K] extends V ? Key.CoerceNumber : never]: T[K]; }; (obj: T, predicate: FilterOutPredicate): { [K in keyof T as T[K] extends E ? never : Key.CoerceNumber]: T[K]; }; (obj: T, predicate: (value: unknown) => boolean): { [K in keyof T as Key.CoerceNumber]?: T[K]; }; } export type FilterInPredicate = (value: unknown) => value is V; export type FilterOutPredicate = (value: unknown) => value is Exclude; export type FilterPredicate = FilterInPredicate | FilterOutPredicate | FilterSimplePredicate; export type FilterSimplePredicate = (value: unknown) => boolean; export declare const filter: FilterFunction; /** Creates a new object by omitting specified keys from the original object. */ export declare const omit: ( /** Object to omit properties from */ obj: T, /** Keys of properties to omit */ omitKeys: K[] | readonly K[]) => { [P in keyof T as P extends K ? never : Key.CoerceNumber

]: T[P]; }; /** * Creates a new object by picking specified paths from the original object. * * @important Keys with dots that are not paths will not properly reconstruct as nested objects. * * @example * ```ts * const obj = { a: { b: { c: 3 } }, d: 4 }; * const result = pick(obj, ['a.b.c', 'd']); * // result is { a: { b: { c: 3 } }, d: 4 } * * const obj2 = { 'x.y': { z: 5 } }; * const result2 = pick(obj2, ['x.y']); * // result2 is { x: { y: { z: never } } } */ export declare const pick: >(obj: T, pickPaths: P[] | readonly P[]) => PickPaths; /** * Flattens a nested object into a single-level object with dot-separated keys. * * @important Objects with keys that contain dots will not be flattened correctly. * * @example * ```ts * // ✅ * const obj = { a: { b: { c: 3 } }, d: 4 }; * const result = flat(obj); * // result is { 'a.b.c': 3, d: 4 } * * // ❌ * const obj2 = { 'x.y': { z: 5 } }; * const result2 = flat(obj2); * // result2 is {} * ``` */ export declare const flat: (obj: T) => Flat; /** * Tests whether at least one property satisfies the predicate * * @example * ```ts * some({ a: 1, b: 2, c: 3 }, v => v > 2) // true * ``` */ export declare const some: (obj: T, predicate: KeyValueCb) => boolean; /** * Tests whether all properties satisfy the predicate * * @example * ```ts * every({ a: 1, b: 2, c: 3 }, v => v > 0) // true * ``` */ export declare const every: (obj: T, predicate: KeyValueCb) => boolean; /** * Returns the first key-value pair that satisfies the predicate * * @example * ```ts * find({ a: 1, b: 2, c: 3 }, v => v > 1) // ['b', 2] * ``` */ export declare const find: (obj: T, predicate: KeyValueCb) => [key: Key.Enumerable, value: T[keyof T]] | undefined; export type ObjectSortComparator = (a: Entry, b: Entry) => number; /** * Creates a new object with its keys sorted based on the provided comparator function. * * @template T - Object type * @template K - Key type * @param obj - The object to be sorted. * @param comparator - A function that defines the sort order of the keys. * By default, it sorts keys in ascending lexicographical order. * @returns A new object with the same key-values but in sorted order. * * @example * ```ts * const unsorted = { b: 2, a: 1, c: 3 }; * const sorted = sort(unsorted); * // sorted is { a: 1, b: 2, c: 3 } * const customSorted = sort(unsorted, (a, b) => b[0].localeCompare(a[0])); * // customSorted is { c: 3, b: 2, a: 1 } * ``` */ export declare const sort: (obj: T, comparator?: ObjectSortComparator) => FromEntries>; /** * Sorts the entries of an object based on the provided comparator function. * * @template T - Object type * @param obj - The object whose entries are to be sorted. * @param comparator - A function that defines the sort order of the entries. * By default, it sorts entries in ascending lexicographical order based on their keys. * @returns An array of sorted entries (key-value pairs) from the object. * * @example * ```ts * const unsorted = { b: 2, a: 1, c: 3 }; * const sortedEntries = sortEntries(unsorted); * // sortedEntries is [['a', 1], ['b', 2], ['c', 3]] * const customSortedEntries = sortEntries(unsorted, (a, b) => b[0].localeCompare(a[0])); * // customSortedEntries is [['c', 3], ['b', 2], ['a', 1]] * ``` */ export declare const sortEntries: (obj: T, comparator?: ObjectSortComparator) => Entries;