Skip to content

Instantly share code, notes, and snippets.

@bobpoekert
Created November 8, 2013 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobpoekert/7376221 to your computer and use it in GitHub Desktop.
Save bobpoekert/7376221 to your computer and use it in GitHub Desktop.
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