ex_commons v0.1.1 ExCommons

Link to this section Summary

Functions

Deconstructs tuples to return their output, will raise on error and on unknown tuples

Drops all mime types from a string

Converts nil to any value, defaults to 0. Can iterate over lists, and maps, and keyword lists (also handles tuples). It will change only values of the enumerable/tuple types

Transforms module name to key, useful for polymorphism in ecto associations

Link to this section Functions

Link to this function deconstruct(arg)

Deconstructs tuples to return their output, will raise on error and on unknown tuples.

Examples

iex> ExCommons.deconstruct({:ok, "Output"})
"Output"

iex> ExCommons.deconstruct({:safe, "Safe"})
"Safe"

iex> ExCommons.deconstruct({:error, %{message: "Error"}})
** (RuntimeError) Error

iex> ExCommons.deconstruct({:unknown, nil})
** (RuntimeError) Can't handle :unknown.
Link to this function drop_mimes(str)
drop_mimes(String.t()) :: String.t()

Drops all mime types from a string

Examples

iex> ExCommons.drop_mimes(“file_name.pdf”) “file_name”

iex> ExCommons.drop_mimes(“file.with.three.mimes”) “file”

Link to this function escape_nil(n, x \\ 0)

Converts nil to any value, defaults to 0. Can iterate over lists, and maps, and keyword lists (also handles tuples). It will change only values of the enumerable/tuple types.

Examples

iex> ExCommons.escape_nil(nil) 0

iex> ExCommons.escape_nil(nil, “”) “”

iex> ExCommons.escape_nil(123) 123

iex> ExCommons.escape_nil([1.23, 456, nil]) [1.23, 456, 0]

iex> ExCommons.escape_nil([a: “a”, b: “b”, c: nil], “c”) [a: “a”, b: “b”, c: “c”]

## Warning: it will reorder the map. iex> ExCommons.escape_nil(%{c: nil, a: “a”, b: “b”}, “c”) %{a: “a”, b: “b”, c: “c”}

Link to this function module_to_key(module)

Transforms module name to key, useful for polymorphism in ecto associations.

Examples

iex> ExCommons.module_to_key(Some.ModuleName)
:module_name

iex> ExCommons.module_to_key("SomeString")
{:error, %{message: "SomeString is not module name"}}