Lodash: _.isBlank(str)

Created on 29 Apr 2015  路  3Comments  路  Source: lodash/lodash

Hi! Thanks for helping me put bread on my family's table!

Anyhow, I found the isBlank() semantics pretty useful back in the day when doing Java coding with Commons Lang. I looked for it today in lodash and when I found it wasn't there I rolled my own with _.isString() and _.trim() and length > 0.

Here's the function I'm thinking about:

https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringUtils.html#isBlank%28java.lang.CharSequence%29

Perhaps our take on _.isBlank() could fallback to _.isEmpty() semantics for non-String types.

enhancement wontfix

Most helpful comment

Oh yeah, empty strings are falsey! I always hated that about strings, and now it's finally useful. ;)

So, just return _.isString(s) ? !!_.trim(s) : _.isEmpty(s);

All 3 comments

I think you're on the right track with _.trim. Usually you want to cleanup your data and then perform checks. In this case if you _.trim your result would end up being falsey for blank strings anyways.

Oh yeah, empty strings are falsey! I always hated that about strings, and now it's finally useful. ;)

So, just return _.isString(s) ? !!_.trim(s) : _.isEmpty(s);

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

toranb picture toranb  路  3Comments

satrong picture satrong  路  3Comments

dengyaolong picture dengyaolong  路  3Comments

aj0strow picture aj0strow  路  3Comments

SameerSiddiqui picture SameerSiddiqui  路  3Comments