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

@victr/deepmerge

v1.3.2

Published

Merges the enumerable properties of two or more objects deeply.

Downloads

19

Readme

@victr/deepmerge

NPM version

This is an ESM only version of @fastify/deepmerge.
Merges the enumerable properties of two or more objects deeply. Fastest implementation of deepmerge, see section 'Benchmarks'.

Install

npm i @victr/deepmerge

Usage

The module exports a default function, which provides a function to deepmerge Objects. Also a deepmerge, and a deepmergeAll shorthand.

deepmerge(options)(...objects)
deepmerge(object1, object2)
deepmergeAll(...objects)

options is optional and can contain following values

  • symbols (boolean, optional) - should also merge object-keys which are symbols, default is false
  • all (boolean, optional) - merges all parameters, default is false
  • mergeArray (function, optional) - provide a function, which returns a function to add custom array merging function
  • cloneProtoObject (function, optional) - provide a function, which must return a clone of the object with the prototype of the object
import deepmerge from "@victr/deepmerge"

const result1 = deepmerge()({ a: "value" }, { b: 404 })
const result2 = deepmerge({ all: true })({ a: "value" }, { b: 404 }, { a: 404 })

console.log(result1) // {a: 'value',  b: 404 }
console.log(result2) // {a: 404,  b: 404 }

or without curry

import { deepmergeAll, deepmerge } from "@victr/deepmerge"

const result1 = deepmerge({ a: "value" }, { b: 404 })
const result2 = deepmergeAll({ a: "value" }, { b: 404 }, { a: 404 })

console.log(result1) // {a: 'value',  b: 404 }
console.log(result2) // {a: 404,  b: 404 }

mergeArray

The default mode to merge Arrays is to concat the source-Array to the target-Array.

import deepmerge from "@victr/deepmerge"

const target = [1, 2, 3]
const source = [4, 5, 6]
const result = deepmerge()(target, source)
console.log(result) // [1, 2, 3, 4, 5, 6]

To overwrite the default behaviour regarding merging Arrays, you can provide a function to the mergeArray option of the deepmerge-function. The function provided to mergeArray gets an options-parameter passed, which is an Object containing the following keys and values.

clone: (value: any) => any;
isMergeableObject: (value: any) => any;
deepmerge: DeepMergeFn;
getKeys: (value: object) => string[];

The mergeAray-Function needs to return the actual Array merging function, which accepts two parameters of type Array, and returns a value.

Example 1: Replace the target-Array with a clone of the source-Array.

import deepmerge from "@victr/deepmerge"

function replaceByClonedSource(options) {
	const clone = options.clone
	return function (target, source) {
		return clone(source)
	}
}

const result = deepmerge({ mergeArray: replaceByClonedSource })([1, 2, 3], [4, 5, 6])
console.log(result) // [4, 5, 6]

Example 2: Merge each element of the source-Array with the element at the same index-position of the target-Array.

import deepmerge from "@victr/deepmerge"

function deepmergeArray(options) {
	const deepmerge = options.deepmerge
	const clone = options.clone
	return function (target, source) {
		let i = 0
		const tl = target.length
		const sl = source.length
		const il = Math.max(target.length, source.length)
		const result = new Array(il)
		for (i = 0; i < il; ++i) {
			if (i < sl) {
				result[i] = deepmerge(target[i], source[i])
			} else {
				result[i] = clone(target[i])
			}
		}
		return result
	}
}

// default behaviour
const deepmergeConcatArray = deepmerge()
const resultConcatArray = deepmergeConcatArray([{ a: [1, 2, 3] }], [{ b: [4, 5, 6] }])
console.log(resultConcatArray) // [ { a: [ 1, 2, 3 ]}, { b: [ 4, 5, 6 ] } ]

// modified behaviour
const deepmergeDeepmergeArray = deepmerge({ mergeArray: deepmergeArray })
const resultDeepmergedArray = deepmergeDeepmergeArray([{ a: [1, 2, 3] }], [{ b: [4, 5, 6] }])
console.log(resultDeepmergedArray) // [ { a: [ 1, 2, 3 ], b: [ 4, 5, 6 ] } ]

cloneProtoObject

Merging objects with prototypes, such as Streams or Buffers, are not supported by default. You can provide a custom function to let this module deal with the object that has a prototype (JSON object excluded).

import deepmerge from "@victr/deepmerge"

function cloneByReference(source) {
	return source
}

const deepmergeByReference = deepmerge({
	cloneProtoObject: cloneByReference,
})

const result = deepmergeByReference({}, { stream: process.stdout })
console.log(result) // { stream: <ref *1> WriteStream }

Benchmarks

The benchmarks are available in the benchmark-folder.

npm run bench - benchmark various use cases of deepmerge:

@fastify/deepmerge: merge regex with date x 1,256,523,040 ops/sec ±0.16% (92 runs sampled)
@fastify/deepmerge: merge object with a primitive x 1,256,082,915 ops/sec ±0.25% (97 runs sampled)
@fastify/deepmerge: merge two arrays containing strings x 25,392,605 ops/sec ±0.22% (97 runs sampled)
@fastify/deepmerge: two merge arrays containing objects x 1,655,426 ops/sec ±0.65% (96 runs sampled)
@fastify/deepmerge: merge two flat objects x 15,571,029 ops/sec ±0.45% (96 runs sampled)
@fastify/deepmerge: merge nested objects x 7,601,328 ops/sec ±0.31% (96 runs sampled)

npm run bench:compare - comparison of @fastify/deepmerge with other popular deepmerge implementation:

@fastify/deepmerge x 605,343 ops/sec ±0.87% (96 runs sampled)
deepmerge x 20,312 ops/sec ±1.06% (92 runs sampled)
merge-deep x 83,167 ops/sec ±1.30% (94 runs sampled)
ts-deepmerge x 175,977 ops/sec ±0.57% (96 runs sampled)
deepmerge-ts x 174,973 ops/sec ±0.44% (93 runs sampled)
lodash.merge x 89,213 ops/sec ±0.70% (98 runs sampled)

License

Licensed under MIT.