Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow using descending order as initial order #432

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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
Loading