View Source MalanUtils (malan_utils v0.1.0)

Link to this section Summary

Types

A hex-encoded UUID string.

A raw binary representation of a UUID.

Functions

Macro that makes a function public in test, private in non-test

Runs IO.inspect/2 with pretty printing, colors, and unlimited size.

Retrieve syntax colors for embedding into :syntax_colors of Inspect.Opts

Quick regex check to see if the supplied string is a valid UUID

Convert a list to a String, suitable for printing

Takes a list of strings or atoms and returns a list with string and atoms.

Convert a map with String keys into a map with Atom keys.

Convert a map with String keys into a map with Atom keys.

Convert a map to a String, suitable for printing.

Takes a map and a list of keys whose values should be masked

Replaces the caracters in str with asterisks "*", thus "masking" the value.

Checks if the passed item is nil or empty string.

Easy drop-in to a pipe to inspect the return value of the previous function.

if value (value of the argument) is nil, this will raise MalanUtils.CantBeNil

if value (value of the argument) is nil, this will raise MalanUtils.CantBeNil

Converts a struct to a regular map by deleting the :__meta__ key

Convert the value, map, or list to a string, suitable for printing or storing.

Convert a tuple to a String, suitable for printing

Generates a new random UUIDv4

Link to this section Types

@type uuid() :: <<_::288>>

A hex-encoded UUID string.

@type uuid_raw() :: <<_::128>>

A raw binary representation of a UUID.

Link to this section Functions

Link to this macro

defp_testable(head, body \\ nil)

View Source (macro)

Macro that makes a function public in test, private in non-test

See: https://stackoverflow.com/a/47598190/2062384

Link to this function

inspect(val, opaque_struct \\ true, limit \\ 50)

View Source

Runs IO.inspect/2 with pretty printing, colors, and unlimited size.

If opaque_struct is false, then structs will be printed as Maps, which allows you to see any opaque fields they might have set

Link to this function

inspect_format(opaque_struct \\ true, limit \\ 50)

View Source

Get Inspect.Opts for Kernel.inspect or IO.inspect

If opaque_struct is false, then structs will be printed as Maps, which allows you to see any opaque fields they might have set

limit is the max number of stuff printed out. Can be an integer or :infinity

Retrieve syntax colors for embedding into :syntax_colors of Inspect.Opts

You probably don't want this directly. You probably want inspect_format

Quick regex check to see if the supplied string is a valid UUID

Check is done by simple regular expression and is not overly sophisticated.

Return true || false

examples

Examples

iex> MalanUtils.is_uuid?(nil)
false
iex> MalanUtils.is_uuid?("hello world")
false
iex> MalanUtils.is_uuid?("4c2fd8d3-a6e3-4e4b-a2ce-3f21456eeb85")
true
Link to this function

list_to_string(list, mask_keys \\ [])

View Source
@spec list_to_string(list :: list() | String.Chars.t(), mask_keys :: [binary()]) ::
  binary()

Convert a list to a String, suitable for printing

Will raise a String.chars error if can't coerce part to a String

mask_keys is used to mask the values in any keys that are in maps in the list

Link to this function

list_to_strings_and_atoms(list)

View Source

Takes a list of strings or atoms and returns a list with string and atoms.

examples

Examples

iex> list_to_strings_and_atoms([:circle])
[:circle, "circle"]

iex> list_to_strings_and_atoms([:circle, :square])
[:square, "square", :circle, "circle"]

iex> list_to_strings_and_atoms(["circle", "square"])
["square", :square, "circle", :circle]
Link to this function

map_atom_keys_to_strings(map)

View Source

Convert a map with String keys into a map with Atom keys.

examples

Examples

iex> MalanUtils.map_atom_keys_to_strings(%{one: "one", two: "two"})
%{"one" => "one", "two" => "two"}
Link to this function

map_string_keys_to_atoms(map)

View Source

Convert a map with String keys into a map with Atom keys.

examples

Examples

iex> MalanUtils.map_string_keys_to_atoms(%{"one" => "one", "two" => "two"})
%{one: "one", two: "two"}m
Link to this function

map_to_string(map, mask_keys \\ [])

View Source
@spec map_to_string(map :: map() | String.Chars.t(), mask_keys :: [binary()]) ::
  binary()

Convert a map to a String, suitable for printing.

Optionally pass a list of keys to mask.

examples

Examples

iex> map_to_string(%{michael: "knight"})
"michael: 'knight'"

iex> map_to_string(%{michael: "knight", kitt: "karr"})
"kitt: 'karr', michael: 'knight'"

