Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@75lb/deep-merge
Advanced tools
@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.
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 } }
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.
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 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.
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.
import deepMerge from '@75lb/deep-merge'
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' }
}
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'] }
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'
© 2018-24 Lloyd Brookes <75pound@gmail.com>.
Tested by test-runner. Documented by jsdoc-to-markdown.
FAQs
Deep-merge the values of one object structure into another
The npm package @75lb/deep-merge receives a total of 0 weekly downloads. As such, @75lb/deep-merge popularity was classified as not popular.
We found that @75lb/deep-merge demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.