View Source Transmogrify.As (transmogrify v2.0.1)
Polymorphic data transforms — accepts many input forms and tries to put them into the expected form.
Easiest use:
iex> import Transmogrify.As
iex> as_int("10")
{:ok, 10}
iex> as_int!(10.0)
10
Summary
Functions
Accept various data forms and assures they are atoms.
Accept various data forms and assures they are atoms.
Accept various data forms and assures they are atoms (from the existing atoms table). See String.to_existing_atom/1 for more details.
Accept various data forms and assures they are atoms.
Accept strings, floats, and ints, and assure the result is either an float (parsed if necessary) or an error.
Accept strings, floats, and ints, and assure the result is either
an float (parsed if necessary) or return the specified default (which
is 0.0
if unspecified)
Accept strings, floats, and ints, and assure the result is either an int (parsed if necessary) or an error.
Accept strings, floats, and ints, and assure the result is either
an int (parsed if necessary) or return the specified default (which
is 0
if unspecified)
Accept atom or string. If string, run through snakecase and convert to atom. If atom, assume this is already done.
iex> as_kvstr(%{a: "b", c: "10", d: "longer with space", x: :c.pid(0, 999, 999), y: Module, z: nil}) "c=10 a=b d=\"longer with space\" y=Module x=<0.999.999> z="
Accept strings, floats, and ints, and assure the result is either a float or int (parsed if necessary) or an error.
Accept strings, floats, and ints, and assure the result is either
a float or int (parsed if necessary) or return the specified default (which
is 0
if unspecified)
Functions
Accept various data forms and assures they are atoms.
WARNING: Make sure inputs being called are not using user-submitted data; or this may exhaust the atoms table.
iex> as_atom("long ugly thing prolly")
{:ok, :"long ugly thing prolly"}
iex> as_atom("as_atom")
{:ok, :as_atom}
iex> as_atom(:atom)
{:ok, :atom}
iex> as_atom(["as_atom", "another"])
{:ok, [:as_atom, :another]}
iex> as_atom('test')
{:ok, :test}
iex> as_atom(:atom)
{:ok, :atom}
Accept various data forms and assures they are atoms.
WARNING: Make sure inputs being called are not using user-submitted data; or this may exhaust the atoms table.
iex> as_atom!("long ugly thing prolly")
:"long ugly thing prolly"
iex> as_atom!("as_atom")
:as_atom
iex> as_atom!(:atom)
:atom
iex> as_atom!(["as_atom", "another"])
[:as_atom, :another]
iex> as_atom!('test')
:test
iex> as_atom!(:atom)
:atom
iex> as_atom!({:oops})
** (ArgumentError) argument error
Accept various data forms and assures they are atoms (from the existing atoms table). See String.to_existing_atom/1 for more details.
iex> as_existing_atom("long ugly thing prolly")
{:ok, :"long ugly thing prolly"}
iex> as_existing_atom("non_existing_atom")
:error
iex> as_existing_atom("as_existing_atom")
{:ok, :as_existing_atom}
iex> as_existing_atom(:atom)
{:ok, :atom}
iex> as_existing_atom(["as_existing_atom", "another"])
{:ok, [:as_existing_atom, :another]}
iex> as_existing_atom(:atom)
{:ok, :atom}
iex> as_existing_atom('test')
{:ok, :test}
Accept various data forms and assures they are atoms.
WARNING: Make sure inputs being called are not using user-submitted data; or this may exhaust the atoms table.
iex> as_existing_atom!("long ugly thing prolly")
:"long ugly thing prolly"
iex> as_existing_atom!("as_existing_atom")
:as_existing_atom
iex> as_existing_atom!(:atom)
:atom
iex> as_existing_atom!(["as_existing_atom", "another"])
[:as_existing_atom, :another]
iex> as_existing_atom!(:atom)
:atom
iex> as_existing_atom!({:oops})
** (ArgumentError) argument error
iex> as_existing_atom!("non_existing_atom")
** (ArgumentError) argument error
Accept strings, floats, and ints, and assure the result is either an float (parsed if necessary) or an error.
iex> as_float(10.52)
{:ok, 10.52}
iex> as_float(10)
{:ok, 10.0}
iex> as_float("10.5")
{:ok, 10.5}
iex> as_float(".55")
{:ok, 0.55}
iex> as_float(".#")
:error
iex> as_float("tardis")
:error
iex> as_float({})
:error
Accept strings, floats, and ints, and assure the result is either
an float (parsed if necessary) or return the specified default (which
is 0.0
if unspecified)
iex> as_float!(10.0)
10.0
iex> as_float!(10)
10.0
iex> as_float!("10.0")
10.0
iex> as_float!("tardis", 9.0)
9.0
Accept strings, floats, and ints, and assure the result is either an int (parsed if necessary) or an error.
iex> as_int(10)
{:ok, 10}
iex> as_int(10.0)
{:ok, 10}
iex> as_int("10")
{:ok, 10}
iex> as_int("tardis")
:error
iex> as_int(:foo)
:error
Accept strings, floats, and ints, and assure the result is either
an int (parsed if necessary) or return the specified default (which
is 0
if unspecified)
iex> as_int!(10)
10
iex> as_int!(10.0)
10
iex> as_int!("10")
10
iex> as_int!("tardis", 9)
9
iex> as_int!(:foo, 8)
8
Accept atom or string. If string, run through snakecase and convert to atom. If atom, assume this is already done.
iex> as_key("thiskey") :thiskey iex> as_key("this key") :"this key" iex> as_key(:thisKey) :thisKey iex> as_key(:this_key) :this_key
iex> as_kvstr(%{a: "b", c: "10", d: "longer with space", x: :c.pid(0, 999, 999), y: Module, z: nil}) "c=10 a=b d=\"longer with space\" y=Module x=<0.999.999> z="
Accept strings, floats, and ints, and assure the result is either a float or int (parsed if necessary) or an error.
iex> as_number(10.52)
{:ok, 10.52}
iex> as_number(10)
{:ok, 10}
iex> as_number("10.5")
{:ok, 10.5}
iex> as_number(".55")
{:ok, 0.55}
iex> as_number(".#")
:error
iex> as_number("tardis")
:error
iex> as_number({})
:error
Accept strings, floats, and ints, and assure the result is either
a float or int (parsed if necessary) or return the specified default (which
is 0
if unspecified)
iex> as_number!(10.0)
10.0
iex> as_number!(10)
10
iex> as_number!("10.0")
10.0
iex> as_number!("tardis", 9.0)
9.0