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
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
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"
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
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
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