Created
February 7, 2019 21:15
-
-
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.
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
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