UNPKG

3.03 kBMarkdownView Raw
1deepmerge
2=========
3
4> ~554B gzipped, ~1.10KiB minified
5
6Merge the enumerable attributes of two objects deeply.
7
8example
9=======
10
11<!--js
12var merge = require('./')
13-->
14
15```js
16var x = {
17 foo: { bar: 3 },
18 array: [{
19 does: 'work',
20 too: [ 1, 2, 3 ]
21 }]
22}
23
24var y = {
25 foo: { baz: 4 },
26 quux: 5,
27 array: [{
28 does: 'work',
29 too: [ 4, 5, 6 ]
30 }, {
31 really: 'yes'
32 }]
33}
34
35var expected = {
36 foo: {
37 bar: 3,
38 baz: 4
39 },
40 array: [{
41 does: 'work',
42 too: [ 1, 2, 3 ]
43 }, {
44 does: 'work',
45 too: [ 4, 5, 6 ]
46 }, {
47 really: 'yes'
48 }],
49 quux: 5
50}
51
52merge(x, y) // => expected
53```
54
55methods
56=======
57
58```
59var merge = require('deepmerge')
60```
61
62merge(x, y, [options])
63-----------
64
65Merge two objects `x` and `y` deeply, returning a new merged object with the
66elements from both `x` and `y`.
67
68If an element at the same key is present for both `x` and `y`, the value from
69`y` will appear in the result.
70
71Merging creates a new object, so that neither `x` or `y` are be modified.
72
73merge.all(arrayOfObjects, [options])
74-----------
75
76Merges any number of objects into a single result object.
77
78```js
79var x = { foo: { bar: 3 } }
80var y = { foo: { baz: 4 } }
81var z = { bar: 'yay!' }
82
83var expected = { foo: { bar: 3, baz: 4 }, bar: 'yay!' }
84
85merge.all([x, y, z]) // => expected
86```
87
88### options
89
90#### arrayMerge
91
92The merge will also concatenate arrays and merge array values by default.
93
94However, 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.
95
96```js
97function overwriteMerge(destinationArray, sourceArray, options) {
98 return sourceArray
99}
100merge(
101 [1, 2, 3],
102 [3, 2, 1],
103 { arrayMerge: overwriteMerge }
104) // => [3, 2, 1]
105```
106
107To prevent arrays from being merged:
108
109```js
110const dontMerge = (destination, source) => source
111const output = merge({ coolThing: [1,2,3] }, { coolThing: ['a', 'b', 'c'] }, { arrayMerge: dontMerge })
112output // => { coolThing: ['a', 'b', 'c'] }
113```
114
115To use the old (pre-version-2.0.0) array merging algorithm, pass in this function:
116
117```js
118function oldArrayMerge(target, source, optionsArgument) {
119 var destination = target.slice()
120 source.forEach(function(e, i) {
121 if (typeof destination[i] === 'undefined') {
122 destination[i] = cloneIfNecessary(e, optionsArgument)
123 } else if (isMergeableObject(e)) {
124 destination[i] = deepmerge(target[i], e, optionsArgument)
125 } else if (target.indexOf(e) === -1) {
126 destination.push(cloneIfNecessary(e, optionsArgument))
127 }
128 })
129 return destination
130}
131```
132
133#### clone
134
135*Deprecated.*
136
137Defaults to `true`.
138
139If `clone` is `false` then child objects will be copied directly instead of being cloned. This was the default behavior before version 2.x.
140
141install
142=======
143
144With [npm](http://npmjs.org) do:
145
146```sh
147npm install deepmerge
148```
149
150Just want to download the file without using any package managers/bundlers? [Download the UMD version from unpkg.com](https://unpkg.com/deepmerge/dist/umd.js).
151
152test
153====
154
155With [npm](http://npmjs.org) do:
156
157```sh
158npm test
159```
160
161license
162=======
163
164MIT