cldr_utils v2.0.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 for each key/ value pair that transforms the map

Recursively traverse a map and invoke a function for each key and a function for each value that transform the map

Deep merge two maps

Delete all members of a map that have a key in the list of keys

Transforms a map’s values to Float.t values

Transforms a map’s keys to Integer.t keys

Transforms a map’s values to Integer.t values

Returns the result of deep merging a list of maps

Removes any leading underscores from map keys

Rename map keys from from to to

Transforms a map’s atom() keys to String.t keys

Convert a camelCase string or atome to a snake_case

Convert map keys from camelCase to snake_case

Link to this section Functions

Link to this function atomize_keys(map, options \\ [only_existing: false]) View Source

Transforms a map’s String.t keys to atom() keys.

  • map is any map/0

  • options is a keyword list of options. The available option is:

    • :only_existing which is set to true will only convert the binary key to an atom if the atom already exists. The default is false.

Examples

Link to this function atomize_values(map, options \\ [only_existing: false]) View Source

Transforms a map’s String.t values to atom() values.

  • map is any map/0

  • options is a keyword list of options. The available option is:

    • :only_existing which is set to true will only convert the binary value to an atom if the atom already exists. The default is false.

Examples

Link to this function deep_map(map, function) View Source
deep_map(map(), function :: function()) :: map()

Recursively traverse a map and invoke a function for each key/ value pair that transforms the map.

Arguments

  • map is any map/0

  • function is a function or function reference that is called for each key/value pair of the provided map

Returns

  • The map transformed by the recursive application of function

Example

iex> map = %{a: “a”, b: %{c: “c”}} iex> Cldr.Map.deep_map map, fn {k, v} -> …> {k, String.upcase(v)} …> end %{a: “A”, b: %{c: “C”}}

Link to this function deep_map(map, key_function, value_function) View Source
deep_map(term(), key_function :: function(), value_function :: function()) ::
  term()

Recursively traverse a map and invoke a function for each key and a function for each value that transform the map.

  • map is any map/0

  • key_function is a function or function reference that is called for each key of the provided map and any keys of any submaps

  • value_function is a function or function reference that is called for each value of the provided map and any values of any submaps

Returns:

  • The map transformed by the recursive application of key_function and value_function

Examples

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 values to Float.t values.

The map value is converted to a float from either an atom or String.t only when the value is comprised of a valid float forma.

Keys which cannot be converted to a float are returned unchanged.

Examples

Transforms a map’s keys to Integer.t keys.

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.

Examples

Transforms a map’s values to Integer.t values.

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.

Examples

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"}
Link to this function remove_leading_underscores(map) View Source

Removes any leading underscores from map keys.

Examples

Link to this function rename_key(map, from, to) View Source

Rename map keys from from to to

  • map is any map/0

  • from is any value map key

  • to is any valud map key

Examples

Transforms a map’s atom() keys to String.t keys.

Examples

Link to this function underscore(atom) View Source
underscore(string :: String.t() | atom()) :: String.t()

Convert a camelCase string or atome to a snake_case

  • string is a String.t or atom() 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

Convert map keys from camelCase to snake_case

Examples