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

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

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