Function deepMerge

  • Merges the two given records, recursively merging any nested records with the second collection overriding the first in case of conflict.

    For arrays, maps and sets, a merging strategy can be specified to either replace values, or merge them instead.

    Type Parameters

    • T extends Record<PropertyKey, unknown>

      Type of the first record

    Parameters

    • record: Partial<Readonly<T>>

      First record to merge.

    • other: Partial<Readonly<T>>

      Second record to merge.

    • Optionaloptions: Readonly<DeepMergeOptions>

      Merging options.

    Returns T

    A new record with the merged values.

    import { deepMerge } from "@std/collections/deep-merge";
    import { assertEquals } from "@std/assert";

    const a = { foo: true };
    const b = { foo: { bar: true } };

    const result = deepMerge(a, b);

    const expected = { foo: { bar: true } };

    assertEquals(result, expected);
    import { deepMerge } from "@std/collections/deep-merge";
    import { assertEquals } from "@std/assert";

    const a = { foo: [1, 2] };
    const b = { foo: [3, 4] };

    const result = deepMerge(a, b);

    const expected = { foo: [1, 2, 3, 4] };

    assertEquals(result, expected);
    import { deepMerge } from "@std/collections/deep-merge";
    import { assertEquals } from "@std/assert";

    const a = { foo: new Map([["a", 1]]) };
    const b = { foo: new Map([["b", 2]]) };

    const result = deepMerge(a, b);

    const expected = { foo: new Map([["a", 1], ["b", 2]]) };

    assertEquals(result, expected);
    import { deepMerge } from "@std/collections/deep-merge";
    import { assertEquals } from "@std/assert";

    const a = { foo: new Set([1]) };
    const b = { foo: new Set([2]) };

    const result = deepMerge(a, b);

    const expected = { foo: new Set([1, 2]) };

    assertEquals(result, expected);
    import { deepMerge } from "@std/collections/deep-merge";
    import { assertEquals } from "@std/assert";

    const a = { foo: [1, 2] };
    const b = { foo: [3, 4] };

    const result = deepMerge(a, b, { arrays: "replace" });

    const expected = { foo: [3, 4] };

    assertEquals(result, expected);
  • Merges the two given records, recursively merging any nested records with the second collection overriding the first in case of conflict.

    For arrays, maps and sets, a merging strategy can be specified to either replace values, or merge them instead.

    Type Parameters

    • T extends Record<PropertyKey, unknown>

      Type of the first record

    • U extends Record<PropertyKey, unknown>

      Type of the second record

    • Options extends DeepMergeOptions

      Merging options

    Parameters

    • record: Readonly<T>

      First record to merge.

    • other: Readonly<U>

      Second record to merge.

    • Optionaloptions: Readonly<Options>

      Merging options.

    Returns DeepMerge<T, U, Options>

    A new record with the merged values.

    import { deepMerge } from "@std/collections/deep-merge";
    import { assertEquals } from "@std/assert";

    const a = { foo: true };
    const b = { foo: { bar: true } };

    const result = deepMerge(a, b);

    const expected = { foo: { bar: true } };

    assertEquals(result, expected);
    import { deepMerge } from "@std/collections/deep-merge";
    import { assertEquals } from "@std/assert";

    const a = { foo: [1, 2] };
    const b = { foo: [3, 4] };

    const result = deepMerge(a, b);

    const expected = { foo: [1, 2, 3, 4] };

    assertEquals(result, expected);
    import { deepMerge } from "@std/collections/deep-merge";
    import { assertEquals } from "@std/assert";

    const a = { foo: new Map([["a", 1]]) };
    const b = { foo: new Map([["b", 2]]) };

    const result = deepMerge(a, b);

    const expected = { foo: new Map([["a", 1], ["b", 2]]) };

    assertEquals(result, expected);
    import { deepMerge } from "@std/collections/deep-merge";
    import { assertEquals } from "@std/assert";

    const a = { foo: new Set([1]) };
    const b = { foo: new Set([2]) };

    const result = deepMerge(a, b);

    const expected = { foo: new Set([1, 2]) };

    assertEquals(result, expected);
    import { deepMerge } from "@std/collections/deep-merge";
    import { assertEquals } from "@std/assert";

    const a = { foo: [1, 2] };
    const b = { foo: [3, 4] };

    const result = deepMerge(a, b, { arrays: "replace" });

    const expected = { foo: [3, 4] };

    assertEquals(result, expected);