@catbee/utils
Version:
A modular, production-grade utility toolkit for Node.js and TypeScript, designed for robust, scalable applications (including Express-based services). All utilities are tree-shakable and can be imported independently.
117 lines • 4.72 kB
TypeScript
/**
* Checks whether the object has no own enumerable properties.
*
* @param {Record<any, any>} obj - The object to check.
* @returns {boolean} True if the object is empty, false otherwise.
*/
export declare function isObjEmpty(obj: Record<any, any>): boolean;
/**
* Returns a new object with only the specified keys picked.
*
* @template T
* @template K
* @param {T} obj - The source object.
* @param {K[]} keys - Keys to pick from the object.
* @returns {Pick<T, K>} New object with picked keys.
*/
export declare function pick<T extends object, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
/**
* Returns a new object with the specified keys omitted.
*
* @template T
* @template K
* @param {T} obj - The source object.
* @param {K[]} keys - Keys to omit from the object.
* @returns {Omit<T, K>} New object without omitted keys.
*/
export declare function omit<T extends object, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>;
/**
* Deeply merges two objects (mutates and returns the target object, not pure).
*
* @template T
* @param {T} target - The object to merge into (will be mutated).
* @param {Partial<T>} source - The object to merge from.
* @returns {T} The merged object (same as target).
*/
export declare function deepObjMerge<T extends Record<string, any>>(target: T, source: Partial<T>): T;
/**
* Flattens a nested object using dot notation for keys (e.g., `{a: {b: 1}}` → `{ "a.b": 1 }`).
*
* @template T
* @param {T} obj - The object to flatten.
* @param {string} [prefix=""] - Optional prefix for nested keys (used internally).
* @returns {Record<string, any>} A new object with flattened keys.
*/
export declare function flattenObject<T extends Record<string, any>>(obj: T, prefix?: string): Record<string, any>;
/**
* Safely gets the value of a deeply nested key in an object using dot/bracket notation path.
*
* @template T
* @param {T} obj - The object to extract from.
* @param {string} path - String path using dot and/or bracket notation (e.g., 'user.friends[0].name').
* @returns {any} The value at the given path, or undefined if not found.
*/
export declare function getValueByPath<T extends object>(obj: T, path: string): any;
/**
* Sets a value at a deeply nested key in an object using dot/bracket notation path.
* Creates intermediate objects and arrays as needed.
*
* @template T
* @param {T} obj - The object to modify.
* @param {string} path - String path using dot and/or bracket notation (e.g., 'user.friends[0].name').
* @param {any} value - The value to set at the path.
* @returns {T} The modified object.
*/
export declare function setValueByPath<T extends object>(obj: T, path: string, value: any): T;
/**
* Performs a deep equality check between two objects.
*
* @param {any} a - First value to compare.
* @param {any} b - Second value to compare.
* @returns {boolean} True if values are deeply equal.
*/
export declare function isEqual(a: any, b: any): boolean;
/**
* Filters object properties based on a predicate function.
*
* @template T
* @param {T} obj - The source object.
* @param {(value: any, key: string, obj: T) => boolean} predicate - Filter function.
* @returns {Partial<T>} New object with filtered properties.
*/
export declare function filterObject<T extends object>(obj: T, predicate: (value: any, key: string, obj: T) => boolean): Partial<T>;
/**
* Maps object values to new values using a mapping function.
*
* @template T
* @template U
* @param {T} obj - The source object.
* @param {(value: any, key: string, obj: T) => U} mapFn - Mapping function.
* @returns {Record<keyof T, U>} New object with mapped values.
*/
export declare function mapObject<T extends object, U>(obj: T, mapFn: (value: any, key: string, obj: T) => U): Record<keyof T, U>;
/**
* Recursively freezes an object and all its properties.
* Makes an object immutable.
*
* @template T
* @param {T} obj - The object to freeze.
* @returns {Readonly<T>} The frozen object.
*/
export declare function deepFreeze<T extends object>(obj: T): Readonly<T>;
/**
* Safely checks if a value is an object (not null, not array).
*
* @param {unknown} value - Value to check.
* @returns {boolean} True if value is a non-null, non-array object.
*/
export declare function isObject(value: unknown): value is Record<string, any>;
/**
* Gets all paths in an object using dot notation.
*
* @param {Record<string, any>} obj - Object to analyze.
* @param {string} [parentPath=""] - Internal param for recursion.
* @returns {string[]} Array of all paths in dot notation.
*/
export declare function getAllPaths(obj: Record<string, any>, parentPath?: string): string[];
//# sourceMappingURL=obj.utils.d.ts.map