PassiveSupport.Map (passive_support v0.8.4)
Convenience functions for working with maps.
Link to this section Summary
Functions
Returns map
with its keys as atoms, if those atoms already exist.
Returns a new map with key
replaced by new_key
or the return of fun
Deletes the struct metadata from structs, but leaves ordinary maps unchanged
Returns a map
with any string keys that safely coerced to existing atoms
Returns a version of map
whose keys have all been
converted to strings.
Returns a copy of map
containing only keys
and raises
if any are missing.
Link to this section Types
key()
Specs
key() :: any()
Link to this section Functions
atomize_keys!(map)
Returns map
with its keys as atoms, if those atoms already exist.
Raises ArgumentError
otherwise.
Examples
iex> [:oh, :yay] # existing keys
iex> atomize_keys!(%{"oh" => "ooh", 'yay' => 'yaaay'})
%{oh: "ooh", yay: 'yaaay'}
iex> atomize_keys!(%{"oh" => "ooh", "noo" => "noooo"})
** (PassiveSupport.NonexistentAtoms) No atoms exist for the values ["noo"]
change_key(map, key, fun)
Specs
Returns a new map with key
replaced by new_key
or the return of fun
If key
is not found within map
, returns the map
unaltered.
Useful for when you're shuffling values around inside of a map, or, I dunno, going through your music collection and you discover you accidentally attributed an entire Beatles album to the Monkees.
Although how you did that is beyond me. You monster.
Examples
iex> change_key(%{dog: "rusty"}, :dog, :cat)
%{cat: "rusty"}
iex> change_key(%{dog: "rusty"}, :dog, &(:"best_#{&1}"))
%{best_dog: "rusty"}
iex> change_key(%{1 => "foo", 2 => "bar", 5 => "idklol"}, 1, fn _key, map -> Enum.max(Map.keys(map)) + 1 end)
%{2 => "bar", 5 => "idklol", 6 => "foo"}
iex> change_key(%{cake_boss: "you blew it"}, :no_key_here, :oops)
%{cake_boss: "you blew it"}
plain(struct)
Specs
Deletes the struct metadata from structs, but leaves ordinary maps unchanged
Examples
PassiveSupport.Map.plain(%{foo: :bar})
# => %{foo: :bar}
defmodule Plane do
defstruct plains: :great # ... little plain punning for ya.
end
PassiveSupport.Map.plain(%Plane{})
# => %{plains: :great}
safe_atomize_keys(map)
Returns a map
with any string keys that safely coerced to existing atoms
I'm not giving y'all the ridiculously dangerous footgun of
wanton atom space pollution. I'm not some crazy, foot-shooting
cowboy, like Jason
.
Examples
iex> [:oh, :yay] # existing keys
iex> safe_atomize_keys(%{"oh" => "ooh", 'yay' => 'yaaay'})
%{oh: "ooh", yay: 'yaaay'}
iex> safe_atomize_keys(%{"oh" => "ooh", "noo" => "noooo"})
%{oh: "ooh"}
stringify_keys(map)
Returns a version of map
whose keys have all been
converted to strings.
Examples
iex> stringify_keys(%{7 => 8, "hey" => "ha", hi: "ho"})
%{"7" => 8, "hey" => "ha", "hi" => "ho"}
take!(map, keys \\ [])
Specs
Returns a copy of map
containing only keys
and raises
if any are missing.
If keys
are not provided, returns map
unchanged
Examples
iex> take!(%{a: "foo", b: 42, c: :ok}, [:b, :c])
%{b: 42, c: :ok}
iex> take!(%{a: "foo", b: 42})
%{a: "foo", b: 42}
iex> take!(%{"a" => "foo", "b" => 42, "c" => :ok}, ["c", "e"])
** (PassiveSupport.KeysNotFound) Expected to find keys ["c", "e"] but only found keys ["c"]