Skip to content

Instantly share code, notes, and snippets.

@ugommirikwe
Created February 7, 2019 21:15
Show Gist options
  • Save ugommirikwe/d993b6a45385e205b2d62419f977d1cb to your computer and use it in GitHub Desktop.
Save ugommirikwe/d993b6a45385e205b2d62419f977d1cb to your computer and use it in GitHub Desktop.
Utility function to coerce a value of any native JavaScript data type to a boolean representation.
function isEmpty(data) {
if (typeof data === 'number' || typeof data === 'boolean') {
return false
}
if (typeof data === 'undefined' || data === null) {
return true
}
if (typeof data.length !== 'undefined') {
if (typeof data === 'string') return data.trim().length === 0
return data.length === 0
}
let count = 0
for (let i in data) {
if (data.hasOwnProperty(i)) {
count++
}
}
return count === 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment