Search documentation of Localize

# Localize.Utils.Helpers (Localize v0.39.0)

Copy Markdown View Source

General purpose helper functions for Localize.

Provides utility functions for checking empty data structures and wrapping `:persistent_term` operations.

## Summary

### Functions

empty?(map)

Returns a boolean indicating if a data structure is semantically empty.

existing_atom(string)

Converts a string to an existing atom, returning `nil` if the atom does not already exist in the atom table.

## Functions

# empty?(map)

Returns a boolean indicating if a data structure is semantically empty.

### Arguments

- `value` — the value to check. Supported types are lists, maps, and `nil`.

### Returns

- `true` if the value is an empty list, an empty map, or `nil`.

- `false` otherwise.

### Examples

    iex> Localize.Utils.Helpers.empty?([])
    true

    iex> Localize.Utils.Helpers.empty?(%{})
    true

    iex> Localize.Utils.Helpers.empty?(nil)
    true

    iex> Localize.Utils.Helpers.empty?([1, 2])
    false

    iex> Localize.Utils.Helpers.empty?(%{a: 1})
    false

# existing_atom(string)

    @spec existing_atom(String.t()) :: atom() | nil

Converts a string to an existing atom, returning `nil` if the atom does not already exist in the atom table.

This is the safe alternative to `String.to_existing_atom/1` that avoids using `try/rescue` as control flow.

### Arguments

- `string` is a binary string.

### Returns

- The atom if it exists, or `nil` otherwise.