wishthis/node_modules/defaults/README.md

40 lines
771 B
Markdown
Raw Normal View History

2022-06-08 10:36:39 +00:00
# defaults
2022-10-10 08:26:02 +00:00
> A simple one level options merge utility
2022-06-08 10:36:39 +00:00
2022-10-10 08:26:02 +00:00
## Install
2022-06-08 10:36:39 +00:00
2022-10-10 08:26:02 +00:00
```sh
npm install defaults
```
2022-06-08 10:36:39 +00:00
2022-10-10 08:26:02 +00:00
## Usage
2022-06-08 10:36:39 +00:00
2022-10-10 08:26:02 +00:00
```js
const defaults = require('defaults');
2022-06-08 10:36:39 +00:00
2022-10-10 08:26:02 +00:00
const handle = (options, fn) => {
options = defaults(options, {
timeout: 100
});
2022-06-08 10:36:39 +00:00
2022-10-10 08:26:02 +00:00
setTimeout(() => {
fn(options);
}, options.timeout);
2022-06-08 10:36:39 +00:00
}
2022-10-10 08:26:02 +00:00
handle({timeout: 1000}, () => {
// We're here 1000 ms later
2022-06-08 10:36:39 +00:00
});
2022-10-10 08:26:02 +00:00
handle({timeout: 10000}, () => {
// We're here 10s later
2022-06-08 10:36:39 +00:00
});
```
2022-10-10 08:26:02 +00:00
## Summary
2022-06-08 10:36:39 +00:00
this module exports a function that takes 2 arguments: `options` and `defaults`. When called, it overrides all of `undefined` properties in `options` with the clones of properties defined in `defaults`
Sidecases: if called with a falsy `options` value, options will be initialized to a new object before being merged onto.