All Projects â†’ unjs â†’ Defu

unjs / Defu

Licence: mit
Assign default properties, recursively. 🌊

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Defu

Lmify
Install NPM dependencies programmatically 🤙
Stars: ✭ 98 (-39.13%)
Mutual labels:  npm, yarn
Sass Recipes
Sass things that I do all the time or should remember to do because googling tutorials gets old
Stars: ✭ 156 (-3.11%)
Mutual labels:  npm, yarn
Democracy Client
DEMOCRACY App Client
Stars: ✭ 98 (-39.13%)
Mutual labels:  npm, yarn
Tbify
使用淘宝镜像运行命令: tbify [nvm|npm|npx|yarn|pnpm|...]
Stars: ✭ 153 (-4.97%)
Mutual labels:  npm, yarn
Foxy
A fast, reliable, and secure NPM/Yarn bridge for Composer
Stars: ✭ 137 (-14.91%)
Mutual labels:  npm, yarn
Emma Cli
📦 Terminal assistant to find and install node packages.
Stars: ✭ 1,201 (+645.96%)
Mutual labels:  npm, yarn
Cheatsheets
A curated list of everything I look up more than twice
Stars: ✭ 109 (-32.3%)
Mutual labels:  npm, yarn
Floatsidebar.js
Lightweight (2kb gzipped), zero-dependency javascript library for making float sidebars based on the finite state machine
Stars: ✭ 56 (-65.22%)
Mutual labels:  npm, yarn
Vscode Deploy Reloaded
Recoded version of Visual Studio Code extension 'vs-deploy', which provides commands to deploy files to one or more destinations.
Stars: ✭ 129 (-19.88%)
Mutual labels:  npm, yarn
Tiny Package Manager
Learn how npm or Yarn v1 works.
Stars: ✭ 125 (-22.36%)
Mutual labels:  npm, yarn
Have It
The fastest NPM install does nothing because you already have it
Stars: ✭ 75 (-53.42%)
Mutual labels:  npm, yarn
Klap
zero config, zero dependency bundler for tiny javascript packages
Stars: ✭ 143 (-11.18%)
Mutual labels:  npm, yarn
Package.json
文件 package.json 的说明文档。
Stars: ✭ 67 (-58.39%)
Mutual labels:  npm, yarn
Node Developer Boilerplate
🍭 Boilerplate for ES6+ Node.js and npm Developer
Stars: ✭ 82 (-49.07%)
Mutual labels:  npm, yarn
Yarn.msbuild
MSBuild integration for the Yarn package manager.
Stars: ✭ 57 (-64.6%)
Mutual labels:  npm, yarn
Yarn
The 1.x line is frozen - features and bugfixes now happen on https://github.com/yarnpkg/berry
Stars: ✭ 40,325 (+24946.58%)
Mutual labels:  npm, yarn
React Use Api
Async HTTP request data for axios. Designed for diverse UI states, SSR and data pre-caching.
Stars: ✭ 49 (-69.57%)
Mutual labels:  npm, yarn
Gitpkg
use a sub directory of a github repo as yarn / npm dependency directly
Stars: ✭ 54 (-66.46%)
Mutual labels:  npm, yarn
Kymsu
Keep Your macOs Stuff Updated (KYMSU)
Stars: ✭ 119 (-26.09%)
Mutual labels:  npm, yarn
Yerna
A Lerna-like tool for managing Javascript monorepos using Yarn
Stars: ✭ 140 (-13.04%)
Mutual labels:  npm, yarn

defu

🌊 defu

Assign default properties, recursively. 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 💖

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].