Code Monkey home page Code Monkey logo

deeplyassign's Introduction

deeplyAssign

A deeply copy the values of all enumerable own properties and symbol properties from one or more source objects to a target object. It will return the target object. Written in TypeScript.

Translate ✏️

install

Install with npm:

$ npm i deeplyassign

Install with yarn:

$ yarn add deeplyassign

Usage

You can use any module sys to use determined by your environment, such as below:

//const deeplyAssign = require("deeplyassign").default
import deeplyAssign from 'deeplyassign';

  const source1 = { 
    info: { 
      author: 'Tommy',
      from: 'China' 
    }, 
    finish: false,
    other: 'follow' 
  }

  const source2 = { 
    info: { 
      author: 'moZLeo', 
      sex: 'male' 
    }, 
    finish: true,
    log: console.log
  }

  console.log(deeplyAssign({}, source1, source2))

  // {
  //   info: { author: 'moZLeo', from: 'China', sex: 'male' },
  //   finish: true,
  //   other: 'follow',
  //   log: console.log
  // }

Behavior

  • This does basically same behavior as Object.assign(), and run in recursive for deep clone.

    • difference: The value type of the attribute of the source object is used when the corresponding attribute value type of target and source is inconsistent. This means that the value of the original target attribute will be discarded or overridden. Such as:

      const target = { prop: { org: 'xxx' } };  // type of target.prop => 'object'
      const source = { prop: [ 0, 1 ] };        // type of source.prop => 'array'
      
      console.log(deeplyAssign(target, source)) // { prop: [ 0, 1 ] }
  • There is no direct reference to the object type and the intermediate variable is not modified(see test).

    • Compatible with one layer of circular reference objects.
  • When a property value of a target is a function, cloning with a reference retains the additional property (so modifying it affects the original object).

  • String and Symbol properties are copied(IE browser automatically ignores this type attribute because it is not compatible with the Symbol type.).

License

MIT © Tommy White

deeplyassign's People

Contributors

lihroff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

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.