forked from shibao/cannery
fix tables unable to sort on nil dates
This commit is contained in:
12
lib/cannery/comparable_date.ex
Normal file
12
lib/cannery/comparable_date.ex
Normal file
@ -0,0 +1,12 @@
|
||||
defmodule Cannery.ComparableDate do
|
||||
@moduledoc """
|
||||
A custom `Date` module that provides a `compare/2` function that is comparable
|
||||
with nil values
|
||||
"""
|
||||
|
||||
@spec compare(Date.t() | any(), Date.t() | any()) :: :lt | :gt | :eq
|
||||
def compare(%Date{} = date_1, %Date{} = date_2), do: Date.compare(date_1, date_2)
|
||||
def compare(%Date{}, _date_2), do: :lt
|
||||
def compare(_date_1, %Date{}), do: :gt
|
||||
def compare(_date_1, _date_2), do: :eq
|
||||
end
|
15
lib/cannery/comparable_datetime.ex
Normal file
15
lib/cannery/comparable_datetime.ex
Normal file
@ -0,0 +1,15 @@
|
||||
defmodule Cannery.ComparableDateTime do
|
||||
@moduledoc """
|
||||
A custom `DateTime` module that provides a `compare/2` function that is
|
||||
comparable with nil values
|
||||
"""
|
||||
|
||||
@spec compare(DateTime.t() | any(), DateTime.t() | any()) :: :lt | :gt | :eq
|
||||
def compare(%DateTime{} = datetime_1, %DateTime{} = datetime_2) do
|
||||
DateTime.compare(datetime_1, datetime_2)
|
||||
end
|
||||
|
||||
def compare(%DateTime{}, _datetime_2), do: :lt
|
||||
def compare(_datetime_1, %DateTime{}), do: :gt
|
||||
def compare(_datetime_1, _datetime_2), do: :eq
|
||||
end
|
Reference in New Issue
Block a user