Useful (useful v1.14.0)
A Library of Useful
functions for building Elixir
Apps.
Summary
Functions
atomize_map_keys/1
converts a Map
with different keys
to a map with just atom keys. Works recursively for nested maps.
Inspired by stackoverflow.com/questions/31990134
empty_dir_contents/1
delete all files including any directories
recursively in a dir but not the dir itself.
flatten_map/1
flattens a Map
of any depth/nesting for easier processing.
Deeply nested maps are denoted by "" (double underscore) e.g:
%{name: Alex, detail: %{age: 17}}
becomes `%{name: Alex, detailage: 17}`
this makes it easy to see what the data structure was before flattening.
Map keys are converted to Atom for simpler access and consistency.
Inspired by: https://stackoverflow.com/questions/39401947/flatten-nested-map
get_in_default/3
Proxies Kernel.get_in/2
but allows setting a default
value as the 3rd argument.
list_tuples_to_unique_keys/1
turns a list of tuples
with the same key into a list of tuples with unique keys.
Useful when dealing with "multipart" forms that upload multiple files.
remove_item_from_list/2
removes a given item
from a list
in any position.
stringy_map/1
converts a Map
of any depth/nesting into a string.
Deeply nested maps are denoted by "__" (double underscore). See flatten_map/1
for more details.
Alphabetizes the keys for consistency. See: github.com/dwyl/useful/issues/56
stringify_tuple/1
stringifies a Tuple
with arbitrary values.
Handy when you want to print out a tuple during debugging.
Functions
atomize_map_keys(value)
atomize_map_keys/1
converts a Map
with different keys
to a map with just atom keys. Works recursively for nested maps.
Inspired by stackoverflow.com/questions/31990134
Examples
iex> Useful.atomize_map_keys(%{"name" => "alex", id: 1})
%{id: 1, name: "alex"}
iex> Useful.atomize_map_keys(%{"name" => "alex", data: %{ "age" => 17}})
%{name: "alex", data: %{age: 17}}
empty_dir_contents(dir)
empty_dir_contents/1
delete all files including any directories
recursively in a dir but not the dir itself.
Very happy for anyone to refactor this function to something pretty.
flatten_map(map)
flatten_map/1
flattens a Map
of any depth/nesting for easier processing.
Deeply nested maps are denoted by "" (double underscore) e.g:
%{name: Alex, detail: %{age: 17}}
becomes `%{name: Alex, detailage: 17}`
this makes it easy to see what the data structure was before flattening.
Map keys are converted to Atom for simpler access and consistency.
Inspired by: https://stackoverflow.com/questions/39401947/flatten-nested-map
Examples
iex> map = %{name: "alex", data: %{age: 17, height: 185}}
iex> Useful.flatten_map(map)
%{data__age: 17, data__height: 185, name: "alex"}
get_in_default(map, keys, default \\ nil)
get_in_default/3
Proxies Kernel.get_in/2
but allows setting a default
value as the 3rd argument.
Examples
iex> map = %{name: "alex", data: %{age: 17, height: 185}}
iex> Useful.get_in_default(map, [:data, :age])
17
iex> Useful.get_in_default(map, [:data, :iq], 180)
180
iex> Useful.get_in_default(nil, [:unhappy, :path], "Happy!")
"Happy!"
list_tuples_to_unique_keys(parts)
list_tuples_to_unique_keys/1
turns a list of tuples
with the same key into a list of tuples with unique keys.
Useful when dealing with "multipart" forms that upload multiple files.
Example
iex> parts = [{"file", "header", "pic1.png"}, {"file", "header", "pic2.png"}]
iex> Useful.list_tuples_to_unique_keys(parts)
[{"file-1", "header", "pic1.png"}, {"file-2", "header", "pic2.png"}]
remove_item_from_list(list, item)
remove_item_from_list/2
removes a given item
from a list
in any position.
Examples
iex> list = ["They'll", "never", "take", "our", "freedom!"]
iex> Useful.remove_item_from_list(list, "never")
["They'll", "take", "our", "freedom!"]
stringify_map(map)
stringy_map/1
converts a Map
of any depth/nesting into a string.
Deeply nested maps are denoted by "__" (double underscore). See flatten_map/1
for more details.
Alphabetizes the keys for consistency. See: github.com/dwyl/useful/issues/56
Examples
iex> map = %{name: "alex", data: %{age: 17, height: 185}}
iex> Useful.stringify_map(map)
"data__age: 17, data__height: 185, name: alex"
stringify_tuple(arg)
stringify_tuple/1
stringifies a Tuple
with arbitrary values.
Handy when you want to print out a tuple during debugging.
Examples
iex> tuple = {:name, "alex"}
iex> Useful.stringify_tuple(tuple)
"name: alex"
typeof(x)
typeof/1
returns the type of a vairable.
Inspired by stackoverflow.com/questions/28377135/check-typeof-variable-in-elixir
Examples
iex> Useful.typeof(:atom)
"atom"
iex> bin = "hello"
iex> Useful.typeof(bin)
"binary"
iex> bitstr = <<1::3>>
iex> Useful.typeof(bitstr)
"bitstring"
iex> Useful.typeof(:true)
"boolean"
iex> pi = 3.14159
iex> Useful.typeof(pi)
"float"
iex> fun = fn (a, b) -> a + b end
iex> Useful.typeof(fun)
"function"
iex> Useful.typeof(&Useful.typeof/1)
"function"
iex> int = 42
iex> Useful.typeof(int)
"integer"
iex> list = [1,2,3,4]
iex> Useful.typeof(list)
"list"
iex> map = %{:foo => "bar", "hello" => :world}
iex> Useful.typeof(map)
"map"
iex> Useful.typeof(nil)
"nil"
iex> pid = spawn(fn -> 1 + 2 end)
iex> Useful.typeof(pid)
"pid"
iex> port = Port.open({:spawn, "cat"}, [:binary])
iex> Useful.typeof(port)
"port"
iex> ref = :erlang.make_ref
iex> Useful.typeof(ref)
"reference"
iex> tuple = {:name, "alex"}
iex> Useful.typeof(tuple)
"tuple"