Map Sorter v0.1.14 MapSorter.Support View Source

Generates a sort function from a list of sort specs (ascending/descending keys).

Link to this section Summary

Functions

Adapts the given string to invoke the sortable/1 function

Returns a sort function based on the given sort specs

Returns the AST of a sort function based on the given sort specs (compile time or runtime)

Converts a value to a sortable format, if needed

Link to this section Types

Link to this type sort_dir() View Source
sort_dir() :: :asc | :desc
Link to this type sort_fun() View Source
sort_fun() :: (map(), map() -> boolean())
Link to this type sort_spec() View Source
sort_spec() :: any() | {sort_dir(), any()}

Link to this section Functions

Adapts the given string to invoke the sortable/1 function.

Examples

iex> alias MapSorter.Support
iex> Support.adapt(
...>   """
...>   &1[:dob] < ...
...>   &2[:likes] -> ...
...>   """
...> )
"""
MapSorter.Support.sortable(&1[:dob]) < ...
MapSorter.Support.sortable(&2[:likes]) -> ...
"""
Link to this function eval_sort_fun(sort_specs) View Source
eval_sort_fun([sort_spec()]) :: sort_fun()

Returns a sort function based on the given sort specs.

Examples

iex> alias MapSorter.Support
iex> Logger.configure(level: :info) # :debug → debug messages
iex> sort_fun = Support.eval_sort_fun([:dob, desc: :likes])
iex> Logger.configure(level: :info) # :info → no debug messages
iex> is_function(sort_fun, 2)
true
Link to this function sort_fun_ast(sort_specs) View Source
sort_fun_ast([sort_spec()] | {any(), any(), any()}) :: {any(), any(), any()}

Returns the AST of a sort function based on the given sort specs (compile time or runtime).

Examples

iex> alias MapSorter.Support
iex> Logger.configure(level: :info) # :debug → debug messages
iex> sort_fun_ast = Support.sort_fun_ast([:dob, desc: :likes])
iex> Logger.configure(level: :info) # :info → no debug messages
iex> match?({:&, _meta, _args}, sort_fun_ast)
true

iex> alias MapSorter.Support
iex> sort_specs_ast = quote do: Tuple.to_list({:dob, {:desc, :likes}})
iex> Logger.configure(level: :info) # :debug → debug messages
iex> sort_fun_ast = Support.sort_fun_ast(sort_specs_ast)
iex> Logger.configure(level: :info) # :info → no debug messages
iex> match?({{:., _, _}, _meta, _args}, sort_fun_ast)
true
Link to this function sortable(value) View Source
sortable(any()) :: any()

Converts a value to a sortable format, if needed.

Examples

iex> alias MapSorter.Support
iex> Support.sortable(~T[15:41:33])
"15:41:33"

iex> alias MapSorter.Support
iex> Support.sortable(3.1416)
3.1416