Permalink
Browse files

docs, example, and code ripped from the tape module

  • Loading branch information...
0 parents commit 6bec2cc9c82f19c2960f344b5141154d6eaa7380 @substack committed Nov 26, 2012
Showing with 56 additions and 0 deletions.
  1. +4 −0 example/defined.js
  2. +5 −0 index.js
  3. +47 −0 readme.markdown
@@ -0,0 +1,4 @@
+var defined = require('../');
+var opts = { y : false, w : 4 };
+var x = defined(opts.x, opts.y, opts.w, 8);
+console.log(x);
@@ -0,0 +1,5 @@
+module.exports = function () {
+ for (var i = 0; i < arguments.length; i++) {
+ if (arguments[i] !== undefined) return arguments[i];
+ }
+};
@@ -0,0 +1,47 @@
+# defined
+
+return the first argument that is `!== undefined`
+
+Most of the time when I chain together `||`s, I actually just want the first
+item that is not `undefined`, not the first non-falsy item.
+
+# example
+
+``` js
+var defined = require('defined');
+var opts = { y : false, w : 4 };
+var x = defined(opts.x, opts.y, opts.w, 100);
+console.log(x);
+```
+
+```
+$ node example/defined.js
+false
+```
+
+The return value is `false` because `false` is the first item that is
+`!== undefined`.
+
+# methods
+
+``` js
+var defined = require('defined')
+```
+
+## var x = defined(a, b, c...)
+
+Return the first item in the argument list `a, b, c...` that is `!== undefined`.
+
+If all the items are `=== undefined`, return undefined.
+
+# install
+
+With [npm](https://npmjs.org) do:
+
+```
+npm install defined
+```
+
+# license
+
+MIT

0 comments on commit 6bec2cc

Please sign in to comment.