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