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.

53 lines
1.1 KiB

2 years ago
  1. # defined
  2. return the first argument that is `!== undefined`
  3. [![browser support](http://ci.testling.com/substack/defined.png)](http://ci.testling.com/substack/defined)
  4. [![build status](https://secure.travis-ci.org/substack/defined.png)](http://travis-ci.org/substack/defined)
  5. Most of the time when I chain together `||`s, I actually just want the first
  6. item that is not `undefined`, not the first non-falsy item.
  7. This module is like the defined-or (`//`) operator in perl 5.10+.
  8. # example
  9. ``` js
  10. var defined = require('defined');
  11. var opts = { y : false, w : 4 };
  12. var x = defined(opts.x, opts.y, opts.w, 100);
  13. console.log(x);
  14. ```
  15. ```
  16. $ node example/defined.js
  17. false
  18. ```
  19. The return value is `false` because `false` is the first item that is
  20. `!== undefined`.
  21. # methods
  22. ``` js
  23. var defined = require('defined')
  24. ```
  25. ## var x = defined(a, b, c...)
  26. Return the first item in the argument list `a, b, c...` that is `!== undefined`.
  27. If all the items are `=== undefined`, return undefined.
  28. # install
  29. With [npm](https://npmjs.org) do:
  30. ```
  31. npm install defined
  32. ```
  33. # license
  34. MIT