1 | deepmerge
|
2 | =========
|
3 |
|
4 | > ~554B gzipped, ~1.10KiB minified
|
5 |
|
6 | Merge the enumerable attributes of two objects deeply.
|
7 |
|
8 | example
|
9 | =======
|
10 |
|
11 |
|
12 | var merge = require('./')
|
13 | -->
|
14 |
|
15 | ```js
|
16 | var x = {
|
17 | foo: { bar: 3 },
|
18 | array: [{
|
19 | does: 'work',
|
20 | too: [ 1, 2, 3 ]
|
21 | }]
|
22 | }
|
23 |
|
24 | var 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 |
|
35 | var 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 |
|
52 | merge(x, y) // => expected
|
53 | ```
|
54 |
|
55 | methods
|
56 | =======
|
57 |
|
58 | ```
|
59 | var merge = require('deepmerge')
|
60 | ```
|
61 |
|
62 | merge(x, y, [options])
|
63 | -----------
|
64 |
|
65 | Merge two objects `x` and `y` deeply, returning a new merged object with the
|
66 | elements from both `x` and `y`.
|
67 |
|
68 | If an element at the same key is present for both `x` and `y`, the value from
|
69 | `y` will appear in the result.
|
70 |
|
71 | Merging creates a new object, so that neither `x` or `y` are be modified.
|
72 |
|
73 | merge.all(arrayOfObjects, [options])
|
74 | -----------
|
75 |
|
76 | Merges any number of objects into a single result object.
|
77 |
|
78 | ```js
|
79 | var x = { foo: { bar: 3 } }
|
80 | var y = { foo: { baz: 4 } }
|
81 | var z = { bar: 'yay!' }
|
82 |
|
83 | var expected = { foo: { bar: 3, baz: 4 }, bar: 'yay!' }
|
84 |
|
85 | merge.all([x, y, z]) // => expected
|
86 | ```
|
87 |
|
88 | ### options
|
89 |
|
90 | #### arrayMerge
|
91 |
|
92 | The merge will also concatenate arrays and merge array values by default.
|
93 |
|
94 | 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.
|
95 |
|
96 | ```js
|
97 | function overwriteMerge(destinationArray, sourceArray, options) {
|
98 | return sourceArray
|
99 | }
|
100 | merge(
|
101 | [1, 2, 3],
|
102 | [3, 2, 1],
|
103 | { arrayMerge: overwriteMerge }
|
104 | ) // => [3, 2, 1]
|
105 | ```
|
106 |
|
107 | To prevent arrays from being merged:
|
108 |
|
109 | ```js
|
110 | const dontMerge = (destination, source) => source
|
111 | const output = merge({ coolThing: [1,2,3] }, { coolThing: ['a', 'b', 'c'] }, { arrayMerge: dontMerge })
|
112 | output // => { coolThing: ['a', 'b', 'c'] }
|
113 | ```
|
114 |
|
115 | To use the old (pre-version-2.0.0) array merging algorithm, pass in this function:
|
116 |
|
117 | ```js
|
118 | function 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 |
|
137 | Defaults to `true`.
|
138 |
|
139 | If `clone` is `false` then child objects will be copied directly instead of being cloned. This was the default behavior before version 2.x.
|
140 |
|
141 | install
|
142 | =======
|
143 |
|
144 | With [npm](http://npmjs.org) do:
|
145 |
|
146 | ```sh
|
147 | npm install deepmerge
|
148 | ```
|
149 |
|
150 | Just 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 |
|
152 | test
|
153 | ====
|
154 |
|
155 | With [npm](http://npmjs.org) do:
|
156 |
|
157 | ```sh
|
158 | npm test
|
159 | ```
|
160 |
|
161 | license
|
162 | =======
|
163 |
|
164 | MIT
|