### Install Defu with Yarn, NPM, or PNPM Source: https://github.com/unjs/defu/blob/main/README.md Install the defu package using your preferred package manager. ```bash # yarn yarn add defu # npm npm install defu # pnpm pnpm install defu ``` -------------------------------- ### Defu Example: Merging Objects Source: https://github.com/unjs/defu/blob/main/README.md Demonstrates how `defu` merges nested objects, preserving existing properties and adding new ones from defaults. ```javascript import { defu } from "defu"; console.log(defu({ a: { b: 2 } }, { a: { b: 1, c: 3 } })); // => { a: { b: 2, c: 3 } } ``` -------------------------------- ### Defu Array Concatenation Example Source: https://github.com/unjs/defu/blob/main/README.md Demonstrates how `defu` concatenates array values when a default property is defined. ```javascript console.log(defu({ array: ["b", "c"] }, { array: ["a"] })); // => { array: ['b', 'c', 'a'] } ``` -------------------------------- ### Create Custom Defu Merger Source: https://github.com/unjs/defu/blob/main/README.md Use `createDefu` to instantiate a custom merger function. This example shows summing numbers instead of overriding them. ```javascript 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 } ``` -------------------------------- ### Defu Type Utility Example Source: https://github.com/unjs/defu/blob/main/README.md Utilize the `Defu` type utility to infer the merged TypeScript type of objects with default values. ```typescript import type { Defu } from 'defu' type Options = Defu<{ foo: 'bar' }, [{}, { bar: 'baz' }, { something: 42 }]> // returns { foo: 'bar', bar: 'baz', 'something': 42 } ``` -------------------------------- ### Basic Defu Usage Source: https://github.com/unjs/defu/blob/main/README.md Import and use the `defu` function to assign default properties recursively. Leftmost arguments have higher priority. ```javascript import { defu } from "defu"; const options = defu(object, ...defaults); ``` -------------------------------- ### Using Defu with CommonJS Source: https://github.com/unjs/defu/blob/main/README.md Import the `defu` function when using CommonJS modules. ```javascript const { defu } = require("defu"); ``` -------------------------------- ### Defu with Function Merger Source: https://github.com/unjs/defu/blob/main/README.md Utilize `defuFn` to allow functions as default values, which are then called with the default value. Useful for manipulating defaults. ```javascript 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 } */ ``` -------------------------------- ### Defu with Array Function Merger Source: https://github.com/unjs/defu/blob/main/README.md Use `defuArrayFn` for function mergers that specifically apply only to array values defined in defaults. ```javascript 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 } */ ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.