Created
July 24, 2018 07:35
-
-
Save joseluisq/dead3a2c0434785db0454ed614dbf40e to your computer and use it in GitHub Desktop.
Convert a mixed value into a boolean
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
/** | |
* Convert a mixed value into a boolean | |
* | |
* @param {string|number|object} value | |
* @returns {boolean} | |
*/ | |
function toBoolean (value) { | |
if (!value) return false | |
if (typeof value === 'boolean') return value | |
switch (value.toString().toLowerCase()) { | |
case 'true': | |
case 'yes': | |
case '1': | |
return true | |
case 'false': | |
case 'no': | |
case '0': | |
case null: | |
return false | |
default: | |
return Boolean(value) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment