版博士V2.0程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

README.md 4.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ![defu](.github/banner.svg)
  2. # 🌊 defu
  3. > Assign default properties, recursively. Lightweight and Fast!
  4. [![Standard JS][standard-src]][standard-href]
  5. [![codecov][codecov-src]][codecov-href]
  6. [![npm version][npm-v-src]][npm-v-href]
  7. [![npm downloads][npm-dm-src]][npm-dm-href]
  8. [![package phobia][packagephobia-src]][packagephobia-href]
  9. [![bundle phobia][bundlephobia-src]][bundlephobia-href]
  10. ## Install
  11. Install package:
  12. ```bash
  13. # yarn
  14. yarn add defu
  15. # npm
  16. npm install defu
  17. # pnpm
  18. pnpm install defu
  19. ```
  20. ## Usage
  21. ```js
  22. import { defu } from 'defu'
  23. const options = defu(object, ...defaults)
  24. ```
  25. Leftmost arguments have more priority when assigning defaults.
  26. ### Arguments
  27. - **object (Object):** The destination object.
  28. - **source (Object):** The source object.
  29. ```js
  30. import { defu } from 'defu'
  31. console.log(defu({ 'a': { 'b': 2 } }, { 'a': { 'b': 1, 'c': 3 } }))
  32. // => { a: { b: 2, c: 3 } }
  33. ```
  34. ### Using with CommonJS
  35. ```js
  36. const { defu } = require('defu')
  37. ```
  38. ## Custom Merger
  39. Sometimes default merging strategy is not desirable. Using `createDefu` we can create a custom instance with different merging strategy.
  40. This function accepts `obj` (source object), `key` and `value` (current value) and should return `true` if applied custom merging.
  41. **Example:** Sum numbers instead of overriding
  42. ```js
  43. import { createDefu } from 'defu'
  44. const ext = createDefu((obj, key, value) => {
  45. if (typeof obj[key] === 'number' && typeof value === 'number') {
  46. obj[key] += val
  47. return true
  48. }
  49. })
  50. ext({ cost: 15 }, { cost: 10 }) // { cost: 25 }
  51. ```
  52. ## Function Merger
  53. Using `defuFn`, if user provided a function, it will be called with default value instead of merging.
  54. I can be useful for default values manipulation.
  55. **Example:** Filter some items from defaults (array) and add 20 to the count default value.
  56. ```js
  57. import { defuFn } from 'defu'
  58. defuFn({
  59. ignore: (val) => val.filter(item => item !== 'dist'),
  60. count: (count) => count + 20
  61. }, {
  62. ignore: ['node_modules','dist'],
  63. count: 10
  64. })
  65. /*
  66. {
  67. ignore: ['node_modules'],
  68. count: 30
  69. }
  70. */
  71. ```
  72. **Note:** if the default value is not defined, the function defined won't be called and kept as value.
  73. ## Array Function Merger
  74. `defuArrayFn` is similar to `defuFn` but **only applies to array values defined in defaults**.
  75. **Example:** Filter some items from defaults (array) and add 20 to the count default value.
  76. ```js
  77. import { defuArrayFn } from 'defu'
  78. defuArrayFn({
  79. ignore(val) => val.filter(i => i !== 'dist'),
  80. count: () => 20
  81. }, {
  82. ignore: [
  83. 'node_modules',
  84. 'dist'
  85. ],
  86. count: 10
  87. })
  88. /*
  89. {
  90. ignore: ['node_modules'],
  91. count: () => 20
  92. }
  93. */
  94. ```
  95. **Note:** the function is called only if the value defined in defaults is an aray.
  96. ### Remarks
  97. - `object` and `defaults` are not modified
  98. - Nullish values ([`null`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null) and [`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined)) are skipped. Please use [defaults-deep](https://www.npmjs.com/package/defaults-deep) or [omit-deep](http://npmjs.com/package/omit-deep) or [lodash.defaultsdeep](https://www.npmjs.com/package/lodash.defaultsdeep) if you need to preserve or different behavior.
  99. - Assignment of `__proto__` and `constructor` keys will be skipped to prevent security issues with object pollution.
  100. - Will concat `array` values (if default property is defined)
  101. ```js
  102. console.log(defu({ array: ['b', 'c'] }, { array: ['a'] }))
  103. // => { array: ['a', 'b', 'c']}
  104. ```
  105. ## Type
  106. We expose `Defu` as a type utility to return a merged type that follows the rules that defu follows.
  107. ```js
  108. import type { Defu } from 'defu'
  109. type Options = Defu<{ foo: 'bar' }, [{}, { bar: 'baz' }, { something: 42 }]>
  110. // returns { foo: 'bar', bar: 'baz', 'something': 42 }
  111. ```
  112. ## License
  113. MIT. Made with 💖
  114. <!-- Refs -->
  115. [standard-src]: https://flat.badgen.net/badge/code%20style/standard/green
  116. [standard-href]: https://standardjs.com
  117. [npm-v-src]: https://flat.badgen.net/npm/v/defu/latest
  118. [npm-v-href]: https://npmjs.com/package/defu
  119. [npm-dm-src]: https://flat.badgen.net/npm/dm/defu
  120. [npm-dm-href]: https://npmjs.com/package/defu
  121. [packagephobia-src]: https://flat.badgen.net/packagephobia/install/defu
  122. [packagephobia-href]: https://packagephobia.now.sh/result?p=defu
  123. [bundlephobia-src]: https://flat.badgen.net/bundlephobia/min/defu
  124. [bundlephobia-href]: https://bundlephobia.com/result?p=defu
  125. [codecov-src]: https://flat.badgen.net/codecov/c/github/unjs/defu/master
  126. [codecov-href]: https://codecov.io/gh/unjs/defu