View Source FatUtils.Map (FatEcto v1.0.0)

Provides utility functions for working with maps and structs.

This module includes functions for checking keys, values, and performing deep merges.

Summary

Functions

Checks if the map contains only the allowed keys.

Deep merges two maps.

Counts the number of specified keys present in the map.

Checks if all the specified keys are present in the map.

Checks if the map contains only the specified keys and no others.

Checks if all the specified keys in the map have the given value.

Checks if the map contains any of the specified keys.

Functions

contain_only_allowed_keys?(map, keys)

@spec contain_only_allowed_keys?(map(), [any()]) :: boolean()

Checks if the map contains only the allowed keys.

Parameters

  • map: The map to check.
  • keys: A list of allowed keys.

Examples

iex> FatUtils.Map.contain_only_allowed_keys?(%{a: 1, b: 2}, [:a, :b])
true
iex> FatUtils.Map.contain_only_allowed_keys?(%{a: 1, c: 3}, [:a, :b])
false

deep_merge(left, right)

@spec deep_merge(map(), map()) :: map()

Deep merges two maps.

Parameters

  • left: The first map.
  • right: The second map.

Examples

iex> FatUtils.Map.deep_merge(%{a: %{b: 1}}, %{a: %{c: 2}})
%{a: %{b: 1, c: 2}}

get_keys_count(map, keys)

@spec get_keys_count(map(), [any()]) :: non_neg_integer()

Counts the number of specified keys present in the map.

Parameters

  • map: The map to check.
  • keys: A list of keys to count.

Examples

iex> FatUtils.Map.get_keys_count(%{a: 1, b: 2}, [:a, :b, :c])
2

has_all_keys?(map, keys)

@spec has_all_keys?(map(), [any()]) :: boolean()

Checks if all the specified keys are present in the map.

Parameters

  • map: The map to check.
  • keys: A list of keys to check for.

Examples

iex> FatUtils.Map.has_all_keys?(%{a: 1, b: 2}, [:a, :b])
true
iex> FatUtils.Map.has_all_keys?(%{a: 1}, [:a, :b])
false

has_all_keys_exclusive?(map, keys)

@spec has_all_keys_exclusive?(map(), [any()]) :: boolean()

Checks if the map contains only the specified keys and no others.

Parameters

  • map: The map to check.
  • keys: A list of allowed keys.

Examples

iex> FatUtils.Map.has_all_keys_exclusive?(%{a: 1, b: 2}, [:a, :b])
true
iex> FatUtils.Map.has_all_keys_exclusive?(%{a: 1, c: 3}, [:a, :b])
false

has_all_val_equal_to?(map, keys, equal_to)

@spec has_all_val_equal_to?(map(), [any()], any()) :: boolean()

Checks if all the specified keys in the map have the given value.

Parameters

  • map: The map to check.
  • keys: A list of keys to check.
  • equal_to: The value to compare against.

Examples

iex> FatUtils.Map.has_all_val_equal_to?(%{a: 1, b: 1}, [:a, :b], 1)
true
iex> FatUtils.Map.has_all_val_equal_to?(%{a: 1, b: 2}, [:a, :b], 1)
false

has_any_of_keys?(map, keys)

@spec has_any_of_keys?(map(), [any()]) :: boolean()

Checks if the map contains any of the specified keys.

Parameters

  • map: The map to check.
  • keys: A list of keys to check for.

Examples

iex> FatUtils.Map.has_any_of_keys?(%{a: 1}, [:a, :b])
true
iex> FatUtils.Map.has_any_of_keys?(%{c: 3}, [:a, :b])
false