Lightning.Helpers (Lightning v2.13.5)

View Source

Common functions for the context

Summary

Functions

Changes a given maps field from a json string to a map. If it cannot be converted, it leaves the original value

Copies an error from one key to another in the given changeset.

Recursively ensures a given map is safe to convert to JSON, where all keys are strings and all values are json safe (primitive values).

Converts milliseconds (integer) to a human duration, such as "1 minute" or "45 years, 6 months, 5 days, 21 hours, 12 minutes, 34 seconds" using Timex.Format.Duration.Formatters.Humanized.format().

Normalizes all map keys to strings recursively.

Converts a string into a URL-safe format by converting it to lowercase, replacing unwanted characters with hyphens, and trimming leading/trailing hyphens.

Functions

actual_deletion_date(grace_period, cron_expression \\ "4 2 * * *", unit \\ :days)

coerce_json_field(attrs, field)

@spec coerce_json_field(map(), Map.key()) :: map()

Changes a given maps field from a json string to a map. If it cannot be converted, it leaves the original value

copy_error(changeset, original_key, new_key, opts \\ [overwrite: true])

Copies an error from one key to another in the given changeset.

Parameters

  • changeset: The changeset to modify.
  • original_key: The key where the error currently exists.
  • new_key: The key where the error should be duplicated.
  • opts: A keyword list of options. Supports overwrite, which is a boolean indicating whether to overwrite the new_key error if it already exists. Defaults to true.

Example

iex> changeset = %Ecto.Changeset{errors: [name: {"has already been taken", []}]}
iex> updated_changeset = Lightning.Helpers.copy_error(changeset, :name, :raw_name)
iex> updated_changeset.errors
[name: {"has already been taken", []}, raw_name: {"has already been taken", []}]

If the original_key doesn't exist in the errors, or if the new_key already exists and overwrite is set to false, the changeset is returned unchanged.

format_date(date, formatter \\ "%F %T")

format_date_long(date)

json_safe(data)

Recursively ensures a given map is safe to convert to JSON, where all keys are strings and all values are json safe (primitive values).

ms_to_human(milliseconds)

@spec ms_to_human(integer()) :: String.t() | {:error, :invalid_duration}

Converts milliseconds (integer) to a human duration, such as "1 minute" or "45 years, 6 months, 5 days, 21 hours, 12 minutes, 34 seconds" using Timex.Format.Duration.Formatters.Humanized.format().

normalize_keys(map)

Normalizes all map keys to strings recursively.

This function walks through a map and converts all keys to strings using to_string/1. If a key's value is also a map, it recursively normalizes the nested map as well. Non-map values are returned unchanged.

Examples

iex> normalize_keys(%{foo: "bar", baz: %{qux: 123}})
%{"foo" => "bar", "baz" => %{"qux" => 123}}

iex> normalize_keys(%{1 => "one", 2 => "two"})
%{"1" => "one", "2" => "two"}

iex> normalize_keys("not a map")
"not a map"

Parameters

  • map: The map whose keys should be normalized to strings
  • value: Any non-map value that should be returned as-is

Returns

  • A new map with all keys converted to strings (for map inputs)
  • The original value unchanged (for non-map inputs)

url_safe_name(name)

@spec url_safe_name(String.t() | nil) :: String.t()

Converts a string into a URL-safe format by converting it to lowercase, replacing unwanted characters with hyphens, and trimming leading/trailing hyphens.

This function allows international characters, which will be automatically percent-encoded in URLs by browsers.

Parameters

  • name: The string to convert. If nil is passed, it returns an empty string.

Examples

iex> url_safe_name("My Project!!")
"my-project"

iex> url_safe_name(nil)
""