Gathering detailed insights and metrics for deepmerge
Gathering detailed insights and metrics for deepmerge
Gathering detailed insights and metrics for deepmerge
Gathering detailed insights and metrics for deepmerge
@bundled-es-modules/deepmerge
mirror of deepmerge, bundled and exposed as ES module
@webundsoehne/deep-merge
Generic object deepmerge.
deepmerge-ts
Deeply merge 2 or more objects respecting type information.
@fastify/deepmerge
Merges the enumerable properties of two or more objects deeply.
A library for deep (recursive) merging of Javascript objects
npm install deepmerge
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2,765 Stars
331 Commits
217 Forks
16 Watching
4 Branches
35 Contributors
Updated on 23 Nov 2024
Minified
Minified + Gzipped
JavaScript (95.85%)
TypeScript (4.15%)
Cumulative downloads
Total Downloads
Last day
-2.3%
7,879,156
Compared to previous day
Last week
3.5%
42,242,342
Compared to previous week
Last month
16.4%
169,852,115
Compared to previous month
Last year
12.6%
1,759,008,710
Compared to previous year
Merges the enumerable properties of two or more objects deeply.
UMD bundle is 723B minified+gzipped
1const x = { 2 foo: { bar: 3 }, 3 array: [{ 4 does: 'work', 5 too: [ 1, 2, 3 ] 6 }] 7} 8 9const y = { 10 foo: { baz: 4 }, 11 quux: 5, 12 array: [{ 13 does: 'work', 14 too: [ 4, 5, 6 ] 15 }, { 16 really: 'yes' 17 }] 18} 19 20const output = { 21 foo: { 22 bar: 3, 23 baz: 4 24 }, 25 array: [{ 26 does: 'work', 27 too: [ 1, 2, 3 ] 28 }, { 29 does: 'work', 30 too: [ 4, 5, 6 ] 31 }, { 32 really: 'yes' 33 }], 34 quux: 5 35} 36 37merge(x, y) // => output
With npm do:
1npm install deepmerge
deepmerge can be used directly in the browser without the use of package managers/bundlers as well: UMD version from unpkg.com.
deepmerge exposes a CommonJS entry point:
const merge = require('deepmerge')
The ESM entry point was dropped due to a Webpack bug.
merge(x, y, [options])
Merge two objects x
and y
deeply, returning a new merged object with the
elements from both x
and y
.
If an element at the same key is present for both x
and y
, the value from
y
will appear in the result.
Merging creates a new object, so that neither x
or y
is modified.
Note: By default, arrays are merged by concatenating them.
merge.all(arrayOfObjects, [options])
Merges any number of objects into a single result object.
1const foobar = { foo: { bar: 3 } } 2const foobaz = { foo: { baz: 4 } } 3const bar = { bar: 'yay!' } 4 5merge.all([ foobar, foobaz, bar ]) // => { foo: { bar: 3, baz: 4 }, bar: 'yay!' }
arrayMerge
There are multiple ways to merge two arrays, below are a few examples but you can also create your own custom function.
Your arrayMerge
function will be called with three arguments: a target
array, the source
array, and an options
object with these properties:
isMergeableObject(value)
cloneUnlessOtherwiseSpecified(value, options)
arrayMerge
example: overwrite target arrayOverwrites the existing array values completely rather than concatenating them:
1const overwriteMerge = (destinationArray, sourceArray, options) => sourceArray 2 3merge( 4 [1, 2, 3], 5 [3, 2, 1], 6 { arrayMerge: overwriteMerge } 7) // => [3, 2, 1]
arrayMerge
example: combine arraysCombines objects at the same index in the two arrays.
This was the default array merging algorithm pre-version-2.0.0.
1const combineMerge = (target, source, options) => { 2 const destination = target.slice() 3 4 source.forEach((item, index) => { 5 if (typeof destination[index] === 'undefined') { 6 destination[index] = options.cloneUnlessOtherwiseSpecified(item, options) 7 } else if (options.isMergeableObject(item)) { 8 destination[index] = merge(target[index], item, options) 9 } else if (target.indexOf(item) === -1) { 10 destination.push(item) 11 } 12 }) 13 return destination 14} 15 16merge( 17 [{ a: true }], 18 [{ b: true }, 'ah yup'], 19 { arrayMerge: combineMerge } 20) // => [{ a: true, b: true }, 'ah yup']
isMergeableObject
By default, deepmerge clones every property from almost every kind of object.
You may not want this, if your objects are of special types, and you want to copy the whole object instead of just copying its properties.
You can accomplish this by passing in a function for the isMergeableObject
option.
If you only want to clone properties of plain objects, and ignore all "special" kinds of instantiated objects, you probably want to drop in is-plain-object
.
1const { isPlainObject } = require('is-plain-object') 2 3function SuperSpecial() { 4 this.special = 'oh yeah man totally' 5} 6 7const instantiatedSpecialObject = new SuperSpecial() 8 9const target = { 10 someProperty: { 11 cool: 'oh for sure' 12 } 13} 14 15const source = { 16 someProperty: instantiatedSpecialObject 17} 18 19const defaultOutput = merge(target, source) 20 21defaultOutput.someProperty.cool // => 'oh for sure' 22defaultOutput.someProperty.special // => 'oh yeah man totally' 23defaultOutput.someProperty instanceof SuperSpecial // => false 24 25const customMergeOutput = merge(target, source, { 26 isMergeableObject: isPlainObject 27}) 28 29customMergeOutput.someProperty.cool // => undefined 30customMergeOutput.someProperty.special // => 'oh yeah man totally' 31customMergeOutput.someProperty instanceof SuperSpecial // => true
customMerge
Specifies a function which can be used to override the default merge behavior for a property, based on the property name.
The customMerge
function will be passed the key for each property, and should return the function which should be used to merge the values for that property.
It may also return undefined, in which case the default merge behaviour will be used.
1const alex = { 2 name: { 3 first: 'Alex', 4 last: 'Alexson' 5 }, 6 pets: ['Cat', 'Parrot'] 7} 8 9const tony = { 10 name: { 11 first: 'Tony', 12 last: 'Tonison' 13 }, 14 pets: ['Dog'] 15} 16 17const mergeNames = (nameA, nameB) => `${nameA.first} and ${nameB.first}` 18 19const options = { 20 customMerge: (key) => { 21 if (key === 'name') { 22 return mergeNames 23 } 24 } 25} 26 27const result = merge(alex, tony, options) 28 29result.name // => 'Alex and Tony' 30result.pets // => ['Cat', 'Parrot', 'Dog']
clone
Deprecated.
Defaults to true
.
If clone
is false
then child objects will be copied directly instead of being cloned. This was the default behavior before version 2.x.
With npm do:
1npm test
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
7 existing vulnerabilities detected
Details
Reason
Found 5/22 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More