-
-
Save chrisblossom/9ea461b1e7c58cd0f6a2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Parses mixed type values into booleans. This is the same function as filter_var in PHP using boolean validation | |
* @param {Mixed} value | |
* @param {Boolean} nullOnFailure = false | |
* @return {Boolean|Null} | |
*/ | |
var parseBooleanStyle = function(value, nullOnFailure = false){ | |
switch(value){ | |
case true: | |
case 'true': | |
case 1: | |
case '1': | |
case 'on': | |
case 'yes': | |
value = true; | |
break; | |
case false: | |
case 'false': | |
case 0: | |
case '0': | |
case 'off': | |
case 'no': | |
value = false; | |
break; | |
default: | |
if(nullOnFailure){ | |
value = null; | |
}else{ | |
value = false; | |
} | |
break; | |
} | |
return value; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment