You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
5.3 KiB

4 years ago
  1. # deepmerge
  2. Merges the enumerable attributes of two or more objects deeply.
  3. > UMD bundle is 567B minified+gzipped
  4. ### Migration from 1.x to 2.0.0
  5. [***Check out the changes from version 1.x to 2.0.0***](https://github.com/KyleAMathews/deepmerge/blob/master/changelog.md#200)
  6. For the legacy array element-merging algorithm, see [the `arrayMerge` option below](#arraymerge).
  7. ### Webpack bug
  8. If you have `require('deepmerge')` (as opposed to `import merge from 'deepmerge'`) anywhere in your codebase, Webpack 3 and 4 have a bug that [breaks bundling](https://github.com/webpack/webpack/issues/6584).
  9. If you see `Error: merge is not a function`, add this alias to your Webpack config:
  10. ```
  11. alias: {
  12. deepmerge$: path.resolve(__dirname, 'node_modules/deepmerge/dist/umd.js'),
  13. }
  14. ```
  15. ## Getting Started
  16. ### Example Usage
  17. <!--js
  18. var merge = require('./')
  19. -->
  20. ```js
  21. var x = {
  22. foo: { bar: 3 },
  23. array: [{
  24. does: 'work',
  25. too: [ 1, 2, 3 ]
  26. }]
  27. }
  28. var y = {
  29. foo: { baz: 4 },
  30. quux: 5,
  31. array: [{
  32. does: 'work',
  33. too: [ 4, 5, 6 ]
  34. }, {
  35. really: 'yes'
  36. }]
  37. }
  38. var expected = {
  39. foo: {
  40. bar: 3,
  41. baz: 4
  42. },
  43. array: [{
  44. does: 'work',
  45. too: [ 1, 2, 3 ]
  46. }, {
  47. does: 'work',
  48. too: [ 4, 5, 6 ]
  49. }, {
  50. really: 'yes'
  51. }],
  52. quux: 5
  53. }
  54. merge(x, y) // => expected
  55. ```
  56. ### Installation
  57. With [npm](http://npmjs.org) do:
  58. ```sh
  59. npm install deepmerge
  60. ```
  61. deepmerge can be used directly in the browser without the use of package managers/bundlers as well: [UMD version from unpkg.com](https://unpkg.com/deepmerge/dist/umd.js).
  62. ### Includes
  63. CommonJS:
  64. ```
  65. var merge = require('deepmerge')
  66. ```
  67. ES Modules:
  68. ```
  69. import merge from 'deepmerge'
  70. ```
  71. # API
  72. ## `merge(x, y, [options])`
  73. Merge two objects `x` and `y` deeply, returning a new merged object with the
  74. elements from both `x` and `y`.
  75. If an element at the same key is present for both `x` and `y`, the value from
  76. `y` will appear in the result.
  77. Merging creates a new object, so that neither `x` or `y` is modified.
  78. **Note:** By default, arrays are merged by concatenating them.
  79. ## `merge.all(arrayOfObjects, [options])`
  80. Merges any number of objects into a single result object.
  81. ```js
  82. var x = { foo: { bar: 3 } }
  83. var y = { foo: { baz: 4 } }
  84. var z = { bar: 'yay!' }
  85. var expected = { foo: { bar: 3, baz: 4 }, bar: 'yay!' }
  86. merge.all([x, y, z]) // => expected
  87. ```
  88. ## Options
  89. ### `arrayMerge`
  90. There are multiple ways to merge two arrays, below are a few examples but you can also create your own custom function.
  91. #### Overwrite Array
  92. Overwrites the existing array values completely rather than concatenating them
  93. ```js
  94. const overwriteMerge = (destinationArray, sourceArray, options) => sourceArray
  95. merge(
  96. [1, 2, 3],
  97. [3, 2, 1],
  98. { arrayMerge: overwriteMerge }
  99. ) // => [3, 2, 1]
  100. ```
  101. #### Combine Array
  102. Combine arrays, such as overwriting existing defaults while also adding/keeping values that are different names
  103. To use the legacy (pre-version-2.0.0) array merging algorithm, use the following:
  104. ```js
  105. const emptyTarget = value => Array.isArray(value) ? [] : {}
  106. const clone = (value, options) => merge(emptyTarget(value), value, options)
  107. function combineMerge(target, source, options) {
  108. const destination = target.slice()
  109. source.forEach(function(e, i) {
  110. if (typeof destination[i] === 'undefined') {
  111. const cloneRequested = options.clone !== false
  112. const shouldClone = cloneRequested && options.isMergeableObject(e)
  113. destination[i] = shouldClone ? clone(e, options) : e
  114. } else if (options.isMergeableObject(e)) {
  115. destination[i] = merge(target[i], e, options)
  116. } else if (target.indexOf(e) === -1) {
  117. destination.push(e)
  118. }
  119. })
  120. return destination
  121. }
  122. merge(
  123. [{ a: true }],
  124. [{ b: true }, 'ah yup'],
  125. { arrayMerge: combineMerge }
  126. ) // => [{ a: true, b: true }, 'ah yup']
  127. ```
  128. ### `isMergeableObject`
  129. By default, deepmerge clones every property from almost every kind of object.
  130. You may not want this, if your objects are of special types, and you want to copy the whole object instead of just copying its properties.
  131. You can accomplish this by passing in a function for the `isMergeableObject` option.
  132. If you only want to clone properties of plain objects, and ignore all "special" kinds of instantiated objects, you probably want to drop in [`is-plain-object`](https://github.com/jonschlinkert/is-plain-object).
  133. ```js
  134. const isPlainObject = require('is-plain-object')
  135. function SuperSpecial() {
  136. this.special = 'oh yeah man totally'
  137. }
  138. const instantiatedSpecialObject = new SuperSpecial()
  139. const target = {
  140. someProperty: {
  141. cool: 'oh for sure'
  142. }
  143. }
  144. const source = {
  145. someProperty: instantiatedSpecialObject
  146. }
  147. const defaultOutput = merge(target, source)
  148. defaultOutput.someProperty.cool // => 'oh for sure'
  149. defaultOutput.someProperty.special // => 'oh yeah man totally'
  150. defaultOutput.someProperty instanceof SuperSpecial // => false
  151. const customMergeOutput = merge(target, source, {
  152. isMergeableObject: isPlainObject
  153. })
  154. customMergeOutput.someProperty.cool // => undefined
  155. customMergeOutput.someProperty.special // => 'oh yeah man totally'
  156. customMergeOutput.someProperty instanceof SuperSpecial // => true
  157. ```
  158. ### `clone`
  159. *Deprecated.*
  160. Defaults to `true`.
  161. If `clone` is `false` then child objects will be copied directly instead of being cloned. This was the default behavior before version 2.x.
  162. # Testing
  163. With [npm](http://npmjs.org) do:
  164. ```sh
  165. npm test
  166. ```
  167. # License
  168. MIT