IfElse (IfElse v0.1.0)

Functions providing conditional logic.

Interactive Elixir (1.14.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> import IfElse
IfElse
iex(2)> is_empty?("")
true
iex(3)> empty_else("foo", "bar")
"foo"
iex(4)> empty_else("", "bar")
"bar"
iex(5)> empty_else("", fn -> "bar" end)
"bar"
iex(6)> coalesce([false, nil, "true", false])
"true"
iex(7)> put_if(%{one: %{two: :three}}, :four, [:one, :two])
%{one: %{two: :four}}
iex(8)> put_if(%{one: %{two: :three}}, nil, [:one, :two])
%{one: %{two: :three}}

Link to this section Summary

Functions

Call the two argument function with arg1 and arg2 if arg2 is truthy.

Return the first truthy element in the list, otherwise return nil.

If the value is nil or an empty string, return the else_value. Otherwise, return the value.

Check for empty strings, where empty is nil or "".

If value is truthy, put it in the structure at path and return the updated structure. Otherwise return the structure unchanged.

Link to this section Functions

Link to this function

call_if(arg1, arg2, function)

@spec call_if(
  arg1 :: any(),
  arg2 :: nil | false | any(),
  function :: (any(), any() -> any())
) :: any()

Call the two argument function with arg1 and arg2 if arg2 is truthy.

@spec coalesce(list()) :: nil | any()

Return the first truthy element in the list, otherwise return nil.

Link to this function

empty_else(value, else_value)

@spec empty_else(
  value :: nil | String.t() | any(),
  else_value :: (() -> any()) | any()
) :: any()

If the value is nil or an empty string, return the else_value. Otherwise, return the value.

If else_value is a function, it is called.

Link to this function

is_empty?(arg1)

@spec is_empty?(nil | String.t()) :: true | false

Check for empty strings, where empty is nil or "".

Link to this function

put_if(structure, value, path)

@spec put_if(structure :: Access.t(), value :: function() | any(), path :: list()) ::
  Access.t()

If value is truthy, put it in the structure at path and return the updated structure. Otherwise return the structure unchanged.