UNPKG

1.62 kBTypeScriptView Raw
1export default assignWithDepth;
2/**
3 * @function assignWithDepth Extends the functionality of {@link ObjectConstructor.assign} with the
4 * ability to merge arbitrary-depth objects For each key in src with path `k` (recursively)
5 * performs an Object.assign(dst[`k`], src[`k`]) with a slight change from the typical handling of
6 * undefined for dst[`k`]: instead of raising an error, dst[`k`] is auto-initialized to {} and
7 * effectively merged with src[`k`]<p> Additionally, dissimilar types will not clobber unless the
8 * config.clobber parameter === true. Example:
9 *
10 * ```js
11 * let config_0 = { foo: { bar: 'bar' }, bar: 'foo' };
12 * let config_1 = { foo: 'foo', bar: 'bar' };
13 * let result = assignWithDepth(config_0, config_1);
14 * console.log(result);
15 * //-> result: { foo: { bar: 'bar' }, bar: 'bar' }
16 * ```
17 *
18 * Traditional Object.assign would have clobbered foo in config_0 with foo in config_1. If src is a
19 * destructured array of objects and dst is not an array, assignWithDepth will apply each element
20 * of src to dst in order.
21 * @param {any} dst - The destination of the merge
22 * @param {any} src - The source object(s) to merge into destination
23 * @param {{ depth: number; clobber: boolean }} [config={ depth: 2, clobber: false }] - Depth: depth
24 * to traverse within src and dst for merging - clobber: should dissimilar types clobber (default:
25 * { depth: 2, clobber: false }). Default is `{ depth: 2, clobber: false }`
26 * @returns {any}
27 */
28declare function assignWithDepth(dst: any, src: any, config?: {
29 depth: number;
30 clobber: boolean;
31} | undefined): any;