How to check if a string is blank in Elixir
There is String.strip/1 which will convert your 3 examples to ""
which you can compare against.
iex(4)> String.strip("\n") == ""
true
iex(5)> String.strip("") == ""
true
iex(6)> String.strip(" ") == ""
true
There was an issue about it https://github.com/elixir-lang/elixir/pull/2707
String.trim/1 seems to do the trick as of Elixir 1.3.0.
strip
still works, but it was soft deprecated in the 1.3.0 release and it isn't listed in the String docs.
I published a tiny library to do this properly for any data type. It implements identical behaviour as Rails' blank?
method in Elixir as far as that's possible.
Library is here: https://github.com/samphilipd/blankable
To install, add blankable to your list of dependencies in mix.exs:
def deps do
[{:blankable, "~> 0.0.1"}]
end