Util (fnord v0.8.24)

View Source

Summary

Functions

Filters an enumerable asynchronously using the provided function. The function should return true for items to keep and false for items to discard. The result is a stream of items that passed the filter.

Convenience wrapper for Task.async_stream/3 with the default options for concurrency and timeout set to Application.get_env(:fnord, :workers) and :infinity, respectively.

Compares two files using the diff command and returns the output. If the files are identical, it returns {:ok, "No changes detected."}. If there are differences, it returns {:ok, output} with the diff output. If an error occurs, it returns {:error, output} with the error message.

Expands a given path to its absolute form, expanding . and ... If a root directory is provided, it will expand the path relative to that root. If no root is provided, it will expand the path relative to the current working directory. The root directory is expanded first, if provided (see Path.expand/2).

Shortcut for find_file_within_root(path, <cwd>). Returns {:error, :enoent} if the current working directory cannot be determined.

Finds a file within the specified root directory. It resolves symlinks for both the file path and the root directory. If the resolved file path is within the root directory, it returns {:ok, resolved_path}, otherwise {:error, :enoent}.

Converts a value to an integer, raising an ArgumentError if the value cannot be parsed as an integer. Accepts both binary strings and integers.

Adds line numbers to each line of the input text, separated by a specified separator (default is "|"). The numbering starts from 1, or start_index, if set.

Parses a binary string into an integer, returning {:ok, int} if successful, or {:error, :invalid_integer} if the string cannot be parsed as an integer. Accepts both binary strings and integers.

Returns true if the given path is within the specified root directory, false otherwise. Expands both the path and the root to their absolute forms, resolving symlinks, before performing the check.

Resolves a symlink to its final target. If the path is relative, it will first be expanded relative to the given root. If root is not provided, it will expand relative to the current working directory. If a circular symlink is detected, it returns {:error, :circular_symlink}. Otherwise, it returns the absolute, resolved path or the error tuple originating from File.lstat/1.

Converts all string keys in a map to atoms, recursively.

Capitalizes the first letter of each word in the input string.

Types

async_cb()

@type async_cb() :: (async_item() -> any())

async_item()

@type async_item() :: {:ok, any()} | {:error, any()} | {:error, {any(), any()}}

Functions

async_filter(enumerable, fun)

Filters an enumerable asynchronously using the provided function. The function should return true for items to keep and false for items to discard. The result is a stream of items that passed the filter.

async_stream(enumerable, fun, options \\ [])

@spec async_stream(Enumerable.t(), async_cb(), Keyword.t()) :: Enumerable.t()

Convenience wrapper for Task.async_stream/3 with the default options for concurrency and timeout set to Application.get_env(:fnord, :workers) and :infinity, respectively.

diff_files(a, b)

@spec diff_files(binary(), binary()) :: {:ok, binary()} | {:error, binary()}

Compares two files using the diff command and returns the output. If the files are identical, it returns {:ok, "No changes detected."}. If there are differences, it returns {:ok, output} with the diff output. If an error occurs, it returns {:error, output} with the error message.

expand_path(path, root \\ nil)

Expands a given path to its absolute form, expanding . and ... If a root directory is provided, it will expand the path relative to that root. If no root is provided, it will expand the path relative to the current working directory. The root directory is expanded first, if provided (see Path.expand/2).

find_file(path)

@spec find_file(binary()) :: {:ok, binary()} | {:error, :enoent}

Shortcut for find_file_within_root(path, <cwd>). Returns {:error, :enoent} if the current working directory cannot be determined.

find_file_within_root(path, root)

@spec find_file_within_root(binary(), binary()) :: {:ok, binary()} | {:error, :enoent}

Finds a file within the specified root directory. It resolves symlinks for both the file path and the root directory. If the resolved file path is within the root directory, it returns {:ok, resolved_path}, otherwise {:error, :enoent}.

format_number(int)

get_latest_version()

get_running_version()

int_damnit(value)

@spec int_damnit(binary() | integer()) :: integer()

Converts a value to an integer, raising an ArgumentError if the value cannot be parsed as an integer. Accepts both binary strings and integers.

numbered_lines(text, separator \\ "|", start_index \\ 1)

@spec numbered_lines(binary(), binary(), integer()) :: binary()

Adds line numbers to each line of the input text, separated by a specified separator (default is "|"). The numbering starts from 1, or start_index, if set.

parse_int(val)

@spec parse_int(binary() | integer()) :: {:ok, integer()} | {:error, :invalid_integer}

Parses a binary string into an integer, returning {:ok, int} if successful, or {:error, :invalid_integer} if the string cannot be parsed as an integer. Accepts both binary strings and integers.

path_within_root?(path, root)

@spec path_within_root?(binary(), binary()) :: boolean()

Returns true if the given path is within the specified root directory, false otherwise. Expands both the path and the root to their absolute forms, resolving symlinks, before performing the check.

resolve_symlink(path, root \\ nil)

@spec resolve_symlink(binary(), binary() | nil) ::
  {:ok, binary()} | {:error, :circular_symlink} | {:error, File.posix()}

Resolves a symlink to its final target. If the path is relative, it will first be expanded relative to the given root. If root is not provided, it will expand relative to the current working directory. If a circular symlink is detected, it returns {:error, :circular_symlink}. Otherwise, it returns the absolute, resolved path or the error tuple originating from File.lstat/1.

string_keys_to_atoms(list)

Converts all string keys in a map to atoms, recursively.

ucfirst(input)

Capitalizes the first letter of each word in the input string.