Giter VIP home page Giter VIP logo

blend.js's Introduction

NPM

blend.js

Micro library for performing shallow/deep copying of objects with support for array merging + dedup

Extend for Node, CommonJS, Browserify, AMD and Browser

blend is NOT a direct port of jQuery's extend() method. This lib follows the same approach that jQuery.extend does except it will handle the deep copying of arrays differently. jQuery will overwrite the index of an array during a deep copy where as blend will merge the contents of the arrays, and dedup if defined.

NPM Installation

This package is available on npm as: blend.js

npm install blend.js

Bower Installation

bower install blend.js

// include the file inside your app
<script type="text/javascript" src="/bower_components/blend/lib/blend.min.js"></script>

Usage

Merge the contents of two or more objects into the target object.

Syntax: blend ( [deep], [dedup], target, object1, [objectN] )

Arguments

  • deep Boolean If true, the merge becomes recursive (optional)

  • dedup Boolean If true, arrays will be dedupped during deep copy (optional)

  • target Object The object receiving the new properties

  • objectN Object (Optional) One or more additional objects to merge with the first

Example:

Shallow copy:

var blend = require('blend'), // window.blend for browsers (`blend` is global)
    target = {
      test: 'me'
    },
    object1 = {
      hello: {
        name: 'world',
        ids: [1,2,3,4]
      }
    },
    object2 = {
      hello: {
        first: 'kieran',
        ids: [4,5,6,7]
      }
    };

blend(target, object1, object2);

// output
{
  test: 'me',
  hello: {
    first: 'kieran',
    ids: [4,5,6,7]
  }
}

Deep copy:

blend(true, target, object1, object2);

// output
{
  test: 'me',
  hello: {
    first: 'kieran',
    name: 'world',
    ids: [1,2,3,4,4,5,6,7]
  }
}

Deep copy + dedup:

blend(true, true, target, object1, object2);

// output
{
  test: 'me',
  hello: {
    first: 'kieran',
    name: 'world',
    ids: [1,2,3,4,5,6,7]
  }
}

Acknowledgements

Credit to jQuery for the foundation of this utility function.

blend.js's People

Contributors

dysfunc avatar

Stargazers

 avatar Yulia avatar

Forkers

yuliasavinkova

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.