UNPKG

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