Lightning.Helpers (Lightning v2.13.5)
View SourceCommon 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
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.
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. Supportsoverwrite
, which is a boolean indicating whether to overwrite thenew_key
error if it already exists. Defaults totrue
.
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.
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.
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 stringsvalue
: 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)
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. Ifnil
is passed, it returns an empty string.
Examples
iex> url_safe_name("My Project!!")
"my-project"
iex> url_safe_name(nil)
""