Skip to content

Commit

Permalink
docs, example, and code ripped from the tape module
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Nov 26, 2012
0 parents commit 6bec2cc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
4 changes: 4 additions & 0 deletions example/defined.js
Original file line number Diff line number Diff line change
@@ -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);
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function () {
for (var i = 0; i < arguments.length; i++) {
if (arguments[i] !== undefined) return arguments[i];
}
};
47 changes: 47 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
@@ -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.