cldr_utils v2.13.2 Cldr.Map View Source
Functions for transforming maps, keys and values.
Link to this section Summary
Functions
Transforms a map's String.t keys to atom() keys.
Transforms a map's String.t values to atom() values.
Recursively traverse a map and invoke a function that transforms the mapfor each key/value pair.
Deep merge two maps
Delete all members of a map that have a key in the list of keys
Transforms a map's String.t keys to Float.t values.
Transforms a map's String.t values to Float.t values.
Returns am argument unchanged.
Transforms a map's String.t keys to Integer.t keys.
Transforms a map's String.t values to Integer.t values.
Returns the result of deep merging a list of maps
Removes any leading underscores from map
String.t keys.
Rename map keys from from to to
Transforms a map's atom() keys to String.t keys.
Transforms a map's atom() keys to String.t keys.
Convert a camelCase string or atome to a snake_case
Convert map String.t keys from camelCase to snake_case
Link to this section Functions
Transforms a map's String.t keys to atom() keys.
Arguments
mapis anymap/0optionsis a keyword list of options passed todeep_map/3. One additional option apples to this function directly::only_existingwhich is set totruewill only convert the binary value to an atom if the atom already exists. The default isfalse.
Example
iex> Cldr.Map.atomize_keys %{"a" => %{"b" => %{1 => "c"}}}
%{a: %{b: %{1 => "c"}}} Transforms a map's String.t values to atom() values.
Arguments
mapis anymap/0optionsis a keyword list of options passed todeep_map/3. One additional option apples to this function directly::only_existingwhich is set totruewill only convert the binary value to an atom if the atom already exists. The default isfalse.
Examples
iex> Cldr.Map.atomize_values %{"a" => %{"b" => %{1 => "c"}}}
%{"a" => %{"b" => %{1 => :c}}} deep_map(map, function, options \\ [level: %{__struct__: Range, first: 1, last: 1000000}])
View SourceSpecs
deep_map( map() | list(), function :: function() | {function(), function()}, options :: list() ) :: map() | list()
Recursively traverse a map and invoke a function that transforms the mapfor each key/value pair.
Arguments
mapis anymap/0functionis a1-arityfunction or function reference that is called for each key/value pair of the provided map. It can also be a 2-tuple of the form{key_function, value_function}- In the case where
functionis a single function it will be called with the 2-tuple argument{key, value} - In the case where function is of the form
{key_function, value_function}thekey_functionwill be called with the argumentkeyand the value function will be called with the argumentvalue
- In the case where
optionsis a keyword list of options. The default is[]
Options
:levelindicates the starting (and optionally ending) levels of the map at which thefunctionis executed. This can be an integer representing onelevelor a range indicating a range of levels. The default is1..1000000:onlyis a term or list of terms or acheck function. If it is a term or list of terms, thefunctionis only called if thekeyof the map is equal to the term or in the list of terms. If:onlyis acheck functionthen thecheck functionis passed the{k, v}of the current branch in themap. It is expected to return atruthyvalue that iftruesignals that the argumentfunctionwill be executed.:exceptis a term or list of terms or acheck function. If it is a term or list of terms, thefunctionis only called if thekeyof the map is not equal to the term or not in the list of terms. If:exceptis acheck functionthen thecheck functionis passed the{k, v}of the current branch in themap. It is expected to return atruthyvalue that iftruesignals that the argumentfunctionwill not be executed.
Notes
If both the options :only and :except are provided then the function
is called only when a term meets both criteria.
Returns
- The
maptransformed by the recursive application offunction
Examples
iex> map = %{a: :a, b: %{c: :c}}
iex> fun = fn
...> {k, v} when is_atom(k) -> {Atom.to_string(k), v}
...> other -> other
...> end
iex> Cldr.Map.deep_map map, fun
%{"a" => :a, "b" => %{"c" => :c}}
iex> map = %{a: :a, b: %{c: :c}}
iex> Cldr.Map.deep_map map, fun, only: :c
%{a: :a, b: %{"c" => :c}}
iex> Cldr.Map.deep_map map, fun, except: [:a, :b]
%{a: :a, b: %{"c" => :c}}
iex> Cldr.Map.deep_map map, fun, level: 2
%{a: :a, b: %{"c" => :c}} Deep merge two maps
Examples
iex> Cldr.Map.deep_merge %{a: "a", b: "b"}, %{c: "c", d: "d"}
%{a: "a", b: "b", c: "c", d: "d"}
iex> Cldr.Map.deep_merge %{a: "a", b: "b"}, %{c: "c", d: "d", a: "aa"}
%{a: "aa", b: "b", c: "c", d: "d"} Delete all members of a map that have a key in the list of keys
Examples
iex> Cldr.Map.delete_in %{a: "a", b: "b"}, [:a]
%{b: "b"} Transforms a map's String.t keys to Float.t values.
Arguments
mapis anymap/0optionsis a keyword list of options passed todeep_map/3
The map key is converted to a float from
a String.t only when the key is comprised of
a valid float form.
Keys which cannot be converted to a float
are returned unchanged.
Examples
iex> Cldr.Map.floatize_keys %{a: %{"1.0" => "value"}}
%{a: %{1.0 => "value"}}
iex> Cldr.Map.floatize_keys %{a: %{"1" => "value"}}
%{a: %{1.0 => "value"}} Transforms a map's String.t values to Float.t values.
Arguments
mapis anymap/0optionsis a keyword list of options passed todeep_map/3
The map value is converted to a float from
a String.t only when the
value is comprised of a valid float form.
Values which cannot be converted to a float
are returned unchanged.
Examples
iex> Cldr.Map.floatize_values %{a: %{b: "1.0"}}
%{a: %{b: 1.0}}
iex> Cldr.Map.floatize_values %{a: %{b: "1"}}
%{a: %{b: 1.0}} Returns am argument unchanged.
Useful when a noop function is required.
Transforms a map's String.t keys to Integer.t keys.
Arguments
mapis anymap/0optionsis a keyword list of options passed todeep_map/3
The map key is converted to an integer from
either an atom or String.t only when the
key is comprised of integer digits.
Keys which cannot be converted to an integer
are returned unchanged.
Example
iex> Cldr.Map.integerize_keys %{a: %{"1" => "value"}}
%{a: %{1 => "value"}} Transforms a map's String.t values to Integer.t values.
Arguments
mapis anymap/0optionsis a keyword list of options passed todeep_map/3
The map value is converted to an integer from
either an atom or String.t only when the
value is comprised of integer digits.
Keys which cannot be converted to an integer are returned unchanged.
Example
iex> Cldr.Map.integerize_values %{a: %{b: "1"}}
%{a: %{b: 1}} Returns the result of deep merging a list of maps
Examples
iex> Cldr.Map.merge_map_list [%{a: "a", b: "b"}, %{c: "c", d: "d"}]
%{a: "a", b: "b", c: "c", d: "d"} Removes any leading underscores from map
String.t keys.
mapis anymap/0optionsis a keyword list of options passed todeep_map/3
Examples
iex> Cldr.Map.remove_leading_underscores %{"a" => %{"_b" => "b"}}
%{"a" => %{"b" => "b"}} Rename map keys from from to to
mapis anymap/0fromis any value map keytois any valid map keyoptionsis a keyword list of options passed todeep_map/3
Example
iex> Cldr.Map.rename_keys %{"a" => %{"this_one" => "value"}}, "this_one", "that_one"
%{"a" => %{"that_one" => "value"}} Transforms a map's atom() keys to String.t keys.
Arguments
mapis anymap/0optionsis a keyword list of options passed todeep_map/3
Example
iex> Cldr.Map.stringify_keys %{a: %{"1" => "value"}}
%{"a" => %{"1" => "value"}} Transforms a map's atom() keys to String.t keys.
Arguments
mapis anymap/0optionsis a keyword list of options passed todeep_map/3
Example
iex> Cldr.Map.stringify_values %{a: %{"1" => :value}}
%{a: %{"1" => "value"}} Specs
Convert a camelCase string or atome to a snake_case
stringis aString.toratom()to be transformed
This is the code of Macro.underscore with modifications. The change is to cater for strings in the format:
This_That
which in Macro.underscore gets formatted as
this__that (note the double underscore)
when we actually want
that_that
Examples
iex> Cldr.Map.underscore "thisThat"
"this_that"
iex> Cldr.Map.underscore "This_That"
"this_that" Convert map String.t keys from camelCase to snake_case
mapis anymap/0optionsis a keyword list of options passed todeep_map/3
Example
iex> Cldr.Map.underscore_keys %{"a" => %{"thisOne" => "value"}}
%{"a" => %{"this_one" => "value"}}