-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs, example, and code ripped from the tape module
- Loading branch information
James Halliday
committed
Nov 26, 2012
0 parents
commit 6bec2cc
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |