Created
April 1, 2014 23:02
-
-
Save sorensen/9924872 to your computer and use it in GitHub Desktop.
Find first defined argument
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
/** | |
* Get first defined argument | |
* | |
* @param {...} any argument(s) | |
* @return {Any} first defined argument | |
*/ | |
exports.defined = function() { | |
var args = Array.prototype.slice.call(arguments); | |
for (var i = 0; i < args.length; i++) { | |
var arg = args[i] | |
, type = Object.prototype.toString.call(arg) | |
; | |
// Check that arg is not undefined, null, or NaN | |
if (type !== '[object Undefined]' && type !== '[object Null]' && !isNaN(arg)) { | |
return arg; | |
} | |
} | |
// Return last argument | |
return args[args.length - 1]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment