Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

deepmerge-plus

bluelovers15.3kMIT3.0.2TypeScript support: included

A library for deep (recursive) merging of Javascript objects

and, clone, copy, deep, extend, merge, or, overwrite, recursive, create-by-yarn-tool, create-by-tsdx

readme

deepmerge-plus

Merge the enumerable attributes of two objects deeply.

npm install deepmerge-plus

Check out the changes from version 1.x to 2.0.0

For the old array element-merging algorithm, see the arrayMerge option below.

example

    var moment = require('moment')

    var monday = moment('2016-09-27T01:08:12.761Z')
    var tuesday = moment('2016-09-28T01:18:12.761Z')

    var target = {
        date: monday
    }
    var source = {
        date: tuesday
    }

    let options = {
        isMergeableObject(value, isMergeableObject) {
            let bool;

            if (bool = moment.isMoment(value)) {
                return false;
            }

            return isMergeableObject(value)
        }
    }

    var expected = {
        date: tuesday
    }
    var actual = merge(target, source, options) // => expected
var x = {
    foo: { bar: 5 },
    array: [{
        does: 'work',
        too: [ 1, 2, 3 ]
    }]
}

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

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

merge(x, y) // => expected

methods

var merge = require('deepmerge')

merge(x, y, [options])

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.

Merging creates a new object, so that neither x or y are be modified.

merge.all(arrayOfObjects, [options])

Merges any number of objects into a single result object.

var x = { foo: { bar: 3 } }
var y = { foo: { baz: 4 } }
var z = { bar: 'yay!' }

var expected = { foo: { bar: 3, baz: 4 }, bar: 'yay!' }

merge.all([x, y, z]) // => expected

options

arrayMerge

The merge will also concatenate arrays and merge array values by default.

However, there are nigh-infinite valid ways to merge arrays, and you may want to supply your own. You can do this by passing an arrayMerge function as an option.

function overwriteMerge(destinationArray, sourceArray, options) {
    return sourceArray
}
merge(
    [1, 2, 3],
    [3, 2, 1],
    { arrayMerge: overwriteMerge }
) // => [3, 2, 1]

To prevent arrays from being merged:

const dontMerge = (destination, source) => source
const output = merge({ coolThing: [1,2,3] }, { coolThing: ['a', 'b', 'c'] }, { arrayMerge: dontMerge })
output // => { coolThing: ['a', 'b', 'c'] }

To use the old (pre-version-2.0.0) array merging algorithm, pass in this function:

const isMergeableObject = require('is-mergeable-object')
const emptyTarget = value => Array.isArray(value) ? [] : {}
const clone = (value, options) => merge(emptyTarget(value), value, options)

function oldArrayMerge(target, source, optionsArgument) {
    const destination = target.slice()

    source.forEach(function(e, i) {
        if (typeof destination[i] === 'undefined') {
            const cloneRequested = !optionsArgument || optionsArgument.clone !== false
            const shouldClone = cloneRequested && isMergeableObject(e)
            destination[i] = shouldClone ? clone(e, optionsArgument) : e
        } else if (isMergeableObject(e)) {
            destination[i] = merge(target[i], e, optionsArgument)
        } else if (target.indexOf(e) === -1) {
            destination.push(e)
        }
    })
    return destination
}

merge(
    [{ a: true }],
    [{ b: true }, 'ah yup'],
    { arrayMerge: oldArrayMerge }
) // => [{ a: true, b: true }, 'ah yup']

clone

Deprecated.

Defaults to true.

If clone is false then child objects will be copied directly instead of being cloned. This was the default behavior before version 2.x.

install

With npm do:

npm install deepmerge

Just want to download the file without using any package managers/bundlers? Download the UMD version from unpkg.com.

test

With npm do:

npm test

license

MIT

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

3.0.1 (2023-09-25)

BREAKING CHANGES

  • update code

🔖 Miscellaneous

2.0.1 "🔖 Miscellaneous" (2017-11-01)

🔖 Miscellaneous

  • 2.0.1 (3ab89f2)
  • Adding 2.0.1 to changelog (40709a1)
  • Fixing insignificant whitespace (6b57cc9)
  • Fix the old array merge algorithm to not try to clone when it shouldn't (e5ad65f)
  • Fix the oldArrayMerge example (a6a41cf), closes #83
  • Add an explicit note about array merging change (a69a151)
  • Adding a "check out 2.0.0 changes" link to readme (d140772)

2.0.0 "🔖 Miscellaneous" (2017-10-09)

