Socket
Socket
Sign inDemoInstall

defaults

Package Overview
Dependencies
0
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    defaults

Easily handle defaults for your options


Version published
Weekly downloads
20M
increased by2.34%
Maintainers
1
Install size
5.52 kB
Created
Weekly downloads
 

Package description

What is defaults?

The `defaults` npm package is primarily used for merging a set of default properties with user-provided options. It is particularly useful in situations where you want to ensure that an object contains a certain set of properties with default values, even if some of those properties are not provided by the user. This can be very handy in configuration objects for libraries, APIs, or any other modular pieces of code that require a predictable structure of input options.

What are defaults's main functionalities?

Merging default options with user options

This feature allows the merging of a user-provided options object with a default options object. If the user provides a value for a given property, that value is used; otherwise, the default value is applied. This is particularly useful for configuring applications or modules where certain parameters are optional but should have a defined default behavior.

{"const defaults = require('defaults');\nconst userOptions = { color: 'blue' };\nconst defaultOptions = { color: 'red', size: 'large' };\nconst options = defaults(userOptions, defaultOptions);\nconsole.log(options); // Output: { color: 'blue', size: 'large' }"}

Other packages similar to defaults

Readme

Source

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

Keywords

FAQs

Last updated on 18 Oct 2023

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc