chaperon v0.1.0 Chaperon.Util View Source

Helper functions used throughout Chaperon’s codebase.

Link to this section Summary

Functions

Converts a given value to a list. If given a list, simply returns the list. Otherwise wraps the given value in a list

Returns last amount elements in a given Enum as a List

Inserts a given key-value pair ({k2, v2} under any values within map that are also maps)

Converts a map’s values to be prefixed (put in a tuple as the first element)

Link to this section Functions

Link to this function as_list(l) View Source
as_list(any) :: [any]

Converts a given value to a list. If given a list, simply returns the list. Otherwise wraps the given value in a list.

Examples

iex> Chaperon.Util.as_list([1,2,3])
[1,2,3]
iex> Chaperon.Util.as_list(nil)
[]
iex> Chaperon.Util.as_list(1)
[1]
iex> Chaperon.Util.as_list("foo")
["foo"]

Returns last amount elements in a given Enum as a List.

Example

iex> alias Chaperon.Util
iex> [] |> Util.last(1)
[]
iex> [1] |> Util.last(1)
[1]
iex> [1,2,3,4] |> Util.last(1)
[4]
iex> [1,2,3,4] |> Util.last(2)
[3,4]
iex> [1,2,3,4] |> Util.last(3)
[2,3,4]
iex> [1,2,3,4] |> Util.last(4)
[1,2,3,4]
iex> [1,2,3,4] |> Util.last(5)
[1,2,3,4]
Link to this function map_nested_put(map, k2, v2) View Source
map_nested_put(map, any, any) :: map

Inserts a given key-value pair ({k2, v2} under any values within map that are also maps).

Example

iex> m = %{a: 1, b: %{baz: 3}, c: %{foo: 1, bar: 2}}
iex> Chaperon.Util.map_nested_put(m, :baz, 10)
%{a: 1, b: %{baz: 10}, c: %{foo: 1, bar: 2, baz: 10}}
iex> Chaperon.Util.map_nested_put(m, :foo, "ok")
%{a: 1, b: %{baz: 3, foo: "ok"}, c: %{foo: "ok", bar: 2}}
Link to this function map_prefix_value(map, prefix) View Source
map_prefix_value(map, any) :: map

Converts a map’s values to be prefixed (put in a tuple as the first element).

Examples

iex> Chaperon.Util.map_prefix_value(%{foo: 1, bar: 2}, :wat)
%{foo: {:wat, 1}, bar: {:wat, 2}}
Link to this function preserve_vals_merge(map1, map2) View Source
preserve_vals_merge(map, map) :: map
Link to this function shortened_module_name(mod) View Source