🔖 Miscellaneous

  • 2.0.0 (db34d2f)
  • Simplify the arrayMerge example to involve less distracting assertions (d87eb96)
  • Update changelog for 2.0.0 (494d019)
  • Update the byte counts thanks to my leet golf skillz (ea8524b)
  • Simplify "was clone specified" logic (4fdce6a)
  • Removing outdated line about how child objects weren't cloned by default (48dc381)
  • Update the byte sizes (55c1aa7)
  • Remove arrow function (635e795)
  • Update the readme for version 2 (93688b2)
  • Handle an empty array too, why not (970a26e), closes #71
  • Allow calling merge.all with only one element (bdc290b), closes #71
  • Test the new readme filename (7559f45)
  • README.markdown -> readme.md (e021528)
  • Switch the default array to concatenation instead of the complicated thing (9005f24)
  • Drop the CJS dist (1159ffb)
  • Default to cloning (307b908)

1.5.2 "🔖 Miscellaneous" (2017-09-21)

♻️ Chores

  • update rollup's config options for v0.49.3 (f779b7a)
  • update rollup to v0.49.3 & plugin-commonjs to v8.2.1 (5710262)

🔖 Miscellaneous

1.5.1 "🔖 Miscellaneous" (2017-08-15)

🔖 Miscellaneous

1.5.0 "🔖 Miscellaneous" (2017-07-06)

🔖 Miscellaneous

1.4.4 "🔖 Miscellaneous" (2017-06-19)

🔖 Miscellaneous

  • 1.4.4 (9cf7945)
  • Updating "main" in bower.json (8d19b56), closes #63
  • Add node 8 to travis testing (8c7c4d5)
  • const -> var in the rollup config file (a833a18)

1.4.3 "🔖 Miscellaneous" (2017-06-14)

🔖 Miscellaneous

  • 1.4.3 (9ec020d)
  • Updating changelog for 1.4.3 (a488e14)
  • Inline is-mergeable-object in the CJS build, too (0b34e6e)

1.4.2 "🔖 Miscellaneous" (2017-06-14)

🔖 Miscellaneous

  • 1.4.2 (1e016af)
  • Updating changelog with 1.4.0 through 1.4.2 (399318c)
  • Bump is-mergeable-object dependency (5906c76)

1.4.1 "🔖 Miscellaneous" (2017-06-13)

🔖 Miscellaneous

  • 1.4.1 (ff54d84)
  • Updating the unpkg link to go directly to the umd build (acc45be)

1.4.0 "🔖 Miscellaneous" (2017-06-13)

🔖 Miscellaneous

1.3.2 "🔖 Miscellaneous" (2017-01-27)

🔖 Miscellaneous

  • 1.3.2 (bac0e9f)
  • Update with releases 1.3.0 - 1.3.2 (e651e74)
  • MDN-style formatting on JavaScript blocks (60efb50)
  • add info about source code file size (c49a5c9)

1.3.1 "🔖 Miscellaneous" (2016-12-03)

🔖 Miscellaneous

  • 1.3.1 (bfc85d8)
  • Make jsmd check the arrayMerge arguements (f6a9584)
  • Improve options section of the readme (3dba5cc)

1.3.0 "🔖 Miscellaneous" (2016-11-12)

🔖 Miscellaneous

  • 1.3.0 (ca9c3d9)
  • Adding documentation for merge.all (3b6a17c)
  • fix incostintent indentation – move to 4 spaces (e3f4b23)
  • add deep merge implementation for arrays of values (2195ebf)
  • fix test for clone check (52f108f)
  • add tests for checking functionality of merge.all (5792c52)
  • add tests for invoking merge.all (6082ebf)
  • Adding a maintenance pull request I forgot to 1.2.0 (eae827d)

1.2.0 "🔖 Miscellaneous" (2016-10-14)

🔖 Miscellaneous

1.1.1 "🔖 Miscellaneous" (2016-10-12)

🔖 Miscellaneous

1.1.0 "🔖 Miscellaneous" (2016-09-29)

🔖 Miscellaneous

1.0.3 "🔖 Miscellaneous" (2016-09-29)

🔖 Miscellaneous

  • 1.0.3 (9e8e757)
  • Updating changelog for 1.0.3 (d3ead9e)
  • Update to the current version of tap (6eee0cb)
  • package.json: add keywords, update git references (bc3898e)
  • Re-adding bower.json without the version (f9a149d), closes #38

1.0.2 "🔖 Miscellaneous" (2016-09-27)

🔖 Miscellaneous

1.0.1 "🔖 Miscellaneous" (2016-09-27)

