IfElse (IfElse v0.1.1)
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}}
conditional-function-calls-in-a-pipeline
Conditional function calls in a pipeline
Only call put_session/3 if session_value is not nil.
conn
|> call_if(session_value, & put_session(&1, :session_key, &2))
|> ...
Link to this section Summary
Functions
Call the two argument function with arg1 and arg2 unless arg2 is
falsy.
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.
If the value is nil or an empty string, return the else_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
call_if(arg1, arg2, function)
@spec call_if( arg1 :: any(), arg2 :: nil | false | String.t() | any(), function :: (any(), any() -> any()) ) :: any()
Call the two argument function with arg1 and arg2 unless arg2 is
falsy.
In case arg2 is a function, call arg2.(arg1). If the result is not empty,
pass the result as the second argument in the call, function.(arg1, arg2_result).
For example, only call put_session/3 if session_value is not nil.
conn
|> call_if(session_value, & put_session(&1, :session_key, &2))
|> ...
coalesce(list)
Return the first truthy element in the list, otherwise return nil.
empty_else(value, else_value)
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.
empty_else(value, not_empty_value, else_value)
@spec empty_else( value :: nil | String.t() | any(), not_empty_value :: (any() -> any()) | any(), else_value :: (() -> any()) | any() ) :: any()
If the value is nil or an empty string, return the else_value.
If else_value is a function, it is called.
Otherwise, return the not_empty_value. If not_empty_value is a function,
it is called.
is_empty?(arg1)
@spec is_empty?(nil | String.t()) :: true | false
Check for empty strings, where empty is nil or "".
put_if(structure, value, path)
If value is truthy, put it in the structure at path and return the updated
structure. Otherwise return the structure unchanged.