UNPKG

@andranik-arakelyan/js-utilities

Version:
43 lines (42 loc) 1.34 kB
/** * Deep merges one object into another object, optionally at a specific path. * * @param target - The target object to merge into * @param source - The source object to merge from * @param path - Optional dot-separated path where the merge should occur (e.g., 'user.profiles.0.transactions') * @returns The merged object (modifies the target object) * * @example * ```ts * // Basic deep merge * const target = { a: 1, b: { c: 2 } }; * const source = { b: { d: 3 }, e: 4 }; * const result = deepMerge(target, source); * // { a: 1, b: { c: 2, d: 3 }, e: 4 } * ``` * * @example * ```ts * // Merge at specific path * const target = { * user: { * profiles: [ * { id: 1, transactions: { count: 5 } } * ] * } * }; * const source = { total: 100, recent: ['tx1', 'tx2'] }; * deepMerge(target, source, 'user.profiles.0.transactions'); * // Result: target.user.profiles[0].transactions now has count, total, and recent properties * ``` * * @example * ```ts * // Array handling - arrays are replaced, not merged * const target = { items: [1, 2, 3] }; * const source = { items: [4, 5] }; * deepMerge(target, source); * // { items: [4, 5] } - arrays are replaced * ``` */ export declare function deepMerge<T extends Record<string, any>>(target: T, source: Record<string, any>, path?: string): T;