Return directory, add . to extension

This commit is contained in:
The Magician 2023-11-18 19:21:46 +00:00
parent d58b6f9148
commit 1cd2ae7a6d
2 changed files with 17 additions and 12 deletions

View File

@ -2,17 +2,19 @@ describe("splitfilename", function()
local nextfile = require "nextfile" local nextfile = require "nextfile"
it("should return the filename and extension of an absolute filepath", function() it("should return the filename and extension of an absolute filepath", function()
local name, ext = nextfile.splitfilename("/testdir/701.txt") local dir, name, ext = nextfile.splitfilename("/testdir/701.txt")
assert.are.equal("/testdir/", dir)
assert.are.equal("701", name) assert.are.equal("701", name)
assert.are.equal("txt", ext) assert.are.equal(".txt", ext)
end) end)
it("should return the filename and extension of a relative filepath", function() it("should return the filename and extension of a relative filepath", function()
local name, ext = nextfile.splitfilename("../../foo/testdir/file.mp3") local dir, name, ext = nextfile.splitfilename("../../foo/testdir/file.mp3")
assert.are.equal("../../foo/testdir/", dir)
assert.are.equal("file", name) assert.are.equal("file", name)
assert.are.equal("mp3", ext) assert.are.equal(".mp3", ext)
end) end)
end) end)
@ -39,13 +41,13 @@ describe("nextfile", function()
end) end)
end) end)
--insulate("prevfile.prevfile returns a numeric filename", function() insulate("prevfile.prevfile returns a numeric filename", function()
--prevfile.prevfile = function() return "/testdir/701.txt" end prevfile.prevfile = function() return "/testdir/701.txt" end
--
--it("should return the result of incrementing the filename", function() --it("should return the result of incrementing the filename", function()
--local file = nextfile.nextfile("/testdir") --local file = nextfile.nextfile("/testdir")
--
--assert.are.equal("/testdir/702.txt", file) --assert.are.equal("/testdir/702.txt", file)
--end) --end)
--end) end)
end) end)

View File

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