5.1.0 • Published 1 year ago

deepmerge-ts v5.1.0

Weekly downloads
-
License
BSD-3-Clause
Repository
github
Last release
1 year ago

DeepmergeTS

npm version deno 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. 😄

Installation

Node

# Install with npm
npm install deepmerge-ts

# Install with pnpm
pnpm add deepmerge-ts

# Install with yarn
yarn add deepmerge-ts

Deno

// import_map.json
{
  "imports": {
    "deepmerge-ts": "https://deno.land/x/deepmergets@__version__/dist/deno/index.ts"
  }
}

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 merged = deepmerge(x, y);

console.log(merged);

// Prettierfied output:
//
// Object {
//   "record": Object {
//     "prop1": "changed",
//     "prop2": "value2",
//     "prop3": "value3",
//   },
//   "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 is different to 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.

write-pkg@yesmaintenance/scriptsastro-no-undiciastro-img@infinitebrahmanuniverse/nolb-deeopenstamanageres-utils-tsincremental-astrointegreat@everything-registry/sub-chunk-1460tdk-giveawaysgladdishexo-seogivibottgivifygraphql-codegen-faker-fixtureshasura-component-libhasura-console-component-library-1google-srhx-easy-apilionconfiglocaldev-tui@virgo-ui/vueastro-romebaxast@playform/biome-astro@playform/rome-astroeslint-plugin-functionaleslint-plugin-functional-noshiro-fixedfiles-pipefiles-pipelineember-native-class-codemodexsta-giveawaysexstadev-giveawaysf5-corkscreweslint-config-really-strictgit-command-helpernx-ts-confignext-routes-dirportfolio-managerpapuapower-giveawaysprisma-selectionsreact-hook-form-utilspug-doc-generatorreact-floaterreact-app-layoutssbg-clisbox-reactreact-native-responsive-table-viewremark-mdatsgtmshepherd.jstypescript-esbuildtypegeesestoatspot-storesitevision-toolstrpc-selectionstyped-restful@lightrix/scripts@kyndryl-design-system/shidoka-charts@kumori/kam@playform/format@playform/maintain@playform/pipe@playform/build@playform/file-pipe@pluslabs/style-guide@prisma-extensions/factory@pubnub/tomato@nikolarhristov/maintenance@nikolarhristov/pipeline@puppedo/core@freeday-ai/webchat-widgetzompt@ezweb/graphql-helpers@fiori/home-page@fireproof/solid-js@francyfox/fn-forms@scampersand/react-theme-provideruse-a11y-formvante-giveawayvante-giveawaysvante.ai@otterlord/astro-rome@pandaria/panda-preset@leonzalion/configs@hyvor/design@langos/cli@odx/angular@odx/auth@lla-ui/theme@loccy/cli@ishtar-gate/utils@isr4astro/astro@inlang/core@roenlie/mirage-docs@roenlie/package-toolbox@roenlie/lit-aegis
5.1.0

1 year ago

5.0.0

1 year ago

5.0.0-next.3

1 year ago

5.0.0-next.4

1 year ago

5.0.0-next.2

1 year ago

4.3.0

1 year ago

5.0.0-next.1

1 year ago

4.2.2

2 years ago

4.0.4

2 years ago

4.1.0

2 years ago

4.2.1

2 years ago

4.2.0

2 years ago

4.0.1

2 years ago

4.0.3

2 years ago

4.0.2

2 years ago

4.0.0-beta.2

2 years ago

4.0.0

2 years ago

3.0.0-beta.1

2 years ago

3.0.0-beta.3

2 years ago

3.0.0-beta.2

2 years ago

3.0.0-beta.5

2 years ago

3.0.0-beta.4

2 years ago

3.0.0-beta.7

2 years ago

3.0.0-beta.6

2 years ago

3.1.0-beta.1

2 years ago

2.0.1

2 years ago

3.0.1

2 years ago

4.0.0-beta.1

2 years ago

3.0.0

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.8-beta.1

2 years ago

2.0.0-beta.1

2 years ago

2.0.0

2 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago