Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

readme.md 5.7 KiB

il y a 3 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. # deepmerge
  2. Merges the enumerable properties of two or more objects deeply.
  3. > UMD bundle is 646B minified+gzipped
  4. ## Getting Started
  5. ### Example Usage
  6. <!--js
  7. const merge = require('./')
  8. -->
  9. ```js
  10. const x = {
  11. foo: { bar: 3 },
  12. array: [{
  13. does: 'work',
  14. too: [ 1, 2, 3 ]
  15. }]
  16. }
  17. const y = {
  18. foo: { baz: 4 },
  19. quux: 5,
  20. array: [{
  21. does: 'work',
  22. too: [ 4, 5, 6 ]
  23. }, {
  24. really: 'yes'
  25. }]
  26. }
  27. const output = {
  28. foo: {
  29. bar: 3,
  30. baz: 4
  31. },
  32. array: [{
  33. does: 'work',
  34. too: [ 1, 2, 3 ]
  35. }, {
  36. does: 'work',
  37. too: [ 4, 5, 6 ]
  38. }, {
  39. really: 'yes'
  40. }],
  41. quux: 5
  42. }
  43. merge(x, y) // => output
  44. ```
  45. ### Installation
  46. With [npm](http://npmjs.org) do:
  47. ```sh
  48. npm install deepmerge
  49. ```
  50. 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).
  51. ### Include
  52. deepmerge exposes a CommonJS entry point:
  53. ```
  54. const merge = require('deepmerge')
  55. ```
  56. The ESM entry point was dropped due to a [Webpack bug](https://github.com/webpack/webpack/issues/6584).
  57. # API
  58. ## `merge(x, y, [options])`
  59. Merge two objects `x` and `y` deeply, returning a new merged object with the
  60. elements from both `x` and `y`.
  61. If an element at the same key is present for both `x` and `y`, the value from
  62. `y` will appear in the result.
  63. Merging creates a new object, so that neither `x` or `y` is modified.
  64. **Note:** By default, arrays are merged by concatenating them.
  65. ## `merge.all(arrayOfObjects, [options])`
  66. Merges any number of objects into a single result object.
  67. ```js
  68. const foobar = { foo: { bar: 3 } }
  69. const foobaz = { foo: { baz: 4 } }
  70. const bar = { bar: 'yay!' }
  71. merge.all([ foobar, foobaz, bar ]) // => { foo: { bar: 3, baz: 4 }, bar: 'yay!' }
  72. ```
  73. ## Options
  74. ### `arrayMerge`
  75. There are multiple ways to merge two arrays, below are a few examples but you can also create your own custom function.
  76. #### Overwrite Array
  77. Overwrites the existing array values completely rather than concatenating them
  78. ```js
  79. const overwriteMerge = (destinationArray, sourceArray, options) => sourceArray
  80. merge(
  81. [1, 2, 3],
  82. [3, 2, 1],
  83. { arrayMerge: overwriteMerge }
  84. ) // => [3, 2, 1]
  85. ```
  86. #### Combine Array
  87. Combine arrays, such as overwriting existing defaults while also adding/keeping values that are different names
  88. To use the legacy (pre-version-2.0.0) array merging algorithm, use the following:
  89. ```js
  90. const emptyTarget = value => Array.isArray(value) ? [] : {}
  91. const clone = (value, options) => merge(emptyTarget(value), value, options)
  92. const combineMerge = (target, source, options) => {
  93. const destination = target.slice()
  94. source.forEach((item, index) => {
  95. if (typeof destination[index] === 'undefined') {
  96. const cloneRequested = options.clone !== false
  97. const shouldClone = cloneRequested && options.isMergeableObject(item)
  98. destination[index] = shouldClone ? clone(item, options) : item
  99. } else if (options.isMergeableObject(item)) {
  100. destination[index] = merge(target[index], item, options)
  101. } else if (target.indexOf(item) === -1) {
  102. destination.push(item)
  103. }
  104. })
  105. return destination
  106. }
  107. merge(
  108. [{ a: true }],
  109. [{ b: true }, 'ah yup'],
  110. { arrayMerge: combineMerge }
  111. ) // => [{ a: true, b: true }, 'ah yup']
  112. ```
  113. ### `isMergeableObject`
  114. By default, deepmerge clones every property from almost every kind of object.
  115. 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.
  116. You can accomplish this by passing in a function for the `isMergeableObject` option.
  117. 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).
  118. ```js
  119. const isPlainObject = require('is-plain-object')
  120. function SuperSpecial() {
  121. this.special = 'oh yeah man totally'
  122. }
  123. const instantiatedSpecialObject = new SuperSpecial()
  124. const target = {
  125. someProperty: {
  126. cool: 'oh for sure'
  127. }
  128. }
  129. const source = {
  130. someProperty: instantiatedSpecialObject
  131. }
  132. const defaultOutput = merge(target, source)
  133. defaultOutput.someProperty.cool // => 'oh for sure'
  134. defaultOutput.someProperty.special // => 'oh yeah man totally'
  135. defaultOutput.someProperty instanceof SuperSpecial // => false
  136. const customMergeOutput = merge(target, source, {
  137. isMergeableObject: isPlainObject
  138. })
  139. customMergeOutput.someProperty.cool // => undefined
  140. customMergeOutput.someProperty.special // => 'oh yeah man totally'
  141. customMergeOutput.someProperty instanceof SuperSpecial // => true
  142. ```
  143. ### `customMerge`
  144. Specifies a function which can be used to override the default merge behavior for a property, based on the property name.
  145. The `customMerge` function will be passed the key for each property, and should return the function which should be used to merge the values for that property.
  146. It may also return undefined, in which case the default merge behaviour will be used.
  147. ```js
  148. const alex = {
  149. name: {
  150. first: 'Alex',
  151. last: 'Alexson'
  152. },
  153. pets: ['Cat', 'Parrot']
  154. }
  155. const tony = {
  156. name: {
  157. first: 'Tony',
  158. last: 'Tonison'
  159. },
  160. pets: ['Dog']
  161. }
  162. const mergeNames = (nameA, nameB) => `${nameA.first} and ${nameB.first}`
  163. const options = {
  164. customMerge: (key) => {
  165. if (key === 'name') {
  166. return mergeNames
  167. }
  168. }
  169. }
  170. const result = merge(alex, tony, options)
  171. result.name // => 'Alex and Tony'
  172. result.pets // => ['Cat', 'Parrot', 'Dog']
  173. ```
  174. ### `clone`
  175. *Deprecated.*
  176. Defaults to `true`.
  177. If `clone` is `false` then child objects will be copied directly instead of being cloned. This was the default behavior before version 2.x.
  178. # Testing
  179. With [npm](http://npmjs.org) do:
  180. ```sh
  181. npm test
  182. ```
  183. # License
  184. MIT