Allow an optional extension to be passed to nextfile
This commit is contained in:
parent
415a0f1d57
commit
b0cadf63e4
|
@ -83,5 +83,13 @@ describe("nextfile", function()
|
|||
|
||||
assert.are.equal("/testdir/421za.txt", file)
|
||||
end)
|
||||
|
||||
it("should use the provided file extension if one is given", function()
|
||||
prevfile.prevfile = function() return "/testdir/900.txt" end
|
||||
|
||||
local file = nextfile.nextfile("/testdir", "yaml")
|
||||
|
||||
assert.are.equal("/testdir/901.yaml", file)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
|
|
@ -15,19 +15,28 @@ function nextfile.nextletter(letter)
|
|||
return string.char(nextascii)
|
||||
end
|
||||
|
||||
function nextfile.nextfile(directory)
|
||||
function nextfile.nextfile(directory, extension)
|
||||
local previous = prevfile.prevfile(directory)
|
||||
if previous == nil then return nil end
|
||||
|
||||
local dir, name, ext = nextfile.splitfilename(previous)
|
||||
|
||||
value = dir
|
||||
if tonumber(name) == nil then
|
||||
local lastletter = name:sub(#name)
|
||||
local nextletter = nextfile.nextletter(lastletter)
|
||||
return dir .. name:gsub(lastletter .. "$", nextletter) .. ext
|
||||
value = value .. name:gsub(lastletter .. "$", nextletter)
|
||||
else
|
||||
value = value .. name + 1
|
||||
end
|
||||
|
||||
return dir .. name + 1 .. ext
|
||||
if extension ~= nil then
|
||||
value = value .. "." .. extension
|
||||
else
|
||||
value = value .. ext
|
||||
end
|
||||
|
||||
return value
|
||||
end
|
||||
|
||||
return nextfile
|
||||
|
|
Loading…
Reference in New Issue