@kitiumai/utils-ts
Version:
Comprehensive TypeScript utilities for KitiumAI projects
159 lines • 4.56 kB
TypeScript
/**
* Object utility functions
*/
/**
* Deep merge objects
*/
export declare function deepMerge<T extends Record<string, unknown>>(target: T, ...sources: Partial<T>[]): T;
/**
* Deep clone an object
*/
export declare function deepClone<T>(obj: T): T;
/**
* Pick specific properties from object
*/
export declare function pick<T extends object, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
/**
* Omit specific properties from object
*/
export declare function omit<T extends object, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>;
/**
* Safe deep property access with default value
*/
export declare function get<T = any>(obj: any, path: string | string[], defaultValue?: T): T;
/**
* Safe deep property set
*/
export declare function set<T>(obj: any, path: string | string[], value: T): void;
/**
* Deep strict equality check
*/
export declare function isEqual(a: any, b: any): boolean;
/**
* Transform object keys
*/
export declare function mapKeys<T>(obj: Record<string, T>, fn: (key: string, value: T) => string): Record<string, T>;
/**
* Transform object values
*/
export declare function mapValues<T, R>(obj: Record<string, T>, fn: (value: T, key: string) => R): Record<string, R>;
/**
* Check if value is a plain object
*/
export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
/**
* Check if object has property
*/
export declare function has(obj: any, key: string | string[]): boolean;
/**
* Invert object keys and values
*/
export declare function invert<K extends string | number, V extends string | number>(obj: Record<K, V>): Record<V, K>;
/**
* Fill missing properties with defaults
*
* @template T - The type of the target object
* @param target - The target object
* @param sources - Source objects with default values
* @returns A new object with defaults applied
*
* @example
* ```ts
* defaults({ a: 1 }, { b: 2, c: 3 }) // { a: 1, b: 2, c: 3 }
* ```
*/
export declare function defaults<T extends Record<string, unknown>>(target: T, ...sources: Partial<T>[]): T;
/**
* Fill missing properties with defaults (deep merge)
*
* @template T - The type of the target object
* @param target - The target object
* @param sources - Source objects with default values
* @returns A new object with deep defaults applied
*
* @example
* ```ts
* defaultsDeep({ a: { b: 1 } }, { a: { c: 2 } }) // { a: { b: 1, c: 2 } }
* ```
*/
export declare function defaultsDeep<T extends Record<string, unknown>>(target: T, ...sources: Partial<T>[]): T;
/**
* Get all keys of an object (type-safe)
*
* @template T - The type of the object
* @param obj - The object
* @returns An array of keys
*
* @example
* ```ts
* keys({ a: 1, b: 2 }) // ['a', 'b']
* ```
*/
export declare function keys<T extends Record<string, unknown>>(obj: T): Array<keyof T>;
/**
* Get all values of an object (type-safe)
*
* @template T - The type of the object
* @param obj - The object
* @returns An array of values
*
* @example
* ```ts
* values({ a: 1, b: 2 }) // [1, 2]
* ```
*/
export declare function values<T extends Record<string, unknown>>(obj: T): Array<T[keyof T]>;
/**
* Get all entries of an object (type-safe)
*
* @template T - The type of the object
* @param obj - The object
* @returns An array of [key, value] pairs
*
* @example
* ```ts
* entries({ a: 1, b: 2 }) // [['a', 1], ['b', 2]]
* ```
*/
export declare function entries<T extends Record<string, unknown>>(obj: T): Array<[keyof T, T[keyof T]]>;
/**
* Create an object from an array of [key, value] pairs
*
* @template T - The type of values
* @param pairs - Array of [key, value] pairs
* @returns An object created from the pairs
*
* @example
* ```ts
* fromPairs([['a', 1], ['b', 2]]) // { a: 1, b: 2 }
* ```
*/
export declare function fromPairs<T>(pairs: Array<[string, T]>): Record<string, T>;
/**
* Convert an object to an array of [key, value] pairs
*
* @template T - The type of values
* @param obj - The object to convert
* @returns An array of [key, value] pairs
*
* @example
* ```ts
* toPairs({ a: 1, b: 2 }) // [['a', 1], ['b', 2]]
* ```
*/
export declare function toPairs<T>(obj: Record<string, T>): Array<[string, T]>;
/**
* Get the size of a collection (array, string, or object)
*
* @param value - The value to get size of
* @returns The size of the collection
*
* @example
* ```ts
* size([1, 2, 3]) // 3
* size('hello') // 5
* size({ a: 1, b: 2 }) // 2
* ```
*/
export declare function size(value: unknown): number;
//# sourceMappingURL=object.d.ts.map