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" local nextfile = require "nextfile"
it("should return the basename of an absolute filepath", function() it("should return the filename and extension of an absolute filepath", function()
local name = nextfile.basename("/testdir/701.txt") local name, ext = nextfile.splitfilename("/testdir/701.txt")
assert.are.equal("701", name) assert.are.equal("701", name)
assert.are.equal("txt", ext)
end) end)
it("should return the basename of a relative filepath", function() it("should return the filename and extension of a relative filepath", function()
local name = nextfile.basename("../../foo/testdir/file.mp3") local name, ext = nextfile.splitfilename("../../foo/testdir/file.mp3")
assert.are.equal("file", name) assert.are.equal("file", name)
assert.are.equal("mp3", ext)
end) end)
end) end)

View File

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