Coder Social home page Coder Social logo

web-dev-collaborative / nodejs-clone-extend Goto Github PK

View Code? Open in Web Editor NEW

This project forked from shimondoodkin/nodejs-clone-extend

0.0 1.0 0.0 26 KB

Clone & extend / mixin for node js. It is easy to use and practical. supports circular references

JavaScript 100.00%

nodejs-clone-extend's Introduction

Installation

npm:

npm install cloneextend

manual:

cd node_modules
git clone git://github.com/shimondoodkin/nodejs-clone-extend.git
mv nodejs-clone-extend cloneextend

http://github.com/shimondoodkin/nodejs-clone-extend

Node.js Clone Extend

  • It is what you were searching for ...
  • It allows you to merge and clone javascript objects easily.
  • It supports circular references
  • and Dates.

The story behind this module is that I was trying to use underscore.js library for cloning and extending objects but it did not work as expected, nor any other workaround or solution, so I wrote clone and extend functions from ground up for everyone's benefit.

basic functionality:

var ce = require('cloneextend');  
var o1={a:'a',d:new Date(),n:1,ar:[1,2,3]};
var o2=ce.clone(o1); //now o2 will hold a copy of o1
console.log(o2);

cool tricks: DRY (don't repeat yourself):

It allows you to do simple object inheritance.

var _ = require('cloneextend');
  
var basic_col={type:'string',size:10,default_value:''};
var basic_model={ name:'somename', cols:{},
                  init:function(){this.do_something();},
                  do_something:function() { console.log("something"); }
                };

var cars=_.cloneextend(basic_model,
{
 name:'cars',
 cols:
 {
  color:_.clone(basic_col),
  speed:_.cloneextend(basic_col,{type:'number'})
 }
}
);

var red_cars=_.cloneextend(cars,{
 do_something:function(){console.log("my color is "+this.cols.color.default_value);},
 cols:{color:{default_value:'red'}}
});

var blue_cars=_.cloneextend(red_cars,{
 cols:{color:{default_value:'blue'}}
});

cars.init();    
red_cars.init();    
blue_cars.init();    

output: something my color is red my color is blue

include it!

var _ = require('cloneextend');
  
var obj1={apples:10};
var obj2={bananas:20};
_.extend(obj1,obj2); // merge called extend

// obj1 = {apples:10,bananas:20}

replace

// function replace(a, b)
//  for (key in b) a[key] = b[key];

var _ = require('cloneextend');  
var obj1={apples:10};
var obj2={apples:20,bananas:20};
_.replace(obj1,obj2);

// obj1 = {apples:20,bananas:20}

add - adds if not exists

this function does not replace elements if they are exists

// function add(a, b)
//  for (key in b)
//   if(typeof a[key] === 'undefined' || a[key]===null)
//     a[key] = b[key];

var _ = require('cloneextend');  
var obj1={apples:10};
var obj2={apples:20,bananas:20};
_.add(obj1,obj2);

// obj1 = {apples:10,bananas:20}

extend

this function merges second object in to the first and returns the first object by reference (objects are always reference) it replaces elements if they are exists

// function extend(a, b)

var _ = require('cloneextend');  
var basket1={ apples:[{taste:'sour'},{taste:'sweet'}], fruit:'appricot' };
var basket2={ fruit:'orange', vegetable:'cucumber'};
_.extend(basket2,basket1);

// basket2 = { apples:[{taste:'sour'},{taste:'sweet'}], fruit:'appricot' , vegetable:'cucumber' }
// apples    // added
// fruit     // replaced
// vegetable // stayed

extend of custom objects

for now the support is partial only for extend and extenduptolevel functions. extendding plain objects containing custom objects

function MyObject()
{
	this.someproperty={};
}

//write a cloning strategy by obj.constructor.name (function name that used to create the object)
ce.howtoclone.MyObject= function(obj) {
	var cloned=new obj.constructor();
	//cloned.someproperty=obj.someproperty;
	ce.extend(cloned,obj);
	return cloned;
}

var a={obj:new MyObject()};
	a.obj.someproperty={a:1,b:2,c:{d:3}};
var b={};
ce.extend(b,a)

extenduptolevel - extend up to level

this function clones elements and creates new parent objects when they are missing. after the level is reached it starts to repace objects insted of creating new parents and refilling them.

it allows you to clone only the 1st level and let the second level to be references.

Rememberence trick: If you want to modify values after 2nd dot write 2 in the level

// function extenduptolevel(a, b, levels)

var _ = require('cloneextend');  
var a_shared_car={color:'silver',windows:'manual'};
var obj1={ has:{ car:a_shared_car    } };
var obj2={ has:{ laptop : 'hp_laptop'  } }
_.extenduptolevel(obj2,obj1,2);

// obj2 = { has:{ laptop:'hp_laptop', car:a_shared_car } }
// obj1 = { has:{ car:a_shared_car } }
// So if i change she shared car it should shange the other two because they are references:
a_shared_car.windows='electric';

// obj2 = { has:{ laptop:'hp_laptop', car:{color:'silver',windows:'electric'} } }
// obj1 = { has:{ car:{color:'silver',windows:'electric'} } }    

clone

// function clone(obj)
//   extend({}, obj);

var _ = require('cloneextend');  
var obj1={fruit:'apple'};
var newobj1=_.clone(obj1);

// obj1    = {fruit:'apple'}
// newobj1 = {fruit:'apple'}

cloneextend - clone and then extend

// function cloneextend(obj,exteddata)
//   extend(clone(obj),exteddata);

var _ = require('cloneextend');  
var salad_basic={fruit:'apple'};
var new_salad_v1=_.cloneextend(salad_basic,{syrop:'maple syrop'});
var new_salad_v2=_.cloneextend(salad_basic,{syrop:'chocolate syrop'});

// new_salad_v1 = {fruit:'apple',syrop:'maple syrop'}
// new_salad_v2 = {fruit:'apple',syrop:'chocolate syrop'}    

cloneuptolevel - clone up to level

// // clone only numlevels levels other levels leave references
// function cloneuptolevel(obj,level)
//   extenduptolevel({}, obj, levels)

var _ = require('cloneextend');  
var a_shared_car={color:'silver',windows:'manual'};
var obj1={ has:{ car:a_shared_car    } };
var obj2=_.cloneuptolevel(obj2,2);

// obj1 = { has:{ car:a_shared_car } }
// obj2 = { has:{ car:a_shared_car } }

// Again if i change she shared car it should shange the other two because they are references:
a_shared_car.windows='electric';

// obj1 = { has:{ car:{color:'silver',windows:'electric'} } }    
// obj2 = { has:{ laptop:'hp_laptop', car:{color:'silver',windows:'electric'} } }

tests

for tests need to instal mocha:

npm install -g mocha

then to run a test

mocha test.js

Licence

Generaly, 2 close MIT/BSD - Copyright: Shimon Doodkin [email protected] (http://doodkin.com)

But you can use it as you like, I don't care , Enjoy.

nodejs-clone-extend's People

Contributors

shimondoodkin avatar whitfin avatar michaelpetrov avatar

Watchers

James Cloos 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.