defaults
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

defaults

Easily handle defaults for your options

Install

npm install defaults

Usage

import defaults from 'defaults';

const calculate = options => {
	options = defaults(options, {
		timeout: {
			before: 100,
			after: 100
		}
	});

	console.log(options);
	//=> {timeout: {before: 200, after: 100}}

	// …
}

// …

calculate({timeout: {before: 200}});

API

defaults(options, defaultOptions?)

Deeply merges the given options with the specified defaults and returns a new object.

The given parameters are deep-cloned and never mutated.

options

Type: object

The user-provided options.

If the value is not a plain object, a new plain object will be used instead.

defaultOptions

Type: object | undefined

The default options to use when a value is not provided in the options object.

FAQ

Why use this over object-spread?

  • Does not overwrite options if they are not defined in the options object
  • Supports deep merging of objects
  • Provides protection against prototype pollution attacks

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 3.0.0
    41,393
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 3.0.0
    41,393
  • 2.0.2
    1,513
  • 2.0.1
    50
  • 2.0.0
    76
  • 1.0.4
    21,953,661
  • 1.0.3
    13,248,080
  • 1.0.2
    2,326
  • 1.0.1
    4
  • 1.0.0
    1,037

Package Sidebar

Install

npm i defaults

Weekly Downloads

20,533,951

Version

3.0.0

License

MIT

Unpacked Size

5.65 kB

Total Files

5

Last publish

Collaborators

  • sindresorhus