warlock-deepmerge

0.3.0 • Public • Published

deepmerge

Merge the enumerable attributes of two objects deeply.

This is a forked version of nrf110/deepmerge to allow for the Warlock edge case of allowing overwrites in place of merges on particular object keys.

example

var util = require('util')
var merge = require('deepmerge')

var x = { foo: { bar: 3 },
  array: [ { does: 'work', too: [ 1, 2, 3 ] } ] }
var y = { foo: { baz: 4 },
  quux: 5,
  array: [ { does: 'work', too: [ 4, 5, 6 ] }, { really: 'yes' } ] }

console.log(util.inspect(merge(x, y), false, null))

output:

{ foo: { bar: 3, baz: 4 },
  array: [ { does: 'work', too: [ 1, 2, 3, 4, 5, 6 ] }, { really: 'yes' } ],
  quux: 5 }

methods

var merge = require('deepmerge')

merge(x, y, allowOverwrite)

Merge two objects x and y deeply, returning a new merged object with the elements from both x and y.

If an element at the same key is present for both x and y, the value from y will appear in the result.

The merge is immutable, so neither x nor y will be modified.

The merge will also merge arrays and array values.

If y has objects whose key have a prefix of ^, the merge will overwrite the value for the same key in x, rather than merging the values. For example:

var y = [
    { "^key1": { "subkey": "two" }},
    { "key2": { "subkey": "three" }}
];

var x = [
    { "key1": { "subkey1": "one", "subkey2": "two" }}
]

running merge(x, y) will actually produce the following, completely overwriting x.key1

var merged = [
    { "key1": { "subkey": "two" }},
    { "key2": { "subkey": "three" }}
]

You must explicitly pass false as the 3rd parameter to the merge method to prevent such overwrites.

install

With npm do:

npm install warlock-deepmerge

test

With npm do:

npm test

Readme

Keywords

none

Package Sidebar

Install

npm i warlock-deepmerge

Weekly Downloads

15

Version

0.3.0

License

none

Last publish

Collaborators

  • joshdmiller