You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

defu

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

defu

Recursively assign default properties. Lightweight and Fast!


Version published
Weekly downloads
6M
increased by2.61%
Maintainers
1
Install size
18.8 kB
Created
Weekly downloads
 

Package description

What is defu?

The defu npm package is a utility for performing default deep assignments on objects. It is commonly used to merge a set of objects into a single object, where the properties of later objects will overwrite those of earlier ones if they exist. This is particularly useful for managing configurations or options objects where default values need to be easily overridden by user inputs or other sources.

What are defu's main functionalities?

Deep Object Merging

This feature allows for the deep merging of objects. Properties from the second object (`objB`) are merged into the first object (`objA`), with nested properties being merged rather than replaced. This is useful for combining configurations with nested structures.

{"const defu = require('defu');\nconst objA = { foo: { bar: true } };\nconst objB = { foo: { baz: false }, qux: 1 };\nconst result = defu(objA, objB);\nconsole.log(result); // Output: { foo: { bar: true, baz: false }, qux: 1 }"}

Array Merging

This demonstrates how `defu` can be used to merge arrays contained within objects. Instead of replacing the array in `arrA` with the one in `arrB`, `defu` concatenates the arrays, preserving the order and contents of both.

{"const defu = require('defu');\nconst arrA = { arr: [1, 2] };\nconst arrB = { arr: [3, 4] };\nconst result = defu(arrA, arrB);\nconsole.log(result); // Output: { arr: [1, 2, 3, 4] }"}

Other packages similar to defu

Changelog

Source

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

❤️ Contributors

Readme

Source

🌊 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 💖

FAQs

Package last updated on 24 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc