ExShopifySchema.Generator.Graphql.Introspection.Util.Enum (ExShopifySchema v2024.4.2)

View Source

Utility functions for working with enum values.

Summary

Functions

The safe map function is a wrapper around Enum.map that handles nil values gracefully.

Converts a map's keys from camelCase to snake_case recursively.

Converts a string to an existing atom. If the corresponding atom does not exist, it raises an error.

Functions

safe_map(enumerable, fun)

@spec safe_map(enumerable :: nil, fun :: (term() -> term())) :: []
@spec safe_map(enumerable :: Enumerable.t(element), fun :: (element -> value)) :: [
  value
]
when value: var

The safe map function is a wrapper around Enum.map that handles nil values gracefully.

Examples

iex> safe_map([1, 2, 3], fn x -> x * 2 end)
[2, 4, 6]

iex> safe_map(nil, fn x -> x * 2 end)
[]

snake_case_keys(map)

@spec snake_case_keys(term()) :: term()

Converts a map's keys from camelCase to snake_case recursively.

Examples

iex> snake_case_keys(%{"camelCaseKey" => "value"})
%{"camel_case_key" => "value"}

iex> snake_case_keys(%{"nestedMap" => %{"anotherKey" => "value"}})
%{"nested_map" => %{"another_key" => "value"}}

iex> snake_case_keys([%{"listItem" => "value"}])
[%{"list_item" => "value"}]

string_to_atom!(enum)

@spec string_to_atom!(String.t()) :: atom()

Converts a string to an existing atom. If the corresponding atom does not exist, it raises an error.

Examples

iex> string_to_atom!("code")
:code