View Source Dequel.Comparators (Dequel v0.7.0)

Shared comparison utilities for Dequel adapters and matchers.

Provides type coercion and comparison functions used by both the ETS adapter and the in-memory Dequel.matches?/2 function.

Summary

Functions

Compares two values using the given comparison operator function.

Gets a field value from a map or struct.

Checks if a value is within a range (inclusive).

Parses a string into a number (integer or float).

Checks if a string matches using the given string operation.

Functions

Link to this function

compare_values(field_value, value, op)

View Source
@spec compare_values(any(), any(), (any(), any() -> boolean())) :: boolean()

Compares two values using the given comparison operator function.

Handles type coercion when comparing numbers against strings.

Examples

iex> Dequel.Comparators.compare_values(10, "5", &Kernel.>/2)
true

iex> Dequel.Comparators.compare_values(nil, "5", &Kernel.>/2)
false
Link to this function

get_field_value(record, field)

View Source
@spec get_field_value(map(), atom() | [atom()] | binary()) :: any()

Gets a field value from a map or struct.

Handles atoms, strings (converted to existing atoms), and paths (list of atoms).

Examples

iex> Dequel.Comparators.get_field_value(%{name: "Alice"}, :name)
"Alice"

iex> Dequel.Comparators.get_field_value(%{author: %{name: "Bob"}}, [:author, :name])
"Bob"
Link to this function

in_range?(field_value, start_val, end_val)

View Source
@spec in_range?(any(), any(), any()) :: boolean()

Checks if a value is within a range (inclusive).

Examples

iex> Dequel.Comparators.in_range?(15, "10", "20")
true

iex> Dequel.Comparators.in_range?(nil, "10", "20")
false
@spec parse_number(binary()) :: {:ok, number()} | :error

Parses a string into a number (integer or float).

Examples

iex> Dequel.Comparators.parse_number("42")
{:ok, 42}

iex> Dequel.Comparators.parse_number("3.14")
{:ok, 3.14}

iex> Dequel.Comparators.parse_number("not a number")
:error
Link to this function

string_match?(field_value, value, operation)

View Source
@spec string_match?(any(), binary(), :contains | :starts_with | :ends_with) ::
  boolean()

Checks if a string matches using the given string operation.

Returns false for nil values or non-string field values.

Examples

iex> Dequel.Comparators.string_match?("hello world", "world", :contains)
true

iex> Dequel.Comparators.string_match?("hello", "he", :starts_with)
true