Change basename to splitfilename, return extension

This commit is contained in:
The Magician 2023-11-18 18:05:01 +00:00
parent 04e9638261
commit d58b6f9148
2 changed files with 23 additions and 17 deletions

View File

@ -1,16 +1,18 @@
describe("basename", function()
describe("splitfilename", function()
local nextfile = require "nextfile"
it("should return the basename of an absolute filepath", function()
local name = nextfile.basename("/testdir/701.txt")
it("should return the filename and extension of an absolute filepath", function()
local name, ext = nextfile.splitfilename("/testdir/701.txt")
assert.are.equal("701", name)
assert.are.equal("txt", ext)
end)
it("should return the basename of a relative filepath", function()
local name = nextfile.basename("../../foo/testdir/file.mp3")
it("should return the filename and extension of a relative filepath", function()
local name, ext = nextfile.splitfilename("../../foo/testdir/file.mp3")
assert.are.equal("file", name)
assert.are.equal("mp3", ext)
end)
end)

View File

@ -2,14 +2,18 @@ local prevfile = require "prevfile"
local nextfile = {}
function nextfile.basename(path)
return path:match("^.*/(.*)%.%w*")
function nextfile.splitfilename(path)
name, ext = path:match("^.*/(.*)%.(%w*)")
return name, ext
end
function nextfile.nextfile(directory)
prevfile.prevfile(directory)
return nil
local previous = prevfile.prevfile(directory)
if previous == nil then return nil end
local name, ext = nextfile.splitfilename(previous)
return ""
end
return nextfile