From a876678dea903e2a35b6f479606407f19f13b5f7 Mon Sep 17 00:00:00 2001 From: The Magician Date: Sat, 18 Nov 2023 19:57:01 +0000 Subject: [PATCH] Add nextletter function --- spec/nextfile_spec.lua | 30 ++++++++++++++++++++++++++++-- src/nextfile.lua | 18 ++++++++++++++---- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/spec/nextfile_spec.lua b/spec/nextfile_spec.lua index 78a9a62..7b4e430 100644 --- a/spec/nextfile_spec.lua +++ b/spec/nextfile_spec.lua @@ -18,6 +18,22 @@ describe("splitfilename", function() 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() local prevfile = require "prevfile" local nextfile = require "nextfile" @@ -44,10 +60,20 @@ describe("nextfile", function() insulate("prevfile.prevfile returns a numeric filename", function() 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") - --assert.are.equal("/testdir/702.txt", file) + --assert.are.equal("/testdir/421c.txt", file) --end) end) end) diff --git a/src/nextfile.lua b/src/nextfile.lua index 695e4a9..e9ada7f 100644 --- a/src/nextfile.lua +++ b/src/nextfile.lua @@ -7,16 +7,26 @@ function nextfile.splitfilename(path) return dir, name, ext 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) local previous = prevfile.prevfile(directory) if previous == nil then return nil end local dir, name, ext = nextfile.splitfilename(previous) - print(dir) - print(name) - print(ext) + + if tonumber(name) == nil then + local lastletter = name:sub(#name) + return dir .. name:gsub() .. ext + end - return "" + return dir .. name + 1 .. ext end return nextfile