Git Product home page Git Product logo

defu's Introduction

๐ŸŒŠ 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 ๐Ÿ’–

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.