Skip to content

Instantly share code, notes, and snippets.

@joseluisq
Created July 24, 2018 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseluisq/dead3a2c0434785db0454ed614dbf40e to your computer and use it in GitHub Desktop.
Save joseluisq/dead3a2c0434785db0454ed614dbf40e to your computer and use it in GitHub Desktop.
Convert a mixed value into a boolean
/**
* 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