Xema.Utils (xema v0.13.6) View Source

Some utilities for Xema.

Link to this section Summary

Functions

Returns whether the given key exists in the given value.

Returns the size of a list or tuple.

Converts the given string to an existing atom. Returns nil if the atom does not exist.

Returns nil if uri_1 and uri_2 are nil. Parses a URI when the other URI is nil. Merges URIs if both are not nil.

Link to this section Functions

Specs

has_key?(map() | keyword() | [{String.t(), any()}], any()) :: boolean()

Returns whether the given key exists in the given value.

Returns true if

  • value is a map and contains key as a key.
  • value is a keyword and contains key as a key.
  • value is a list of tuples with keyas the first element.

Example

  iex> alias Xema.Utils
  iex> Utils.has_key?(%{foo: 5}, :foo)
  true
  iex> Utils.has_key?([foo: 5], :foo)
  true
  iex> Utils.has_key?([{"foo", 5}], "foo")
  true

Specs

size(list() | tuple()) :: integer()

Returns the size of a list or tuple.

Specs

to_existing_atom(String.t() | atom()) :: atom() | nil

Converts the given string to an existing atom. Returns nil if the atom does not exist.

Examples

  iex> import Xema.Utils
  iex> to_existing_atom(:my_atom)
  :my_atom
  iex> to_existing_atom("my_atom")
  :my_atom
  iex> to_existing_atom("not_existing_atom")
  nil
Link to this function

update_uri(uri_1, uri_2)

View Source

Specs

update_uri(URI.t() | String.t() | nil, URI.t() | String.t() | nil) ::
  URI.t() | nil

Returns nil if uri_1 and uri_2 are nil. Parses a URI when the other URI is nil. Merges URIs if both are not nil.