deepMerge<T extends Record<PropertyKey, unknown>>(): T
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.
Merge objects
Merge objects
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);
Merge arrays
Merge arrays
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);
Merge maps
Merge maps
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);
Merge sets
Merge sets
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);
Merge with custom options
Merge with custom options
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);
record: Partial<Readonly<T>>
First record to merge.
other: Partial<Readonly<T>>
Second record to merge.
options: Readonly<DeepMergeOptions>
Merging options.
A new record with the merged values.
deepMerge<T extends Record<PropertyKey, unknown>,U extends Record<PropertyKey, unknown>,Options extends DeepMergeOptions,>(record: Readonly<T>,other: Readonly<U>,options?: Readonly<Options>,): DeepMerge<T, U, Options>
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.
Merge objects
Merge objects
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);
Merge arrays
Merge arrays
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);
Merge maps
Merge maps
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);
Merge sets
Merge sets
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);
Merge with custom options
Merge with custom options
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);
Options extends DeepMergeOptions
Merging options