UNPKG

4.61 kBMarkdownView Raw
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
16Install package:
17
18```bash
19# yarn
20yarn add defu
21# npm
22npm install defu
23# pnpm
24pnpm install defu
25```
26
27## Usage
28
29```js
30import { defu } from 'defu'
31
32const options = defu(object, ...defaults)
33```
34
35Leftmost 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
43import { defu } from 'defu'
44
45console.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
52const { defu } = require('defu')
53```
54
55## Custom Merger
56
57Sometimes default merging strategy is not desirable. Using `createDefu` we can create a custom instance with different merging strategy.
58
59This 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
64import { createDefu } from 'defu'
65
66const ext = createDefu((obj, key, value) => {
67 if (typeof obj[key] === 'number' && typeof value === 'number') {
68 obj[key] += val
69 return true
70 }
71})
72
73ext({ cost: 15 }, { cost: 10 }) // { cost: 25 }
74```
75
76## Function Merger
77
78Using `defuFn`, if user provided a function, it will be called with default value instead of merging.
79
80I 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
85import { defuFn } from 'defu'
86
87defuFn({
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
111import { defuArrayFn } from 'defu'
112
113defuArrayFn({
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
140console.log(defu({ array: ['b', 'c'] }, { array: ['a'] }))
141// => { array: ['a', 'b', 'c']}
142```
143
144## Type
145
146We expose `Defu` as a type utility to return a merged type that follows the rules that defu follows.
147
148```js
149import type { Defu } from 'defu'
150
151type Options = Defu<{ foo: 'bar' }, [{}, { bar: 'baz' }, { something: 42 }]>
152// returns { foo: 'bar', bar: 'baz', 'something': 42 }
153```
154
155## License
156
157MIT. Made with 💖
158
159<!-- Refs -->
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