Function deepMerge
- deep
Merge <T>(record, other, options?): T Type Parameters
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.
Example: 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);Example: 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);Example: 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);- record: Partial<Readonly<T>>
- deep
Merge <T, U, Options>(record, other, 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
replacevalues, ormergethem 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
Returns DeepMerge<T, U, Options>
A new record with the merged values.
Example: 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);Example: 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);Example: 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);- T extends Record<PropertyKey, unknown>
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
replacevalues, ormergethem instead.