Digger is a collection of helpers to help you operate on nested maps (it will also work on non-nested maps). Elixir gives you a great collection of map methods, but sometimes I find myself having to rewrite the same operations specifically for nested maps.
This library is intended to help your code stay DRY.
This library is a work in progress. Lists are already supported throughout; tuple support (e.g. transforming the payload inside {:ok, _}/{:error, _} while leaving the tag alone) is planned next.
You have a valid data type that needs to be "atomized" This can be a string, number, or nested map with string keys.
Digger.atomize/2 drills down into a nested map and converts (most) keys which are not atoms into atoms.
By default, Digger.atomize/2 creates a new atom for every string it doesn't
already have an atom for. Atoms are never garbage collected, so calling
atomize on untrusted/external input (e.g. a JSON payload from a client) can
exhaust the atom table over time.
Pass existing: true to only atomize strings that already have a matching
atom, and leave anything else as the original string:
Digger.atomize(%{"ok" => 1, "some_string_with_no_atom" => 2}, existing: true)
# => %{ok: 1, "some_string_with_no_atom" => 2}You have a nested map (or a struct that you converted to a nested map) with keys that are atoms and you want to convert those keys to strings.
You have a valid data type that needs to be "stringified" This can be an atom, number, or nested map with atom keys.
Digger.stringify/2 drills down into a nested map and converts (most) keys which are not strings into strings.
You have a valid data type that needs to be camel-cased. This can be an atom, string, or nested map (or a struct that you converted to a nested map) with string or atom keys.
Digger.camel_case/2 "camel cases" your valid data type according to the rules defined by its protocol.
You have a valid data type that needs to be snake-cased. This can be an atom, string, or nested map (or a struct that you converted to a nested map) with string or atom keys.
Digger.snake_case/2 "snake cases" your valid data type according to the rules defined by its protocol.
You have a valid data type that needs the underscores transformed to dashes. This can be a string, or nested map (or a struct that you converted to a nested map) with string keys.
Digger.dasherize/2 "dasherizes" your valid data type according to the rules defined by its protocol.
You have a valid data type that needs the first letter to be lowercase. This can be a string or atom or nested map with string or atom keys.
Digger.lowercase_first/2 "lowercases" your valid data type according to the rules defined by its protocol.
You have a valid data type that needs the first letter to be upper case. This can be a string or atom or nested map with string or atom keys.
Digger.upcase_first/2 "upcases" your valid data type according to the rules defined by its protocol.
The tests in the test folder show you the data types that are handled. The protocol implementations in lib/impl also show you the various data types are handled.
If available in Hex, the package can be installed
by adding digger to your list of dependencies in mix.exs:
def deps do
[
{:digger, "~> 3.1.0"}
]
endDocumentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/digger.