Generate next filenames correctly

This commit is contained in:
The Magician 2023-11-18 20:03:22 +00:00
parent a876678dea
commit 1400dae3df
2 changed files with 15 additions and 6 deletions

View File

@ -68,12 +68,20 @@ describe("nextfile", function()
end) end)
insulate("prevfile.prevfile returns a non-numeric filename", function() insulate("prevfile.prevfile returns a non-numeric filename", function()
it("should return the result of replacing the last character with the next letter of the alphabet", function()
prevfile.prevfile = function() return "/testdir/421b.txt" end prevfile.prevfile = function() return "/testdir/421b.txt" end
--it("should return the result of replacing the last character with the next letter of the alphabet", function() local file = nextfile.nextfile("/testdir")
--local file = nextfile.nextfile("/testdir")
--assert.are.equal("/testdir/421c.txt", file) assert.are.equal("/testdir/421c.txt", file)
--end) end)
it("should return the result of replacing z with za when z is the last letter of the filename", function()
prevfile.prevfile = function() return "/testdir/421z.txt" end
local file = nextfile.nextfile("/testdir")
assert.are.equal("/testdir/421za.txt", file)
end)
end) end)
end) end)

View File

@ -23,7 +23,8 @@ function nextfile.nextfile(directory)
if tonumber(name) == nil then if tonumber(name) == nil then
local lastletter = name:sub(#name) local lastletter = name:sub(#name)
return dir .. name:gsub() .. ext local nextletter = nextfile.nextletter(lastletter)
return dir .. name:gsub(lastletter .. "$", nextletter) .. ext
end end
return dir .. name + 1 .. ext return dir .. name + 1 .. ext