nesty v0.1.0 Nesty

Convenient helpers when dealing with nested keywords and maps.

Link to this section Summary

Functions

Get the value deeply nested at the specified key path

Link to this section Types

Link to this type default()
default() :: any

Link to this section Functions

Link to this function get(data, keys, default \\ nil)
get(any, [key | {key, default}, ...], default) ::
  value |
  default

Get the value deeply nested at the specified key path.

If a key does not exist, a default value will be returned (nil if one is not provided). A default value can be applied on a per key and general basis. Per key defaults take precedence, whereas if a per key default does not exist for the given key, it will then fallback to using the general default value.

The general default value is by default nil, but can be set by changing the third argument of the function.

Per key default values can be set by including a named tuple in the key list { key, default }, as opposed to the regular key entry when no per key default is required.

Example

iex> Nesty.get([a: [b: %{ c: 55 }]], [:a, :b, :c])
55

iex> Nesty.get([a: [b: %{ c: 55 }]], [:a, :d])
nil

iex> Nesty.get([a: [b: %{ c: 55 }]], [:a, :d], 123)
123

iex> Nesty.get([a: [b: %{ c: 55 }]], [:a, { :d, 1000 }, :c])
1000