What is @75lb/deep-merge?
@75lb/deep-merge is an npm package designed to deeply merge multiple objects into one. It handles nested objects and arrays, providing a robust solution for combining complex data structures.
What are @75lb/deep-merge's main functionalities?
Basic Deep Merge
This feature allows you to merge two objects deeply, combining their properties. Nested objects are merged recursively.
const deepMerge = require('@75lb/deep-merge');
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { b: { d: 3 } };
const result = deepMerge(obj1, obj2);
console.log(result); // { a: 1, b: { c: 2, d: 3 } }
Merging Arrays
This feature demonstrates how arrays within objects are merged. The arrays are concatenated.
const deepMerge = require('@75lb/deep-merge');
const obj1 = { a: [1, 2, 3] };
const obj2 = { a: [4, 5] };
const result = deepMerge(obj1, obj2);
console.log(result); // { a: [1, 2, 3, 4, 5] }
Custom Merge Function
This feature allows you to provide a custom merge function to control how specific types of values are merged.
const deepMerge = require('@75lb/deep-merge');
const customMerge = (target, source) => Array.isArray(target) ? target.concat(source) : undefined;
const obj1 = { a: [1, 2], b: { c: 3 } };
const obj2 = { a: [3, 4], b: { d: 4 } };
const result = deepMerge(obj1, obj2, customMerge);
console.log(result); // { a: [1, 2, 3, 4], b: { c: 3, d: 4 } }
Other packages similar to @75lb/deep-merge
lodash.merge
Lodash's merge function is a popular utility for deep merging objects. It is part of the larger Lodash library, which provides a wide range of utility functions for JavaScript. Compared to @75lb/deep-merge, lodash.merge is more widely used and comes with the additional overhead of the entire Lodash library.
deepmerge
The deepmerge package is a standalone library specifically designed for deep merging objects. It is lightweight and focuses solely on merging, similar to @75lb/deep-merge. However, deepmerge has a larger user base and more frequent updates.
merge-deep
merge-deep is another package focused on deep merging objects. It offers similar functionality to @75lb/deep-merge but also includes options for customizing the merge behavior. It is a good alternative if you need more control over the merging process.
@75lb/deep-merge
Deep-merge the values of one object structure into another. Similar to Object.assign()
except it processes the full depth of the object structure, not only the top level. Useful for merging config.
Synopsis
import deepMerge from '@75lb/deep-merge'
Simple
Typical example merging four objects. Input:
deepMerge(
{ port: 8000, data: { animal: 'cow' } },
{ stack: ['one'] },
{ stack: ['two'], help: true },
{ data: { animal: 'bat', metal: 'iron' } }
)
Result
{
port: 8000,
stack: ['two'],
help: true,
data: { animal: 'bat', metal: 'iron' }
}
Arrays
Empty arrays are ignored and not merged in. Input:
deepMerge(
{ stack: ['one'] },
{ stack: [] }
)
Result:
{ stack: ['one'] }
However, if the later array contains one or more values the later array will replace the original:
deepMerge(
{ stack: ['one'] },
{ stack: ['two'] }
)
Result:
{ stack: ['two'] }
Load anywhere
This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation.
Within a Node.js ECMAScript Module:
import deepMerge from '@75lb/deep-merge'
Within an modern browser ECMAScript Module:
import deepMerge from './node_modules/@75lb/deep-merge/dist/index.mjs'
© 2021 Lloyd Brookes <75pound@gmail.com>.
Tested by test-runner. Documented by jsdoc-to-markdown.