| 1 | /**
|
|---|
| 2 | * Merges the properties of the source object into the target object.
|
|---|
| 3 | *
|
|---|
| 4 | * You can provide a custom `merge` function to control how properties are merged. It should return the value to be set in the target object.
|
|---|
| 5 | *
|
|---|
| 6 | * If it returns `undefined`, a default deep merge will be applied for arrays and objects:
|
|---|
| 7 | *
|
|---|
| 8 | * - If a property in the source object is an array or an object and the corresponding property in the target object is also an array or object, they will be merged.
|
|---|
| 9 | * - If a property in the source object is undefined, it will not overwrite a defined property in the target object.
|
|---|
| 10 | *
|
|---|
| 11 | * Note that this function mutates the target object.
|
|---|
| 12 | *
|
|---|
| 13 | * @param {T} target - The target object into which the source object properties will be merged. This object is modified in place.
|
|---|
| 14 | * @param {S} source - The source object whose properties will be merged into the target object.
|
|---|
| 15 | * @param {(targetValue: any, sourceValue: any, key: string, target: T, source: S) => any} merge - A custom merge function that defines how properties should be combined. It receives the following arguments:
|
|---|
| 16 | * - `targetValue`: The current value of the property in the target object.
|
|---|
| 17 | * - `sourceValue`: The value of the property in the source object.
|
|---|
| 18 | * - `key`: The key of the property being merged.
|
|---|
| 19 | * - `target`: The target object.
|
|---|
| 20 | * - `source`: The source object.
|
|---|
| 21 | *
|
|---|
| 22 | * @returns {T & S} The updated target object with properties from the source object merged in.
|
|---|
| 23 | *
|
|---|
| 24 | * @template T - Type of the target object.
|
|---|
| 25 | * @template S - Type of the source object.
|
|---|
| 26 | *
|
|---|
| 27 | * @example
|
|---|
| 28 | * const target = { a: 1, b: 2 };
|
|---|
| 29 | * const source = { b: 3, c: 4 };
|
|---|
| 30 | *
|
|---|
| 31 | * mergeWith(target, source, (targetValue, sourceValue) => {
|
|---|
| 32 | * if (typeof targetValue === 'number' && typeof sourceValue === 'number') {
|
|---|
| 33 | * return targetValue + sourceValue;
|
|---|
| 34 | * }
|
|---|
| 35 | * });
|
|---|
| 36 | * // Returns { a: 1, b: 5, c: 4 }
|
|---|
| 37 | * @example
|
|---|
| 38 | * const target = { a: [1], b: [2] };
|
|---|
| 39 | * const source = { a: [3], b: [4] };
|
|---|
| 40 | *
|
|---|
| 41 | * const result = mergeWith(target, source, (objValue, srcValue) => {
|
|---|
| 42 | * if (Array.isArray(objValue)) {
|
|---|
| 43 | * return objValue.concat(srcValue);
|
|---|
| 44 | * }
|
|---|
| 45 | * });
|
|---|
| 46 | *
|
|---|
| 47 | * expect(result).toEqual({ a: [1, 3], b: [2, 4] });
|
|---|
| 48 | */
|
|---|
| 49 | declare function mergeWith<T extends Record<PropertyKey, any>, S extends Record<PropertyKey, any>>(target: T, source: S, merge: (targetValue: any, sourceValue: any, key: string, target: T, source: S) => any): T & S;
|
|---|
| 50 |
|
|---|
| 51 | export { mergeWith };
|
|---|