🔖 Miscellaneous

  • 1.0.1 (3c6738f)
  • Updating changelog for 1.0.1 (e64d3bc)
  • Adding .travis.yml for CI testing (137b37b)
  • Fixes some special-object-in-array cases (ef1c6ba), closes #18 #23 #31
  • Adding changelog, bumping version to 1.0.0 (0989bc4), closes #15
  • Dropping bower (0af299a)
  • Merge date values like primitives (a2f722b), closes #31
  • Properly handle regular expression values like primitives (bac64a6), closes #23
  • Add license to package.json (ba270a8)
  • Resolve an issue when null is in an array (689504d)
  • Resolve an issue when null is in an array (228eceb)
  • bump version again. last tag failed (6f64cf6)
  • bumped version number (d316e4e)
  • fix for issue #12 (49cc4ed), closes #12
  • Made license legit (0a477b3)
  • Add bower.json for browser install. (7eb740f)
  • added version (3f4e488)
  • added license, bumped version (caf5372)
  • bumped version number (0fa8f3d)
  • merging of arrays of nested objects (98e8b19)
  • fixed bug with merging nested objects (e0e2ff6)
  • Cleaning (a143b45)
  • Fix example (e01a5b2)
  • Fix output (48f3773)
  • Bad bad english (e3ac31d)
  • Bump version 0.2.4 (bf85c33)
  • Add some doc to array merging, add a more complex example (5e682f0)
  • Set back indent to 4 spaces as @substack did, remove trailing ;, , .. (d236097)
  • Merging array of objects (9180a6d)
  • Bump version 0.2.3 (e9633aa)
  • Fix array merge, add tests (8bfdc39)
  • bump version (1eb45c8)
  • Add array merge (b9fa2d0)
  • removed unnecessary Makefile (787e863)
  • incremented version # (0708efd)
  • removed spec directory. was not correclty moved in pull request (f914236)
  • documentation and an example (5970d95)
  • more passing immutability checks (d0fce9a)
  • immutable interface tests all pass (a4583b1)
  • failing test because merge should be immutable (0e0bb94)
  • no more coffee (72ab7b2)
  • using typeof and get rid of cakefile noise (208997b)
  • replaced Cakefile with Makefile. replaced jasmine-node with mocha and should (5c95f13)
  • get rid of broken jasmine in favor of tap (dd54f29)
  • updated package info (2ede3f0)
  • finished tests (8916dbc)
  • tests are passing. need 1 more test case (ae76744)
  • implemented merge. created placeholder test (589de32)
  • initial commit (828ad22)

2.0.1

  • documentation: fix the old array merge algorithm in the readme. #84

2.0.0

  • breaking: the array merge algorithm has changed from a complicated thing to target.concat(source).map(element => cloneUnlessOtherwiseSpecified(element, optionsArgument))
  • breaking: The clone option now defaults to true
  • feature: merge.all now accepts an array of any size, even 0 or 1 elements

See pull request 77.

1.5.2

  • fix: no longer attempts to merge React elements #76

1.5.1

  • bower support: officially dropping bower support. If you use bower, please depend on the unpkg distribution. See #63

1.5.0

  • bug fix: merging objects into arrays was allowed, and doesn't make any sense. #65 published as a feature release instead of a patch because it is a decent behavior change.

1.4.4

  • bower support: updated main in bower.json

1.4.3

  • bower support: inline is-mergeable-object in a new CommonJS build, so that people using both bower and CommonJS can bundle the library 0b34e6

1.4.2

  • performance: bump is-mergeable-object dependency version for a slight performance improvement 5906c7

1.4.1

  • documentation: fix unpkg link acc45b

1.4.0

  • api: instead of only exporting a UMD module, expose a UMD module with pkg.main, a CJS module with pkg.browser, and an ES module with pkg.module #62

1.3.2

  • documentation: note the minified/gzipped file sizes 56
  • documentation: make data structures more readable in merge example: pull request 57

1.3.1

  • documentation: clarify and test some array merging documentation: pull request 51

1.3.0

  • feature: merge.all, a merge function that merges any number of objects: pull request 50

1.2.0

  • fix: an error that would be thrown when an array would be merged onto a truthy non-array value: pull request 46
  • feature: the ability to clone: Issue 28, pull requests 44 and 48
  • maintenance: added tests + travis to .npmignore: pull request 47

1.1.1

  • fix an issue where an error was thrown when merging an array onto a non-array: Pull request 46

1.1.0

  • allow consumers to specify their own array merging algorithm: Pull request 37

1.0.3

  • adding bower.json back: Issue 38
  • updating keywords and Github links in package.json bc3898e

1.0.2

  • Updating the readme: dropping bower, testing that the example works: 7102fc

1.0.1

  • null, dates, and regular expressions are now properly merged in arrays: Issue 18, plus commit: ef1c6b

1.0.0

  • Should only be a patch change, because this module is READY. Issue 15
  • Regular expressions are now treated like primitive values when merging: Issue 30
  • Dates are now treated like primitives when merging: Issue 31