Skip to content

Commit 0eb6a7a

Browse files
authored
Merge pull request #152 from rofinn/rf/sort-join-readdir
Support join and sort kwargs to both readdir and readpath
2 parents 6f003d8 + 0639b4b commit 0eb6a7a

4 files changed

Lines changed: 23 additions & 6 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "FilePathsBase"
22
uuid = "48062228-2e41-5def-b9a4-89aafe57970f"
33
authors = ["Rory Finnegan"]
4-
version = "0.9.15"
4+
version = "0.9.16"
55

66
[deps]
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

src/path.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -702,10 +702,16 @@ function Base.download(url::AbstractPath, localfile::AbstractString)
702702
end
703703

704704
"""
705-
readpath(fp::P) where {P <: AbstractPath} -> Vector{P}
705+
readpath(fp::P; join=true, sort=true) where {P <: AbstractPath} -> Vector{P}
706706
"""
707-
function readpath(p::P)::Vector{P} where P <: AbstractPath
708-
return P[join(p, f) for f in readdir(p)]
707+
function readpath(p::P; join=true, sort=true)::Vector{P} where P <: AbstractPath
708+
@static if VERSION < v"1.4"
709+
results = readdir(p)
710+
sort && sort!(results)
711+
return join ? joinpath.(p, results) : parse.(P, results)
712+
else
713+
return parse.(P, readdir(p; join=join, sort=sort))
714+
end
709715
end
710716

711717
"""

src/system.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ function Base.write(fp::SystemPath, x::Union{String, Vector{UInt8}}, mode="w")
383383
end
384384
end
385385

386-
Base.readdir(fp::SystemPath) = readdir(string(fp))
386+
Base.readdir(fp::SystemPath; kwargs...) = readdir(string(fp); kwargs...)
387387

388388
function Base.download(url::AbstractString, dest::T) where T<:SystemPath
389389
return parse(T, download(url, string(dest)))

test/testpkg.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,21 @@ Base.cd(f::Function, fp::TestPath) = cd(f, test2posix(fp))
6565
Base.mkdir(fp::TestPath; kwargs...) = posix2test(mkdir(test2posix(fp); kwargs...))
6666
Base.symlink(src::TestPath, dest::TestPath; kwargs...) = symlink(test2posix(src), test2posix(dest); kwargs...)
6767
Base.rm(fp::TestPath; kwargs...) = rm(test2posix(fp); kwargs...)
68-
Base.readdir(fp::TestPath) = readdir(test2posix(fp))
6968
Base.read(fp::TestPath) = read(test2posix(fp))
7069
Base.write(fp::TestPath, x) = write(test2posix(fp), x)
7170
Base.chown(fp::TestPath, args...; kwargs...) = chown(test2posix(fp), args...; kwargs...)
7271
Base.chmod(fp::TestPath, args...; kwargs...) = chmod(test2posix(fp), args...; kwargs...)
7372

73+
function Base.readdir(fp::TestPath; join=false, sort=true)
74+
p = test2posix(fp)
75+
@static if VERSION < v"1.4"
76+
return readdir(p)
77+
else
78+
results = readdir(p; join=join, sort=sort)
79+
join || return results
80+
# Awkward wrapper that converts the readdir output back to a TestPath string.
81+
return string.(posix2test.(parse.(PosixPath, results)))
82+
end
83+
end
84+
7485
end

0 commit comments

Comments
 (0)