deeply

Deeply merges properties of the provided objects, returns untangled copy (clone)

Deeply

Javascript (Node + Browser) library that deeply merges properties of the provided objects, returns untangled copy (clone).

$ npm install deeply
$ ender add deeply --use=your_ender_file

– Deeply merges two or more objects.

Node:

var merge = require('deeply');
 
merge({a: {a1: 1}}, {a: {a2: 2}}); // -> {a: {a1: 1, a2: 2}} 

Ender:

$.merge({a: {a1: 1}}, {a: {a2: 2}}); // -> {a: {a1: 1, a2: 2}} 

– As degenerated case of merging one object on itself, it's possible to use deeply as deep clone function.

Node:

var merge = require('deeply')
  , clone = merge
  ;
 
var x = {a: {b: {c: 1}}};
var y = clone(x);
 
y.a.b.= 2;
 
console.log(x.a.b.c); // -> 1 

Ender:

var clone = $.merge;
var x = {a: {b: {c: 1}}};
var y = clone(x);
 
y.a.b.= 2;
 
console.log(x.a.b.c); // -> 1 

– By default array treated as primitive values and being replaced upon conflict, for more meaningful array merge strategy, provide custom reduce function as last argument.

Node:

var merge = require('deeply');
 
// default behavior 
 
merge({ a: { b: [0, 2, 4] }}, { a: {b: [1, 3, 5] }}); // -> { a: { b: [1, 3, 5] }} 
 
// custom merge function 
 
function customMerge(ab)
{
  return (a||[]).concat(b);
}
 
merge({ a: { b: [0, 2, 4] }}, { a: {b: [1, 3, 5] }}, customMerge); // -> { a: { b: [0, 2, 4, 1, 3, 5] }} 

Ender:

 
// default behavior 
 
$.merge({ a: { b: [0, 2, 4] }}, { a: {b: [1, 3, 5] }}); // -> { a: { b: [1, 3, 5] }} 
 
// custom merge function 
 
function customMerge(ab)
{
  return (a||[]).concat(b);
}
 
$.merge({ a: { b: [0, 2, 4] }}, { a: {b: [1, 3, 5] }}, customMerge); // -> { a: { b: [0, 2, 4, 1, 3, 5] }} 
 

More examples can be found in test/index.js.

Deeply is licensed under the MIT license.