defu
Recursively assign default properties. Lightweight and Fast!
Last updated 2 months ago by pi0 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install defu 
SYNC missed versions from official npm registry.

???? defu

Recursively assign default properties. Lightweight and Fast!

Standard JS david dm codecov

npm version npm downloads package phobia bundle phobia

Install

Install package:

yarn add defu
# or
npm install defu

Usage

const options = defu (object, ...defaults)

Leftmost arguments have more priority when assigning defaults.

Arguments

  • object (Object): The destination object.
  • source (Object): The source object.
const defu = require('defu')

console.log(defu({ 'a': { 'b': 2 } }, { 'a': { 'b': 1, 'c': 3 } }))
// => { a: { b: 2, c: 3 } }

Custom Merger

Sometimes default merging strategy is not desirable. Using defu.extend we can create a custom instance with different merging strategy.

This function accepts obj (source object), key and value (current value) and should return true if applied custom merging.

Example: Sum numbers instead of overriding

const ext = defu.extend((obj, key, value) => {
  if (typeof obj[key] === 'number' && typeof value === 'number') {
    obj[key] += val
    return true
  }
})

ext({ cost: 15 }, { cost: 10 }) // { cost: 25 }

Function Merger

Using defu.fn, if user provided a function, it will be called with default value instead of merging.

I can be useful for default values manipulation.

Example: Filter some items from defaults (array) and add 20 to the count default value.


defu.fn({
  ignore: (val) => val.filter(item => item !== 'dist'),
  count: (count) => count + 20
 }, {
   ignore: ['node_modules','dist'],
   count: 10
 })
 /*
 {
    ignore: ['node_modules'],
    count: 30
  }
  */

Note: if the default value is not defined, the function defined won't be called and kept as value.

Array Function Merger

defu.arrayFn is similar to defu.fn but only applies to array values defined in defaults.

Example: Filter some items from defaults (array) and add 20 to the count default value.


defu.arrayFn({
  ignore(val) => val.filter(i => i !== 'dist'),
  count: () => 20
 }, {
   ignore: [
     'node_modules',
     'dist'
   ],
   count: 10
 })
 /*
  {
    ignore: ['node_modules'],
    count: () => 20
  }
  */

Note: the function is called only if the value defined in defaults is an aray.

Remarks

  • object and defaults are not modified
  • null values are skipped same as defaults-deep. Please use either omit-deep or lodash.defaultsdeep if you need to preserve.
  • Assignment of __proto__ and constructor keys will be skipped to prevent security issues with object pollution.
  • Will concat array values (if default property is defined)
console.log(defu({ array: ['b', 'c'] }, { array: ['a'] }))
// => { array: ['a', 'b', 'c']}

License

MIT. Made with ????

Current Tags

  • 3.2.2                                ...           latest (2 months ago)

16 Versions

  • 3.2.2                                ...           2 months ago
  • 3.2.1                                ...           2 months ago
  • 3.2.0                                ...           2 months ago
  • 3.1.0                                ...           5 months ago
  • 3.0.1                                ...           6 months ago
  • 3.0.0                                ...           6 months ago
  • 2.0.4                                ...           8 months ago
  • 2.0.3                                ...           8 months ago
  • 2.0.2                                ...           9 months ago
  • 2.0.1                                ...           9 months ago
  • 2.0.0                                ...           9 months ago
  • 1.0.0                                ...           a year ago
  • 0.0.4                                ...           a year ago
  • 0.0.3                                ...           2 years ago
  • 0.0.1                                ...           2 years ago
  • 0.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 2,790
This Week 5,478
This Month 38,158
Last Day 2,688
Last Week 15,528
Last Month 77,467
Dependencies (0)
None
Dev Dependencies (12)

Copyright 2014 - 2017 © taobao.org |