You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
771 B

1 month ago
  1. # defaults
  2. > A simple one level options merge utility
  3. ## Install
  4. ```sh
  5. npm install defaults
  6. ```
  7. ## Usage
  8. ```js
  9. const defaults = require('defaults');
  10. const handle = (options, fn) => {
  11. options = defaults(options, {
  12. timeout: 100
  13. });
  14. setTimeout(() => {
  15. fn(options);
  16. }, options.timeout);
  17. }
  18. handle({timeout: 1000}, () => {
  19. // We're here 1000 ms later
  20. });
  21. handle({timeout: 10000}, () => {
  22. // We're here 10s later
  23. });
  24. ```
  25. ## Summary
  26. 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`
  27. Sidecases: if called with a falsy `options` value, options will be initialized to a new object before being merged onto.