Skip to content

Commit 205a1ec

Browse files
committed
Add IO.iodata_empty?/0
1 parent 9721759 commit 205a1ec

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

lib/elixir/lib/io.ex

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,29 @@ defmodule IO do
796796
:erlang.iolist_size(iodata)
797797
end
798798

799+
@doc """
800+
Checks if an IO data (the length is zero).
801+
802+
For more information about IO data, see the ["IO data"](#module-io-data)
803+
section in the module documentation.
804+
805+
## Examples
806+
807+
iex> IO.iodata_empty?([])
808+
true
809+
iex> IO.iodata_empty?([""])
810+
true
811+
iex> IO.iodata_empty?([1, 2 | <<3, 4>>])
812+
false
813+
814+
"""
815+
@doc since: "1.20.0"
816+
@spec iodata_empty?(iodata) :: boolean
817+
def iodata_empty?(""), do: true
818+
def iodata_empty?([]), do: true
819+
def iodata_empty?([head | tail]), do: iodata_empty?(head) and iodata_empty?(tail)
820+
def iodata_empty?(_), do: false
821+
799822
@doc false
800823
def each_stream(device, line_or_codepoints) do
801824
case read(device, line_or_codepoints) do

lib/elixir/test/elixir/io_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,16 @@ defmodule IOTest do
273273
assert IO.binstream() == IO.binstream(:stdio, :line)
274274
end
275275

276+
test "iodata_empty?" do
277+
assert IO.iodata_empty?([])
278+
assert IO.iodata_empty?("")
279+
assert IO.iodata_empty?([["", []] | ""])
280+
281+
refute IO.iodata_empty?([1])
282+
refute IO.iodata_empty?("1")
283+
refute IO.iodata_empty?([["", []] | "ok"])
284+
end
285+
276286
defp assert_emits(output, messages) do
277287
for m <- messages do
278288
assert output =~ m

0 commit comments

Comments
 (0)