Last active
January 9, 2018 13:29
-
-
Save pirey/cb645ee8ec44d2bf160c721677ad2b36 to your computer and use it in GitHub Desktop.
is this thing not empty?
This file contains hidden or 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
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