npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

deepmerge-ts

v7.1.3

Published

Deeply merge 2 or more objects respecting type information.

Downloads

4,689,397

Readme

DeepmergeTS

npm version jsr Version CI Coverage Status
code style: prettier GitHub Discussions BSD 3 Clause license Commitizen friendly semantic-release

Deeply merge 2 or more objects respecting type information.

smart merge diagram

Donate

Any donations would be much appreciated. 😄

Enterprise Users

deepmerge-ts is available as part of the Tidelift Subscription.

Tidelift is working with the maintainers of deepmerge-ts and a growing network of open source maintainers to ensure your open source software supply chain meets enterprise standards now and into the future. Learn more.

Installation

npm

# Install with npm
npm install deepmerge-ts

# Install with pnpm
pnpm add deepmerge-ts

# Install with yarn
yarn add deepmerge-ts

# Install with bun
bun add deepmerge-ts

jsr

# Install in a node project
npx jsr add @rebeccastevens/deepmerge

# Install in a deno project
deno add jsr:@rebeccastevens/deepmerge

# Install in a bun project
bunx jsr add @rebeccastevens/deepmerge

Features

  • Smart merging - High performance.
  • Merged output has correct typing.
  • Record merging support.
  • Array merging support.
  • Map and Set merging support.
  • Customized merging.

Usage

Example using default config

import { deepmerge } from "deepmerge-ts";

const x = {
  record: {
    prop1: "value1",
    prop2: "value2",
  },
  array: [1, 2, 3],
  set: new Set([1, 2, 3]),
  map: new Map([
    ["key1", "value1"],
    ["key2", "value2"],
  ]),
};

const y = {
  record: {
    prop1: "changed",
    prop3: "value3",
  },
  array: [2, 3, 4],
  set: new Set([2, 3, 4]),
  map: new Map([
    ["key2", "changed"],
    ["key3", "value3"],
  ]),
};

const z = {
  record: {
    prop1: undefined,
    prop3: undefined,
    prop2: undefined,
    prop4: undefined,
  },
  array: undefined,
  set: undefined,
  map: undefined,
};

const merged = deepmerge(x, y, z);

console.log(merged);

// Prettierfied output:
//
// Object {
//   "record": Object {
//     "prop1": "changed",
//     "prop2": "value2",
//     "prop3": "value3",
//     "prop4": undefined,
//   },
//   "array": Array [1, 2, 3, 2, 3, 4],
//   "set": Set { 1, 2, 3, 4 },
//   "map": Map {
//     "key1" => "value1",
//     "key2" => "changed",
//     "key3" => "value3",
//   },
// }

You can try out this example at codesandbox.io.

Merging into a Target

You can use deepmergeInto if you want to update a target object with the merge result instead of creating a new object.

This function is best used with objects that are all of the same type.

Note: If the target object's type differs from the input objects, we'll assert that the target's type has changed (this is not done automatically with deepmergeIntoCustom).

Customized the Merging Process

We provide a customizer function for each of our main deepmerge functions: deepmergeCustom and deepmergeIntoCustom. You can use these to customize the details of how values should be merged together.

See deepmerge custom docs for more details.

Performance

We use smart merging instead of the classic merging strategy which some alternative libraries use. This vastly improves performance, both in execution time and memory usage.

Classic Merge (not what we do)

With classic merging, each input is merged with the next input until all inputs are merged.

This strategy has large performance issues when lots of items need to be merged.

classic merge animation

Smart Merge (what we do)

With our smart merging, we look ahead to see what can be merged and only merge those things.

In addition to performance improvements, this strategy merges multiple inputs at once; allowing for benefits such as taking averages of the inputs.

smart merge animation

API

See API docs.