iex> map_to_string(%{michael: "knight", kitt: "karr"}, [:kitt])
"kitt: '****', michael: 'knight'"

iex> map_to_string(%{michael: "knight", kitt: "karr"}, [:kitt, :michael])
"kitt: '****', michael: '******'"

iex> map_to_string(%{"michael" => "knight", "kitt" => "karr", "carr" => "hart"}, ["kitt", "michael"])
"carr: 'hart', kitt: '****', michael: '******'"
Link to this function

mask_map_key_values(map, mask_keys)

View Source

Takes a map and a list of keys whose values should be masked

examples

Examples

iex> MalanUtils.mask_map_key_values(%{name: "Ben, title: "Lord"}, [:title])
%{name: "Ben", title: "****"}

iex> MalanUtils.mask_map_key_values(%{name: "Ben, age: 39}, [:age])
%{name: "Ben", age: "**"}

Replaces the caracters in str with asterisks "*", thus "masking" the value.

If argument is nil nothing will change nil will be returned. If argument is not a binary(), it will be coerced to a binary then masked.

Link to this function

nil_or_empty?(str_or_nil)

View Source

Checks if the passed item is nil or empty string.

The param will be passed to Kernel.to_string() and then String.trim() and checked for empty string

examples

Examples

iex> MalanUtils.nil_or_empty?("hello")
false
iex> MalanUtils.nil_or_empty?("")
true
iex> MalanUtils.nil_or_empty?(nil)
true
Link to this function

not_nil_or_empty?(str_or_nil)

View Source
Link to this function

pry_pipe(retval, arg1 \\ nil, arg2 \\ nil, arg3 \\ nil, arg4 \\ nil)

View Source

Easy drop-in to a pipe to inspect the return value of the previous function.

examples

Examples

conn
|> put_status(:not_found)
|> put_view(MalanWeb.ErrorView)
|> render(:"404")
|> pry_pipe()

alternatives

Alternatives

You may also wish to consider using IO.inspect/3 in pipelines. IO.inspect/3 will print and return the value unchanged. Example:

conn
|> put_status(:not_found)
|> IO.inspect(label: "after status")
|> render(:"404")

if value (value of the argument) is nil, this will raise MalanUtils.CantBeNil

argn (name of the argument) will be passed to allow for more helpful error messages that tell you the name of the variable that was nil

examples

Examples

iex> MalanUtils.raise_if_nil!("someval")
"someval"
iex> MalanUtils.raise_if_nil!(nil)
** (MalanUtils.CantBeNil) variable 'somevar' was nil but cannot be
    (malan 0.1.0) lib/malan/utils.ex:142: MalanUtils.raise_if_nil!/1
Link to this function

raise_if_nil!(varname, value)

View Source

if value (value of the argument) is nil, this will raise MalanUtils.CantBeNil

argn (name of the argument) will be passed to allow for more helpful error messages that tell you the name of the variable that was nil

examples

Examples

iex> MalanUtils.raise_if_nil!("somevar", "someval")
"someval"
iex> MalanUtils.raise_if_nil!("somevar", nil)
** (MalanUtils.CantBeNil) variable 'somevar' was nil but cannot be
    (malan 0.1.0) lib/malan/utils.ex:135: MalanUtils.raise_if_nil!/2
Link to this function

struct_to_map(struct, mask_keys \\ [])

View Source

Converts a struct to a regular map by deleting the :__meta__ key

examples

Examples

MalanUtils.struct_to_map(%Something{hello: "world"})
%{hello: "world"}
Link to this function

to_string(value, mask_keys \\ [])

View Source
@spec to_string(input :: map() | list() | String.Chars.t(), mask_keys :: [binary()]) ::
  binary()

Convert the value, map, or list to a string, suitable for printing or storing.

If the value is not a map or list, it must be a type that implements the String.Chars protocol, otherwise this will fail.

The reason to offer this util function rather than implementing String.Chars for maps and lists is that we want to make sure that we never accidentally convert those to a string. This conversion is somewhat destructive and is irreversable, so it should only be done intentionally.

Link to this function

trunc_str(str, length \\ 255)

View Source
Link to this function

tuple_to_string(tuple, mask_keys \\ [])

View Source
@spec tuple_to_string(tuple :: tuple() | String.Chars.t(), mask_keys :: [binary()]) ::
  binary()

Convert a tuple to a String, suitable for printing

Will raise a String.chars error if can't coerce part to a String

mask_keys is used to mask the values in any keys that are in maps in the tuple

Generates a new random UUIDv4

examples

Examples

MalanUtils.uuidgen()
"4c2fd8d3-a6e3-4e4b-a2ce-3f21456eeb85"