chore: extraction and centralization of utility functions
This commit is contained in:
24
lib/bds/map_utils.ex
Normal file
24
lib/bds/map_utils.ex
Normal file
@@ -0,0 +1,24 @@
|
||||
defmodule BDS.MapUtils do
|
||||
@moduledoc false
|
||||
|
||||
@typedoc "An attribute map that may use atom or string keys."
|
||||
@type attrs :: %{optional(atom()) => term(), optional(String.t()) => term()}
|
||||
|
||||
@spec attr(attrs(), atom()) :: term()
|
||||
def attr(attrs, key) do
|
||||
cond do
|
||||
Map.has_key?(attrs, key) -> Map.get(attrs, key)
|
||||
Map.has_key?(attrs, Atom.to_string(key)) -> Map.get(attrs, Atom.to_string(key))
|
||||
true -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@spec maybe_put(map(), term(), term()) :: map()
|
||||
def maybe_put(map, _key, nil), do: map
|
||||
def maybe_put(map, key, value), do: Map.put(map, key, value)
|
||||
|
||||
@spec blank_to_nil(term()) :: term()
|
||||
def blank_to_nil(nil), do: nil
|
||||
def blank_to_nil(""), do: nil
|
||||
def blank_to_nil(value), do: value
|
||||
end
|
||||
Reference in New Issue
Block a user