https://github.com/75lb/deep-merge
Deep merge config objects
https://github.com/75lb/deep-merge
Last synced: over 1 year ago
JSON representation
Deep merge config objects
- Host: GitHub
- URL: https://github.com/75lb/deep-merge
- Owner: 75lb
- License: mit
- Created: 2021-06-09T11:20:15.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-07-31T20:40:39.000Z (almost 2 years ago)
- Last Synced: 2025-03-18T02:51:21.208Z (over 1 year ago)
- Language: JavaScript
- Size: 166 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.npmjs.org/package/@75lb/deep-merge)
[](https://www.npmjs.org/package/@75lb/deep-merge)
[](https://github.com/75lb/deep-merge/network/dependents?dependent_type=REPOSITORY)
[](https://github.com/75lb/deep-merge/network/dependents?dependent_type=PACKAGE)
[](https://github.com/75lb/deep-merge/actions/workflows/node.js.yml)
[](https://github.com/feross/standard)
# @75lb/deep-merge
Deep-merge the values of one object structure into another. Similar to [`Object.assign()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) except it processes the full depth of the object structure, not only the top level. Useful for merging config.
## Synopsis
```js
import deepMerge from '@75lb/deep-merge'
```
### Simple
Typical example merging four objects. Input:
```js
deepMerge(
{ port: 8000, data: { animal: 'cow' } },
{ stack: ['one'] },
{ stack: ['two'], help: true },
{ data: { animal: 'bat', metal: 'iron' } }
)
```
Result
```js
{
port: 8000,
stack: ['two'],
help: true,
data: { animal: 'bat', metal: 'iron' }
}
```
### Arrays
Empty arrays are ignored and not merged in. Input:
```js
deepMerge(
{ stack: ['one'] },
{ stack: [] }
)
```
Result:
```js
{ stack: ['one'] }
```
However, if the later array contains one or more values the later array will *replace* the original:
```js
deepMerge(
{ stack: ['one'] },
{ stack: ['two'] }
)
```
Result:
```js
{ 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:
```js
import deepMerge from '@75lb/deep-merge'
```
Within an modern browser ECMAScript Module:
```js
import deepMerge from './node_modules/@75lb/deep-merge/dist/index.mjs'
```
* * *
© 2018-24 [Lloyd Brookes](https://github.com/75lb) \<75pound@gmail.com\>.
Tested by [test-runner](https://github.com/test-runner-js/test-runner). Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).