Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/riversun/merge-deeply
Clone object with prototype ,and deeply merge (deep copy) properties of two JS objects.
https://github.com/riversun/merge-deeply
clone deep-copy deep-merge js-object json prototype
Last synced: 17 days ago
JSON representation
Clone object with prototype ,and deeply merge (deep copy) properties of two JS objects.
- Host: GitHub
- URL: https://github.com/riversun/merge-deeply
- Owner: riversun
- License: mit
- Created: 2020-01-28T08:11:44.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T04:23:42.000Z (almost 2 years ago)
- Last Synced: 2024-11-07T04:20:55.300Z (about 2 months ago)
- Topics: clone, deep-copy, deep-merge, js-object, json, prototype
- Language: JavaScript
- Homepage:
- Size: 1.56 MB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 22
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# merge-deeply
[![npm version](https://badge.fury.io/js/merge-deeply.svg)](https://badge.fury.io/js/merge-deeply)
[![CircleCI](https://circleci.com/gh/riversun/merge-deeply.svg?style=shield)](https://circleci.com/gh/riversun/merge-deeply)
[![codecov](https://codecov.io/gh/riversun/merge-deeply/branch/master/graph/badge.svg)](https://codecov.io/gh/riversun/merge-deeply)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/76e6355ebb27472aaddcbdcc3dcce90c)](https://app.codacy.com/manual/riversun/merge-deeply?utm_source=github.com&utm_medium=referral&utm_content=riversun/merge-deeply&utm_campaign=Badge_Grade_Dashboard)## Allow you to Merge or Clone with prototypes
- You can deep merge (deep copy) the properties of an object
- An object with a prototype declaration can be cloned to include the prototype.
- Objects that have prototype declarations, prototypes and properties that can be deep copied
# Usage
## Install
### Use with NPM
```
npm install merge-deeply
```### Use on browser from CDN
```
```
## Usage
### Cloning an object that has a prototype declaration
The following will execute a copy of the method declared as the prototype and a deep copy of all the properties.
```js
const mergeDeeply = require('merge-deeply');const clonedObject=mergeDeeply({op:'clone',object1:obj1});
```
### Merging 2 objects that has a prototype declaration
The two objects will be merged and a new object will be created, as follows
Of course the prototype is copied.```js
const mergeDeeply = require('merge-deeply');const mergedObject=mergeDeeply({op:'merge',object1:obj1,object2:obj2});
```
### Copy(overwrite) properties into object
The following merges obj1 with obj2's properties
```js
const mergeDeeply = require('merge-deeply');mergeDeeply({op:'overwrite-merge',object1:obj1,object2:obj2});
```
### Merging 2 "Properties only" Objects
If the "concatArray" is true,
elements of the array are added at merge time```js
const mergeDeeply = require('merge-deeply');const a = {
foo: 'myValue',
bar: {
barKey1: 'myValue1',
barKey2: 'myValue2'
},
myArray: [{arrKey1: 'arrValue1'}, {arrKey2: 'arrValue2'}]};
const b = {
foo: 'updatedMyValue',
bar: {
barKey1: 'updatedMyValue1',
barKey2: 'myValue2'
},
some: {someKey: 'someValue'},
myArray: [{arrKey3: 'arrValue3'}, {arrKey4: 'arrValue4'}]};
const result = mergeDeeply({op:'merge',object1:a,object2:b, concatArray: true});
console.log('result=');
console.log(result);```
When you run it you will get the following result.
```js
result=
{ foo: 'updatedMyValue',
bar: { barKey1: 'updatedMyValue1', barKey2: 'myValue2' },
myArray:
[ { arrKey1: 'arrValue1' },
{ arrKey2: 'arrValue2' },
{ arrKey3: 'arrValue3' },
{ arrKey4: 'arrValue4' } ],
some: { someKey: 'someValue' } }```