diff --git a/Project.toml b/Project.toml index ca66c7b..7d8b13e 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,10 @@ name = "DataAPI" uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a" authors = ["quinnj "] -version = "1.4.0" +version = "1.5.0" + +[deps] +Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" [compat] julia = "1" diff --git a/src/DataAPI.jl b/src/DataAPI.jl index f4c943f..cc7b0c3 100644 --- a/src/DataAPI.jl +++ b/src/DataAPI.jl @@ -1,5 +1,7 @@ module DataAPI +import Dates + """ defaultarray(T, N) @@ -89,6 +91,9 @@ If the collection is not sortable then the order of levels is unspecified. Contrary to [`unique`](@ref), this function may return values which do not actually occur in the data, and does not preserve their order of appearance in `x`. + +If a type of `A` implements `levels` with a meaningful order then it should also +implement `isordered` as by default `isordered` returns `false`. """ function levels(x) T = Base.nonmissingtype(eltype(x)) @@ -100,6 +105,26 @@ function levels(x) levs end +""" + isordered(A) + +Test whether entries in `A` can be compared using `<`, `>` and similar operators, +using the ordering of `levels(A)`. + +If a type of `A` implements `levels` with a meaningful order then it should also +implement `isordered` as by default `isordered` returns `false`. +""" +function isordered end + +isordered(::AbstractArray{<:Union{Missing, AbstractString}}) = true +isordered(::AbstractArray{<:Union{Missing, Real}}) = true +isordered(::AbstractArray{<:Union{Missing, Symbol}}) = true +isordered(::AbstractArray{<:Union{Missing, AbstractChar}}) = true +isordered(::AbstractArray{<:Union{Missing, Dates.Period}}) = true +isordered(::AbstractArray{<:Union{Missing, Dates.TimeType}}) = true +isordered(::AbstractArray{Missing}) = true +isordered(::AbstractArray{<:Any}) = false + """ Between(first, last) diff --git a/test/runtests.jl b/test/runtests.jl index 0496c7f..9d9930e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -84,5 +84,17 @@ end end end + +@testset "isordered" begin + + @test DataAPI.isordered([1]) + @test DataAPI.isordered(Union{Real,Missing}[1]) + @test DataAPI.isordered(["1"]) + @test DataAPI.isordered(['1']) + @test !DataAPI.isordered(Any[1]) + @test !DataAPI.isordered(["1", '1']) + @test !DataAPI.isordered(Union{Char, String}["1", '1']) + +end end # @testset "DataAPI"