View Source Electric.Utils (electric v0.5.2)

Summary

Functions

Return a list of values from enum that are the maximal elements as calculated by the given fun.

Applies either an anonymous function or a MFA tuple, prepending the given arguments in case of an MFA.

Encode binary representation of a UUID into a string

Output a 2-tuple relation (table) reference as pg-style "schema"."table".

Map each value of the enumerable using a mapper, unwrapping a result tuple returned by the mapper and stopping on error.

Parse a markdown table from a string

Parses quoted names.

Format a relation tuple to be correctly escaped for use in SQL queries.

Generate a random UUID v4.

Functions

Link to this function

all_max_by(enum, fun, sorter \\ &>=/2, comparator \\ &==/2, empty_fallback \\ fn -> raise Enum.EmptyError end)

View Source

Return a list of values from enum that are the maximal elements as calculated by the given fun.

Base behaviour is similar to Enum.max_by/4, but this function returns a list of all maximal values instead of just the first one.

Examples

iex> all_max_by([4, 1, 1, 3, -4], &abs/1)
[4, -4]

iex> all_max_by([4, 1, -1, 3, 4], &abs/1, &<=/2)
[1, -1]

iex> all_max_by([], &abs/1)
** (Enum.EmptyError) empty error
Link to this function

apply_fn_or_mfa(fun, args)

View Source

Applies either an anonymous function or a MFA tuple, prepending the given arguments in case of an MFA.

Examples

iex> apply_fn_or_mfa(&String.contains?(&1, "foo"), ["foobar"])
true

iex> apply_fn_or_mfa({String, :contains?, ["foo"]}, ["foobar"])
true

Encode binary representation of a UUID into a string

Examples

iex> encode_uuid(<<1, 35, 69, 103, 137, 171, 76, 222, 143, 227, 251, 149, 223, 249, 31, 215>>)
"01234567-89ab-4cde-8fe3-fb95dff91fd7"
@spec inspect_relation({String.t(), String.t()}) :: String.t()

Output a 2-tuple relation (table) reference as pg-style "schema"."table".

Examples

iex> inspect_relation({"schema", "table"})
~S|"schema"."table"|
Link to this function

map_while_ok(enum, mapper)

View Source
@spec map_while_ok(Enumerable.t(elem), (elem -> {:ok, result} | {:error, term()})) ::
  {:ok, [result]} | {:error, term()}
when elem: var, result: var

Map each value of the enumerable using a mapper, unwrapping a result tuple returned by the mapper and stopping on error.

Examples

iex> map_while_ok(["2015-01-23 23:50:07.0", "2015-01-23 23:50:08"], &NaiveDateTime.from_iso8601/1)
{:ok, [~N[2015-01-23 23:50:07.0], ~N[2015-01-23 23:50:08]]}

iex> map_while_ok(["2015-01-23 23:50:07A", "2015-01-23 23:50:08"], &NaiveDateTime.from_iso8601/1)
{:error, :invalid_format}
Link to this function

parse_md_table(string, opts)

View Source
@spec parse_md_table(String.t(), [{:after, String.t()}]) :: [[String.t(), ...]]

Parse a markdown table from a string

Options:

  • after: - taking a first table that comes right after a given substring.

Example

iex> """
...> Some text
...>
...> ## Known types
...>
...> | type                    | category | preferred? |
...> | ----------------------- | -------- | ---------- |
...> | bool                    | boolean  | t          |
...> | int2                    | numeric  |            |
...> """|> parse_md_table(after: "## Known types")
[["bool", "boolean", "t"], ["int2", "numeric", ""]]

iex> """
...> Some text
...> """|> parse_md_table([])
[]

Parses quoted names.

Examples

iex> parse_quoted_name("foo")
"foo"

iex> parse_quoted_name(~S|"foo"|)
"foo"

iex> parse_quoted_name(~S|"fo""o"|)
~S|fo"o|
@spec relation_to_sql(Electric.relation()) :: String.t()

Format a relation tuple to be correctly escaped for use in SQL queries.

Examples

iex> relation_to_sql({"public", "items"})
~S|"public"."items"|

iex> relation_to_sql({"with spaces", ~S|and "quoted"!|})
~S|"with spaces"."and ""quoted""!"|

Generate a random UUID v4.

Code taken from Ecto: https://github.com/elixir-ecto/ecto/blob/v3.10.2/lib/ecto/uuid.ex#L174

Examples

iex> Regex.match?(~r/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/, uuid4())
true