NLdoc.Util (NLdoc.Util v1.0.7)
View SourceNLdoc.Util
implements utility functions that are used throughout the project,
but don't fit into any of the other modules.
Summary
Functions
Returns the new value if it is not nil, otherwise returns the default value.
Encodes a map, list, keyword list, tuple or any other term into a format that can be encoded with Jason without raising an error. Also handles some special cases to make the output more readable.
Asserts that an OK tuple is {:ok, value}
and returns the value.
Raises the error if the tuple is {:error, error}
.
Tests an OK-tuple, returning the value if the tuple is {:ok, value}
or returning whatever is
returned by calling the given function with the error if the tuple is {:error, error}
.
Returns true if the two lists have the same values, regardless of order.
Functions
@spec default(nil, old) :: old when old: var
@spec default(new, _ :: any()) :: new when new: var
Returns the new value if it is not nil, otherwise returns the default value.
Examples
iex> NLdoc.Util.default(nil, 1)
1
iex> NLdoc.Util.default(2, 1)
2
Encodes a map, list, keyword list, tuple or any other term into a format that can be encoded with Jason without raising an error. Also handles some special cases to make the output more readable.
Examples
iex> NLdoc.Util.encode(%{a: 1, b: 2})
%{a: 1, b: 2}
iex> NLdoc.Util.encode([1, 2, 3])
[1, 2, 3]
iex> NLdoc.Util.encode([a: 1, b: 2])
%{a: 1, b: 2}
iex> NLdoc.Util.encode({:parameterized, {Ecto.Enum, %{data: [1, 2, 3]}}})
[:parameterized, "Ecto.Enum"]
iex> NLdoc.Util.encode({"error", [type: {"error", "details"}, validation: :inclusion]})
["error", %{type: ["error", "details"], validation: :inclusion}]
@spec ok!(:ok) :: :ok
@spec ok!({:ok, value}) :: value when value: var
@spec ok!({:error, any()}) :: no_return()
Asserts that an OK tuple is {:ok, value}
and returns the value.
Raises the error if the tuple is {:error, error}
.
Examples
iex> NLdoc.Util.ok!({:ok, 1})
1
iex> NLdoc.Util.ok!(:ok)
:ok
iex> NLdoc.Util.ok!({:error, "error"})
** (RuntimeError) error
iex> NLdoc.Util.ok!({:error, %File.Error{path: "file", reason: :enoent}})
** (File.Error) could not "file": no such file or directory
iex> NLdoc.Util.ok!({:error, %{foo: "bar"}})
** (RuntimeError) %{foo: "bar"}
@spec ok_else(:ok, any()) :: :ok
@spec ok_else( {:ok, value}, any() ) :: value when value: var
@spec ok_else(
{:error, error},
(error -> return)
) :: return
when error: var, return: var
Tests an OK-tuple, returning the value if the tuple is {:ok, value}
or returning whatever is
returned by calling the given function with the error if the tuple is {:error, error}
.
Examples
iex> NLdoc.Util.ok_else({:ok, 1}, 2)
1
iex> NLdoc.Util.ok_else({:error, "error"}, fn error -> String.upcase(error) end)
"ERROR"
iex> NLdoc.Util.ok_else(:ok, :abc)
:ok
Returns true if the two lists have the same values, regardless of order.
Examples
iex> NLdoc.Util.same_values([], [])
true
iex> NLdoc.Util.same_values([1, 2, 3], [1, 2, 3])
true
iex> NLdoc.Util.same_values([1, 2, 3], [3, 2, 1])
true
iex> NLdoc.Util.same_values([1, 2, 3], [1, 2, 4])
false
iex> NLdoc.Util.same_values([1, 2, 3], [1, 2, 3, 4])
false