What I am trying to achieve here is that, I made a simple utility function and that should return 'true' or 'false', based on the given argument to the isEmpty method.
// The below log should return 'false', But it's returning 'true'
console.log(isEmpty( () => {key: 1} ));
What I tried so far
function isEmpty(value) {
const type = typeof value;
if ((value !== null && type === 'object') || type === 'function') {
const properties = Object.keys(value);
return properties.length === 0 || properties.size === 0
}
return !value;
}
And it's working for below cases
console.log(isEmpty( {} )) // true
console.log(isEmpty( [] )) // true
console.log(isEmpty( Object.create(null) )) // true
console.log(isEmpty( null )) // true
console.log(isEmpty( '' )) // true
console.log(isEmpty( {key: 1} )) // false
console.log(isEmpty( [1,2,3] )) // false
But it's not working, when we get the return object / array from the function
console.log(isEmpty( () => ({key: 1}) ))
console.log(isEmpty( () => ([1,2,3]) ))
isEmptyon the value returned. Something likeif(type === 'function') return isEmpty(value())?() => {key: 1}supposed to be() => ({key: 1})?console.log(isEmpty( () => {key: 1} ))in this case you are passing a function not the return value. If you still want to check if the return value from function you have to invoke the function and check the same