Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

JS: Parse mixed type values into booleans. Like 'true' to true.

View parseBooleanStyle.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/**
* 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
Something went wrong with that request. Please try again.