项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

README.markdown 2.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. deepmerge
  2. =========
  3. > ~540B gzipped, ~1.1kB minified
  4. Merge the enumerable attributes of two objects deeply.
  5. the future
  6. ----------
  7. Should we publish a version 2? [Give your opinion.](https://github.com/KyleAMathews/deepmerge/issues/72)
  8. example
  9. =======
  10. <!--js
  11. var merge = require('./')
  12. -->
  13. ```js
  14. var x = {
  15. foo: { bar: 3 },
  16. array: [{
  17. does: 'work',
  18. too: [ 1, 2, 3 ]
  19. }]
  20. }
  21. var y = {
  22. foo: { baz: 4 },
  23. quux: 5,
  24. array: [{
  25. does: 'work',
  26. too: [ 4, 5, 6 ]
  27. }, {
  28. really: 'yes'
  29. }]
  30. }
  31. var expected = {
  32. foo: {
  33. bar: 3,
  34. baz: 4
  35. },
  36. array: [{
  37. does: 'work',
  38. too: [ 1, 2, 3, 4, 5, 6 ]
  39. }, {
  40. really: 'yes'
  41. }],
  42. quux: 5
  43. }
  44. merge(x, y) // => expected
  45. ```
  46. methods
  47. =======
  48. ```
  49. var merge = require('deepmerge')
  50. ```
  51. merge(x, y, [options])
  52. -----------
  53. Merge two objects `x` and `y` deeply, returning a new merged object with the
  54. elements from both `x` and `y`.
  55. If an element at the same key is present for both `x` and `y`, the value from
  56. `y` will appear in the result.
  57. Merging creates a new object, so that neither `x` or `y` are be modified. However, child objects on `x` or `y` are copied over - if you want to copy all values, you must pass `true` to the clone option.
  58. merge.all(arrayOfObjects, [options])
  59. -----------
  60. Merges two or more objects into a single result object.
  61. ```js
  62. var x = { foo: { bar: 3 } }
  63. var y = { foo: { baz: 4 } }
  64. var z = { bar: 'yay!' }
  65. var expected = { foo: { bar: 3, baz: 4 }, bar: 'yay!' }
  66. merge.all([x, y, z]) // => expected
  67. ```
  68. ### options
  69. #### arrayMerge
  70. The merge will also merge arrays and 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.
  71. ```js
  72. function concatMerge(destinationArray, sourceArray, options) {
  73. destinationArray // => [1, 2, 3]
  74. sourceArray // => [3, 2, 1]
  75. options // => { arrayMerge: concatMerge }
  76. return destinationArray.concat(sourceArray)
  77. }
  78. merge([1, 2, 3], [3, 2, 1], { arrayMerge: concatMerge }) // => [1, 2, 3, 3, 2, 1]
  79. ```
  80. To prevent arrays from being merged:
  81. ```js
  82. const dontMerge = (destination, source) => source
  83. const output = merge({ coolThing: [1,2,3] }, { coolThing: ['a', 'b', 'c'] }, { arrayMerge: dontMerge })
  84. output // => { coolThing: ['a', 'b', 'c'] }
  85. ```
  86. #### clone
  87. Defaults to `false`. If `clone` is `true` then both `x` and `y` are recursively cloned as part of the merge.
  88. install
  89. =======
  90. With [npm](http://npmjs.org) do:
  91. ```sh
  92. npm install deepmerge
  93. ```
  94. 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).
  95. test
  96. ====
  97. With [npm](http://npmjs.org) do:
  98. ```sh
  99. npm test
  100. ```
  101. license
  102. =======
  103. MIT