Skip to content

Commit

Permalink
allow using descending order as initial order
Browse files Browse the repository at this point in the history
  • Loading branch information
woylie committed Nov 18, 2023
1 parent 46bf7b1 commit 235857c
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions lib/flop.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,20 @@ defmodule Flop do
iex> flop.order_directions
[:desc_nulls_last]
This also allows you to sort in descending order initially.
iex> directions = {:desc, :asc}
iex> flop = push_order(%Flop{}, :ttfb, directions: directions)
iex> flop.order_by
[:ttfb]
iex> flop.order_directions
[:desc]
iex> flop = push_order(flop, :ttfb, directions: directions)
iex> flop.order_by
[:ttfb]
iex> flop.order_directions
[:asc]
If a string is passed as the second argument, it will be converted to an atom
using `String.to_existing_atom/1`. If the atom does not exist, the `Flop`
struct will be returned unchanged.
Expand Down Expand Up @@ -2061,11 +2075,19 @@ defmodule Flop do
do: reverse_direction(current_direction)

defp new_order_direction(0, current_direction, {_asc, desc})
when is_asc_direction(current_direction) and is_direction(desc),
when is_asc_direction(current_direction) and is_desc_direction(desc),
do: desc

defp new_order_direction(0, current_direction, {desc, _asc})
when is_asc_direction(current_direction) and is_desc_direction(desc),
do: desc

defp new_order_direction(0, current_direction, {asc, _desc})
when is_desc_direction(current_direction) and is_direction(asc),
when is_desc_direction(current_direction) and is_asc_direction(asc),
do: asc

Check warning on line 2087 in lib/flop.ex

View check run for this annotation

Codecov / codecov/patch

lib/flop.ex#L2087

Added line #L2087 was not covered by tests

defp new_order_direction(0, current_direction, {_desc, asc})
when is_desc_direction(current_direction) and is_asc_direction(asc),
do: asc

defp new_order_direction(0, _current_direction, directions) do
Expand Down

0 comments on commit 235857c

Please sign in to comment.