constructor v1.1.0 Constructor.Convert

Functions in this module will typically perform a type conversion and then a validation.

Link to this section Summary

Functions

Converts nil to []

Converts a binary to an atom. The same warnings in String.to_atom/1 apply here

Converts v to a boolean, in an opinionated manner

Same as to_boolean/1, except nil will NOT be converted to false

Converts an atom such as :foo to "FOO"

Converts a binary to an existing atom. The same warnings in String.to_atom/1 apply here

Converts v to a float, returning a validation error if it cannot

Same as to_float/1, except that nil will NOT be converted to a float

Convert nil and "" to 0, and attempt to convert floats and binaries to integers

Same as to_integer/1, except nil will NOT be converted to an integer

Converts integers, floats and atoms to an equivalent string representation. nil is converted to ""

Same as to_string/1, except that nil will NOT be converted to a string. ""

Link to this section Types

Link to this type

error()
error() :: {:error, String.t()}

Link to this section Functions

Link to this function

nil_to_list(v)
nil_to_list(any()) :: {:ok, list()} | error()

Converts nil to [].

Link to this function

to_atom(item)
to_atom(any()) :: {:ok, atom()} | error()
to_atom(any()) :: {:ok, atom()} | error()

Converts a binary to an atom. The same warnings in String.to_atom/1 apply here.

Link to this function

to_boolean(v)
to_boolean(any()) :: {:ok, boolean()} | error()

Converts v to a boolean, in an opinionated manner.

In order to be useful, this function makes some assumptions about what is truth-y or false-y.

  • "true" and "false" are converted to true and false respectively. All strings are downcased first.
  • integers and floats greater than 0 are true
  • integers and floats less than or equal to 0 are false
  • nil and "" are false
Link to this function

to_boolean_or_nil(v)
to_boolean_or_nil(any()) :: {:ok, boolean() | nil} | error()

Same as to_boolean/1, except nil will NOT be converted to false

Link to this function

to_enum_string(e)

Converts an atom such as :foo to "FOO".

Link to this function

to_existing_atom(item)

Converts a binary to an existing atom. The same warnings in String.to_atom/1 apply here.

Link to this function

to_float(v)
to_float(any()) :: {:ok, float()} | error()

Converts v to a float, returning a validation error if it cannot.

Link to this function

to_float_or_nil(v)
to_float_or_nil(any()) :: {:ok, float() | nil} | error()

Same as to_float/1, except that nil will NOT be converted to a float.

Link to this function

to_integer(v)
to_integer(any()) :: {:ok, integer()} | error()

Convert nil and "" to 0, and attempt to convert floats and binaries to integers.

Link to this function

to_integer_or_nil(v)
to_integer_or_nil(any()) :: {:ok, integer() | nil} | error()

Same as to_integer/1, except nil will NOT be converted to an integer.

Link to this function

to_string(v)
to_string(any()) :: {:ok, String.t()} | error()

Converts integers, floats and atoms to an equivalent string representation. nil is converted to ""

Link to this function

to_string_or_nil(v)
to_string_or_nil(any()) :: {:ok, String.t() | nil} | error()

Same as to_string/1, except that nil will NOT be converted to a string. ""