Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

defu

unjs20.2mMIT6.1.4TypeScript support: included

Recursively assign default properties. Lightweight and Fast!

readme

🌊 defu

Assign default properties, recursively. Lightweight and Fast.

npm version npm downloads bundle Codecov License

Install

Install package:

# yarn
yarn add defu
# npm
npm install defu
# pnpm
pnpm install defu

Usage

import { defu } from "defu";

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

Leftmost arguments have more priority when assigning defaults.

Arguments

  • object (Object): The destination object.
  • source (Object): The source object.
import { defu } from "defu";

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

Using with CommonJS

const { defu } = require("defu");

Custom Merger

Sometimes default merging strategy is not desirable. Using createDefu 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

import { createDefu } from "defu";

const ext = createDefu((obj, key, value) => {
  if (typeof obj[key] === "number" && typeof value === "number") {
    obj[key] += value;
    return true;
  }
});

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

Function Merger

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

It can be useful for default values manipulation.

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

import { defuFn } from "defu";

defuFn(
  {
    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

defuArrayFn is similar to defuFn but only applies to array values defined in defaults.

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

import { defuArrayFn } from 'defu'

defuArrayFn({
  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
  • Nullish values (null and undefined) are skipped. Please use defaults-deep or omit-deep or lodash.defaultsdeep if you need to preserve or different behavior.
  • 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: ['b', 'c', 'a'] }

Type

We expose Defu as a type utility to return a merged type that follows the rules that defu follows.

import type { Defu } from 'defu'

type Options = Defu<{ foo: 'bar' }, [{}, { bar: 'baz' }, { something: 42 }]>
// returns { foo: 'bar', bar: 'baz', 'something': 42 }

License

MIT. Made with 💖

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

v6.1.4

compare changes

🩹 Fixes

  • Merge objects with Module type (#121)

💅 Refactors

  • Move isPlainObject to _utils to allow testing (e922a16)
  • Make isPlainObject logic more readable (e458b63)

📖 Documentation

🏡 Chore

✅ Tests

  • Improve tests for isPlainObject (b24a213)

❤️ Contributors

v6.1.3

compare changes

🩹 Fixes

  • Only merge plain objects (#111)

📖 Documentation

  • Update badges (581dd92)
  • Fix typo (#96)
  • Fix the result of the array merging (#99)
  • Fix typo (#107)

📦 Build

  • Backward compatible cjs entry (#110)

🏡 Chore

🎨 Styles

  • Format with prettier v3 (32650f1)

❤️ Contributors

v6.1.2

compare changes

🩹 Fixes

  • Add node16 compatible type declaration (#73)

🏡 Chore

  • Fix renovate config (#56)
  • Use changelogen to release (5e24124)

🎨 Styles

❤️ Contributors

6.1.1 (2022-11-14)

6.1.0 (2022-08-16)

Features

Bug Fixes

  • types: constrain inferred types of Defu (3d3ea3e)

6.0.1 (2022-08-16)

Bug Fixes

  • add typing to allow for non-objects input args (#42) (1f3a701)
  • merge object strings of many types (#44) (c7226f9)

6.0.0 (2022-03-21)

⚠ BREAKING CHANGES

  • Use named exports:
  • import defu from 'defu' => import { defu } from 'defu'
  • defu.fn => import { defuFn }
  • defu.arrayFn => import { defuArrayFn }
  • When merging input value with defaults with an array, order is reversed

Features

  • concat array defaults to the last (f6df314)
  • use named exports (4a8fc52)

Bug Fixes

5.0.1 (2022-01-13)

5.0.0 (2021-05-12)

⚠ BREAKING CHANGES

  • undefined values will be bypassed and not consistent behavior with defaults-deep anymore.

Features

4.0.1 (2021-04-23)

4.0.0 (2021-04-23)

⚠ BREAKING CHANGES

  • module exports

Features

3.2.2 (2020-11-10)

Bug Fixes

  • switch back to bili for es5 support till fixing in siroc (07786c2)

3.2.1 (2020-11-09)

Bug Fixes

  • types: correct type inference where merged types are same (#26) (f322607)

3.2.0 (2020-11-09)

Features

  • add type inference for defu result (#24) (934d736)
  • pass namespace to custom merger (#25) (6bd7ef5)

3.1.0 (2020-08-04)

Features

3.0.1 (2020-07-29)

Bug Fixes

  • recursively pass merger (ec09394)

3.0.0 (2020-07-28)

⚠ BREAKING CHANGES

  • defau will merge arrays too (#18)

Features

2.0.4 (2020-05-22)

Bug Fixes

2.0.3 (2020-05-22)

Bug Fixes

  • specify type declaration file more precisely (#15) (6aa47d4)

2.0.2 (2020-04-19)

2.0.1 (2020-04-19)

Docs

  • Add note about null

2.0.0 (2020-04-19)

Features

  • Support passing multiple defaults (89ef702)
  • Typescript rewrite (9c906e6)

1.0.0 (2020-02-02)

0.0.4 (2020-01-01)

Bug Fixes

0.0.3 (2019-05-25)

0.0.2 (2019-05-25)

0.0.1 (2019-02-07)

Bug Fixes

  • imrpove non-object handlers (f89fa28)