I'm looking for a way to test a value. I'd like it to behave this way:
_.isBlank(undefined) : true
_.isBlank(null) : true
_.isBlank(NaN) : true
_.isBlank('') : true
_.isBlank('a') : false
_.isBlank(0) : false
_.isBlank(1) : false
_.isBlank({}) : true
_.isBlank([]) : true
_.isBlank({foo: 'bar'}) : false
_.isBlank(['foo', 'bar']) : false
There is _.isNil
, but _.isNil('')
is false. _.isEmpty
sounded promising but _.isEmpty(0)
is true
.
Of course, I could combine several tests together, but it would be cleaner to have it out-of-the-box, wouldn't it?
There is no method in Lodash for your specific case.
You have to make a function if you want to translate your intention:
function isBlank(value) {
return _.isEmpty(value) && !_.isNumber(value) || _.isNaN(value);
}
Results:
isBlank(undefined)
// => true
isBlank(null)
// => true
isBlank('')
// => true
isBlank([])
// => true
isBlank({})
// => true
isBlank(NaN)
// => true
isBlank(0)
// => false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With