Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
@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 270,600 weekly downloads. As such, @75lb/deep-merge popularity was classified as 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
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.