deep-merge

Deep merge objects with custom merging logic

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
deep-merge
1711.0.08 years ago10 years agoMinified + gzip package size for deep-merge in KB

Readme

deep-merge
!build status12 !dependency status34
!browser support56
Deep merge objects with custom merging logic

Deprecated

Use deep-extend instead of this module.

Example

When you create a merge "strategy" you can configure how two values should merge when they have the same key. For example you may want them to be combined into a single array.
var DeepMerge = require("deep-merge")
var assert = require("assert")

var merge = DeepMerge(function mergeStrategy(target, source, key) {
    return [].concat(target, source)
})

var res = merge({
    foo: ["hello"]
}, {
    foo: "goodbye"
})

assert.deepEqual(res, { foo: ["hello", "goodbye"] })

By default objects will recursely call your mergeStrategy on your the values for a key and construct a new object with the key and the value returned from your merge

Merging multiple objects

var DeepMerge = require("deep-merge/multiple")
var merge = DeepMerge(function (a, b) {
    return b
})

var obj = merge([
    { a: "a" },
    { b: "b" },
    { c: { d: "d" } },
    { c: { e: "e" } }
])

assert.deepEqual(obj.c, { d: "d", e: "e" })

Installation

npm install deep-merge

Contributors

- Raynos

MIT Licenced

1: https://secure.travis-ci.org/Raynos/deep-merge.png 2: http://travis-ci.org/Raynos/deep-merge 3: http://david-dm.org/Raynos/deep-merge/status.png 4: http://david-dm.org/Raynos/deep-merge 5: http://ci.testling.com/Raynos/deep-merge.png 6: http://ci.testling.com/Raynos/deep-merge