GithubHelp home page GithubHelp logo

riversun / merge-deeply Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 1.0 1.59 MB

Clone object with prototype ,and deeply merge (deep copy) properties of two JS objects.

License: MIT License

JavaScript 89.18% HTML 10.82%
deep-merge deep-copy js-object json clone prototype

merge-deeply's Introduction

merge-deeply

npm version CircleCI codecov Codacy Badge

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

<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/merge-deeply.js">

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.

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.

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

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

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.

result=
{ foo: 'updatedMyValue',
  bar: { barKey1: 'updatedMyValue1', barKey2: 'myValue2' },
  myArray: 
   [ { arrKey1: 'arrValue1' },
     { arrKey2: 'arrValue2' },
     { arrKey3: 'arrValue3' },
     { arrKey4: 'arrValue4' } ],
  some: { someKey: 'someValue' } }

merge-deeply's People

Contributors

codacy-badger avatar dependabot[bot] avatar riversun avatar

Watchers

 avatar  avatar

Forkers

dmitryyacenko

merge-deeply's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.