Skip to content

Instantly share code, notes, and snippets.

@CyberStrike
Last active July 3, 2019 18:59
Show Gist options
  • Save CyberStrike/aecd7a8c1f6ea66deaabacce984b27e6 to your computer and use it in GitHub Desktop.
Save CyberStrike/aecd7a8c1f6ea66deaabacce984b27e6 to your computer and use it in GitHub Desktop.
Utility function for checking if a value is truthy
function _exists (thing) {
  const IS_UNDEFINED = 'undefined' === (typeof thing)
  const IS_NULL      = null === thing
  if (IS_UNDEFINED || IS_NULL) return false

  const TYPE = (typeof thing)
  const IS_OBJECT = ('object' === TYPE)
  const IS_STRING = ('string' === TYPE)
  const IS_NUMBER = ('number' === TYPE)
  const IS_ARRAY  = (Array.isArray(thing))

  if (IS_STRING || IS_ARRAY) return 0 < thing.length
  if (IS_NUMBER) return 0 < String(thing).length
  if (IS_OBJECT) return 0 < Object.keys(thing).length

  return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment