path.new ==> path.norm & fixed errors

This commit is contained in:
VorTechnix 2024-10-25 16:57:49 -07:00
parent 3c88ae9e23
commit 82d8fd8c31
No known key found for this signature in database
GPG key ID: 091E91A69545D5BA
2 changed files with 9 additions and 9 deletions

View file

@ -1,8 +1,8 @@
local Path = require("worldeditadditions_core.utils.path")
describe("Path.new", function()
describe("Path.norm", function()
it("should correct bad formatting", function()
local result, err = Path.new("C:\\Users\\me\\".."/Documents//code.lua")
local result, err = Path.norm("C:\\Users\\me\\".."/Documents//code.lua")
assert.is_nil(err)
assert.are.same(
table.concat({"C:","Users","me","Documents","code.lua"}, Path.sep),
@ -10,7 +10,7 @@ describe("Path.new", function()
)
end)
it("should return an error if not a string", function()
local result, err = Path.new(123)
local result, err = Path.norm(123)
assert.is_false(result)
assert.are.same("string", type(err))
end)

View file

@ -4,9 +4,9 @@ local path = {}
-- Helper functions
local check = function( ... )
for _, v in ipairs( ... ) do
for _, v in ipairs( {...} ) do
if type(v) ~= "string" then
return false, v .. " is not a string."
return false, tostring(v) .. " is not a string."
end
end
return true
@ -24,8 +24,8 @@ end
-- @return string|false, string? The formatted path string or
-- false and an error message.
-- @example Basic usage
-- local path = path.new("C:\\Users\\me\\".."/Documents//code.lua")
path.new = function( str )
-- local path = path.norm("C:\\Users\\me\\".."/Documents//code.lua")
path.norm = function( str )
local ok, err = check(str)
if not ok then return false, err end
return ({str:gsub("[/\\]+", path.sep)})[1]
@ -39,9 +39,9 @@ end
-- local path = file_path("C:\\Users", "me", "/Documents/code.lua")
path.join = function( ... )
local pathlets = { ... }
local ok, err = check(pathlets)
local ok, err = check( ... )
if not ok then return false, err end
return path.new(table.concat(pathlets, path.sep))
return path.norm(table.concat(pathlets, path.sep))
end
local Path = {}