Created
November 16, 2015 14:21
-
-
Save ArnonEilat/333b0cc4d93bac13acec 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
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