Created
November 8, 2013 19:25
-
-
Save bobpoekert/7376221 to your computer and use it in GitHub Desktop.
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
var truthiness = function(obj) { | |
/* ?P -> Boolean */ | |
switch(obj) { | |
case 0: | |
case false: | |
case null: | |
case undefined: | |
return false; | |
case true: | |
return true; | |
} | |
if (typeof(obj) === 'string') { | |
return /[^\s]/.test(obj); | |
} | |
if (_.isNumber(obj)) { | |
return true; | |
} | |
if (obj.length !== undefined) { | |
return obj.length > 0; | |
} | |
for (var k in obj) { | |
if (obj.hasOwnProperty(k)) { | |
return true; | |
} | |
} | |
if (obj.nodeType) return true; | |
return false; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment