Skip to content

Instantly share code, notes, and snippets.

@ArnonEilat
Created November 16, 2015 14:21
Show Gist options
  • Save ArnonEilat/333b0cc4d93bac13acec to your computer and use it in GitHub Desktop.
Save ArnonEilat/333b0cc4d93bac13acec to your computer and use it in GitHub Desktop.
Boolean.prototype.toggle = function() {
return !this.valueOf();
}
/**
* @description Parses mixed type values into booleans.
* @param {Mixed} value
* @param {Boolean} nullOnFailure = false
* @return {Boolean}
*/
Boolean.prototype.toBoolean: function(obj) {
if (typeof obj === 'undefined')
return false;
if (typeof obj === 'string')
obj = obj.toLowerCase().trim();
switch (obj) {
case 'true':
case 'yes':
case '1':
case 1:
case true:
return true;
case 'false':
case 'no':
case '0':
case false:
case 0:
case null:
return false;
default:
return Boolean(obj);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment