Skip to main content

Built and signed on GitHub Actions

Pure functions for common tasks related to collection types like arrays and objects

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
3 months ago (1.0.10)

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

function deepMerge
deepMerge<T extends Record<PropertyKey, unknown>>(
record: Partial<Readonly<T>>,
other: Partial<Readonly<T>>,
options?: Readonly<DeepMergeOptions>,
): 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.

Examples

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

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

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

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

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);

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.

optional
options: Readonly<DeepMergeOptions>

Merging options.

Return Type

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.

Examples

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

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

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

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

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);

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.

optional
options: Readonly<Options>

Merging options.

Return Type

A new record with the merged values.

Add Package

deno add jsr:@std/collections

Import symbol

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

---- OR ----

Import directly with a jsr specifier

import { deepMerge } from "jsr:@std/collections/deep-merge";

Add Package

npx jsr add @std/collections

Import symbol

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

Add Package

yarn dlx jsr add @std/collections

Import symbol

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

Add Package

pnpm dlx jsr add @std/collections

Import symbol

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

Add Package

bunx jsr add @std/collections

Import symbol

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