From d58b6f9148a6c36d46d5057c4a81af5ec4449a65 Mon Sep 17 00:00:00 2001 From: The Magician Date: Sat, 18 Nov 2023 18:05:01 +0000 Subject: [PATCH] Change basename to splitfilename, return extension --- spec/nextfile_spec.lua | 26 ++++++++++++++------------ src/nextfile.lua | 14 +++++++++----- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/spec/nextfile_spec.lua b/spec/nextfile_spec.lua index 421a52b..a6005de 100644 --- a/spec/nextfile_spec.lua +++ b/spec/nextfile_spec.lua @@ -1,16 +1,18 @@ -describe("basename", function() +describe("splitfilename", function() local nextfile = require "nextfile" - it("should return the basename of an absolute filepath", function() - local name = nextfile.basename("/testdir/701.txt") + it("should return the filename and extension of an absolute filepath", function() + local name, ext = nextfile.splitfilename("/testdir/701.txt") assert.are.equal("701", name) + assert.are.equal("txt", ext) end) - it("should return the basename of a relative filepath", function() - local name = nextfile.basename("../../foo/testdir/file.mp3") + it("should return the filename and extension of a relative filepath", function() + local name, ext = nextfile.splitfilename("../../foo/testdir/file.mp3") assert.are.equal("file", name) + assert.are.equal("mp3", ext) end) end) @@ -37,13 +39,13 @@ describe("nextfile", function() end) end) --- insulate("prevfile.prevfile returns a numeric filename", function() --- prevfile.prevfile = function() return "/testdir/701.txt" end + --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() --- local file = nextfile.nextfile("/testdir") + --it("should return the result of incrementing the filename", function() + --local file = nextfile.nextfile("/testdir") -- --- assert.are.equal("/testdir/702.txt", file) --- end) --- end) + --assert.are.equal("/testdir/702.txt", file) + --end) + --end) end) diff --git a/src/nextfile.lua b/src/nextfile.lua index e0f6735..2464595 100644 --- a/src/nextfile.lua +++ b/src/nextfile.lua @@ -2,14 +2,18 @@ local prevfile = require "prevfile" local nextfile = {} -function nextfile.basename(path) - return path:match("^.*/(.*)%.%w*") - +function nextfile.splitfilename(path) + name, ext = path:match("^.*/(.*)%.(%w*)") + return name, ext end function nextfile.nextfile(directory) - prevfile.prevfile(directory) - return nil + local previous = prevfile.prevfile(directory) + if previous == nil then return nil end + + local name, ext = nextfile.splitfilename(previous) + + return "" end return nextfile