# defaults

> Easily handle defaults for your options

Latest version **3.0.0** (published 2023-10-18) · MIT license · 0 weekly downloads

> **Native alternative:** Object.assign, or if deep clones are needed, use structuredClone — Use native JavaScript (Node 4.0.0+) (https://developer.mozilla.orgGlobal_Objects/Object/assign)

## Install

```sh
npm install defaults
pnpm add defaults
yarn add defaults
bun add defaults
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2023-10-18 |
| First published | 2013-02-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=18 |
| Dependencies | 0 |
| Unpacked size | 5.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 76 |
| Maintainers | sindresorhus |
| Keywords | config, defaults, options, object, merge, assign, properties, deep |

## Links

- npm: https://www.npmjs.com/package/defaults
- Repository: https://github.com/sindresorhus/node-defaults
- Homepage: https://github.com/sindresorhus/node-defaults#readme
- Issues: https://github.com/sindresorhus/node-defaults/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/defaults

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2023-10-18
- 2.0.2 — 2023-08-28
- 2.0.1 — 2023-04-11
- 2.0.0 — 2023-04-11
- 1.0.4 — 2022-10-10
- 1.0.3 — 2015-10-02
- 1.0.2 — 2015-03-15
- 1.0.1 — 2015-03-15
- 1.0.0 — 2013-02-03

## README

# defaults

> Easily handle defaults for your options

## Install

```sh
npm install defaults
```

## Usage

```js
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#spread_in_object_literals)?

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

---
_Source: https://npm.io/package/defaults · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
