Add nextletter function

This commit is contained in:
The Magician 2023-11-18 19:57:01 +00:00
parent 1cd2ae7a6d
commit a876678dea
2 changed files with 42 additions and 6 deletions

View File

@ -18,6 +18,22 @@ describe("splitfilename", function()
end) end)
end) end)
describe("nextletter", function()
local nextfile = require "nextfile"
it("should return the next letter when the current letter is not 'z'", function()
local letter = nextfile.nextletter("q")
assert.are.equal("r", letter)
end)
it("should return 'za' when the current letter is 'z'", function()
local letter = nextfile.nextletter("z")
assert.are.equal("za", letter)
end)
end)
describe("nextfile", function() describe("nextfile", function()
local prevfile = require "prevfile" local prevfile = require "prevfile"
local nextfile = require "nextfile" local nextfile = require "nextfile"
@ -44,10 +60,20 @@ describe("nextfile", function()
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")
assert.are.equal("/testdir/702.txt", file)
end)
end)
insulate("prevfile.prevfile returns a non-numeric filename", function()
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/702.txt", file) --assert.are.equal("/testdir/421c.txt", file)
--end) --end)
end) end)
end) end)

View File

@ -7,16 +7,26 @@ function nextfile.splitfilename(path)
return dir, name, ext return dir, name, ext
end end
function nextfile.nextletter(letter)
if letter == "z" then return "za" end
local ascii = letter:byte()
local nextascii = ascii + 1
return string.char(nextascii)
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 dir, name, ext = nextfile.splitfilename(previous) local dir, name, ext = nextfile.splitfilename(previous)
print(dir)
print(name) if tonumber(name) == nil then
print(ext) local lastletter = name:sub(#name)
return dir .. name:gsub() .. ext
end
return "" return dir .. name + 1 .. ext
end end
return nextfile return nextfile