Skip to content

Instantly share code, notes, and snippets.

@pirey
Last active January 9, 2018 13:29
Show Gist options
  • Save pirey/cb645ee8ec44d2bf160c721677ad2b36 to your computer and use it in GitHub Desktop.
Save pirey/cb645ee8ec44d2bf160c721677ad2b36 to your computer and use it in GitHub Desktop.
is this thing not empty?
const is = require('is_js') // require this awesome library
const notEmpty = o =>
is.not.empty(o) && Object.keys(o).map(key => {
return is.array(o[key])
? !is.all.empty(o[key])
: is.not.empty(o[key])
}).reduce((a, b) => a && b, true)
// empty examples
const a = {}
const b = {
name: ''
}
const c = {
arr: []
}
const d = {
arr: [
{},
{}
]
}
console.log(notEmpty(a)) // false
console.log(notEmpty(b)) // false
console.log(notEmpty(c)) // false
console.log(notEmpty(d)) // false
// not empty examples
const o = {x:1}
const p = {
name: 'hisoka'
}
const q = {
arr: [1, 2]
}
const r = {
arr: [
{x:2},
{}
]
}
console.log(notEmpty(o)) // true
console.log(notEmpty(p)) // true
console.log(notEmpty(q)) // true
console.log(notEmpty(r)) // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment