UNPKG

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