View Source Flamel (flamel v0.1.0)
Documentation for Flamel
.
Summary
Functions
Checks if something is blank?
Is something a boolean?
Checks if something is present?
Converts to an atom. Warning: This uses String.to_atom
Convert something to a boolean
Converts to a Date
Converts to a DateTime
Converts to a float
Converts to an integer
Converts to a DateTime
Converts to a map
Converts to a string
Turn an exception into an error tuple. This can be used with with
to
catch exceptions and turn them into error tuples.
Takes an {:ok, value} or {:error, message} tuple and returns the value
Takes an {:ok, value} tuple and returns the value
Takes an {:error, value} tuple and returns the value or nil
Takes an {:ok, value} tuple and returns the value
Takes an {:ok, value} tuple and returns the value or nil
Functions
Checks if something is blank?
Examples
iex> Flamel.blank?(nil)
true
iex> Flamel.blank?("hello world")
false
iex> Flamel.blank?(%{})
true
iex> Flamel.blank?(%{active: true})
false
iex> Flamel.blank?([])
true
iex> Flamel.blank?(["one"])
false
iex> Flamel.blank?(" ")
true
iex> Flamel.blank?(0)
false
iex> Flamel.blank?(1)
false
Is something a boolean?
Examples
iex> Flamel.boolean?(true)
true
iex> Flamel.boolean?(false)
true
iex> Flamel.boolean?(nil)
false
iex> Flamel.boolean?("true")
false
Checks if something is present?
Examples
iex> Flamel.present?(nil)
false
iex> Flamel.present?("hello world")
true
iex> Flamel.present?(%{})
false
iex> Flamel.present?(%{active: true})
true
iex> Flamel.present?([])
false
iex> Flamel.present?(["one"])
true
iex> Flamel.present?(" ")
false
iex> Flamel.present?(0)
true
iex> Flamel.present?(1)
true
Converts to an atom. Warning: This uses String.to_atom
Examples
iex> Flamel.to_atom(:test)
:test
iex> Flamel.to_atom("test")
:test
iex> Flamel.to_atom(1)
:"1"
iex> Flamel.to_atom(1.1)
:"1.1"
Convert something to a boolean
Examples
iex> Flamel.to_boolean("Y")
true
iex> Flamel.to_boolean("y")
true
iex> Flamel.to_boolean("YES")
true
iex> Flamel.to_boolean("Yes")
true
iex> Flamel.to_boolean("yes")
true
iex> Flamel.to_boolean("true")
true
iex> Flamel.to_boolean(1)
true
iex> Flamel.to_boolean(true)
true
iex> Flamel.to_boolean("N")
false
iex> Flamel.to_boolean("n")
false
iex> Flamel.to_boolean("NO")
false
iex> Flamel.to_boolean("No")
false
iex> Flamel.to_boolean("no")
false
iex> Flamel.to_boolean("false")
false
iex> Flamel.to_boolean(0)
false
iex> Flamel.to_boolean(false)
false
Converts to a Date
Examples
iex> Flamel.to_date(~D[2000-10-31])
~D[2000-10-31]
iex> Flamel.to_date(%{"day" => "01", "month" => "12", "year" => "2004"})
~D[2004-12-01]
iex> Flamel.to_date("2000-10-31")
~D[2000-10-31]
iex> Flamel.to_date("2000-31-12")
nil
iex> Flamel.to_date(nil)
nil
Converts to a DateTime
Examples
iex> Flamel.to_datetime("2000-10-31T01:30:00.000-05:00")
~U[2000-10-31 06:30:00.000Z]
iex> Flamel.to_datetime(~N[2019-10-31 23:00:07])
~N[2019-10-31 23:00:07]
iex> {:ok, datetime} = DateTime.new(~D[2016-05-24], ~T[13:26:08.003], "Etc/UTC")
iex> Flamel.to_datetime(datetime)
~U[2016-05-24 13:26:08.003Z]
iex> Flamel.to_datetime(nil)
nil
iex> Flamel.to_datetime("badstring")
nil
Converts to a float
Examples
iex> Flamel.to_float("1.2")
1.2
iex> Flamel.to_float(1.2)
1.2
iex> Flamel.to_float(1)
1.0
iex> Flamel.to_float(nil)
0.0
Converts to an integer
Examples
iex> Flamel.to_integer("1")
1
iex> Flamel.to_integer(1)
1
iex> Flamel.to_integer(1.2)
1
iex> Flamel.to_integer(nil)
0
Converts to a DateTime
Examples
iex> Flamel.to_iso8601(~U[2000-10-31 06:30:00.000Z])
"2000-10-31T06:30:00.000Z"
iex> Flamel.to_iso8601(~N[2019-10-31 23:00:07])
"2019-10-31T23:00:07"
iex> Flamel.to_iso8601(~D[2019-10-31])
"2019-10-31"
Converts to a map
Converts to a string
Examples
iex> Flamel.to_string(nil)
""
iex> Flamel.to_string("test")
"test"
iex> Flamel.to_string(:test)
"test"
iex> Flamel.to_string(2.3)
"2.3"
iex> Flamel.to_string(1)
"1"
iex> Flamel.to_string(1)
"1"
Turn an exception into an error tuple. This can be used with with
to
catch exceptions and turn them into error tuples.
Examples
iex> Flamel.try_and_return(fn -> :ok end)
:ok
iex> Flamel.try_and_return(fn -> raise "error" end, {:ok, :default_value})
{:ok, :default_value}
iex> Flamel.try_and_return(fn -> raise "error" end)
{:error, "error"}
Takes an {:ok, value} or {:error, message} tuple and returns the value
Examples
iex> Flamel.unwrap({:ok, []})
[]
iex> Flamel.unwrap({:error, "message"})
"message"
Takes an {:ok, value} tuple and returns the value
Examples
iex> Flamel.unwrap_error!({:ok, []})
** (ArgumentError) {:ok, []} is not an :error tuple
iex> Flamel.unwrap_error!({:error, "message"})
"message"
Takes an {:error, value} tuple and returns the value or nil
Examples
iex> Flamel.unwrap_error_or_nil({:ok, []})
nil
iex> Flamel.unwrap_error_or_nil({:error, "message"})
"message"
Takes an {:ok, value} tuple and returns the value
Examples
iex> Flamel.unwrap_ok!({:ok, []})
[]
iex> Flamel.unwrap_ok!({:error, "message"})
** (ArgumentError) {:error, "message"} is not an :ok tuple
Takes an {:ok, value} tuple and returns the value or nil
Examples
iex> Flamel.unwrap_ok_or_nil({:ok, []})
[]
iex> Flamel.unwrap_ok_or_nil({:error, "message"})
nil