diff --git a/.tests/path/join.test.lua b/.tests/path/join.test.lua new file mode 100644 index 0000000..34eb35b --- /dev/null +++ b/.tests/path/join.test.lua @@ -0,0 +1,17 @@ +local Path = require("worldeditadditions_core.utils.path") + +describe("Path.join", function() + it("should correct bad formatting", function() + local result, err = Path.join("C:\\Users\\me\\", "/Documents//code.lua") + assert.is_nil(err) + assert.are.same( + table.concat({"C:","Users","me","Documents","code.lua"}, Path.sep), + result + ) + end) + it("should return an error if not a string", function() + local result, err = Path.join("C://Users/me", 123) + assert.is_false(result) + assert.are.same("string", type(err)) + end) +end) \ No newline at end of file diff --git a/.tests/path/new.test.lua b/.tests/path/new.test.lua new file mode 100644 index 0000000..da84029 --- /dev/null +++ b/.tests/path/new.test.lua @@ -0,0 +1,17 @@ +local Path = require("worldeditadditions_core.utils.path") + +describe("Path.new", function() + it("should correct bad formatting", function() + local result, err = Path.new("C:\\Users\\me\\".."/Documents//code.lua") + assert.is_nil(err) + assert.are.same( + table.concat({"C:","Users","me","Documents","code.lua"}, Path.sep), + result + ) + end) + it("should return an error if not a string", function() + local result, err = Path.new(123) + assert.is_false(result) + assert.are.same("string", type(err)) + end) +end) \ No newline at end of file diff --git a/worldeditadditions_core/init.lua b/worldeditadditions_core/init.lua index 213cab7..1ef85e5 100644 --- a/worldeditadditions_core/init.lua +++ b/worldeditadditions_core/init.lua @@ -18,7 +18,8 @@ end worldeditadditions_core = EventEmitter.new({ version = "1.15-dev", - --- The directory separator on the current host system + --- The directory separator on the current host system [[ DEPRECATED ]] + --- Use worldeditadditions_core.path.sep instead -- @value string dirsep = directory_separator, --- The full absolute filepath to the mod worldeditadditions_core @@ -45,6 +46,8 @@ wea_c.EventEmitter = EventEmitter wea_c.notify = dofile(wea_c.modpath.."/utils/notify/notify.lua") -- BEFORE anything that could use this +wea_c.Path = dofile(wea_c.modpath.."/utils/path.lua") -- BEFORE anything that could use this + wea_c.Set = dofile(wea_c.modpath.."/utils/set.lua") wea_c.Vector3 = dofile(wea_c.modpath.."/utils/vector3.lua") @@ -84,7 +87,7 @@ dofile(wea_c.modpath.."/utils/player.lua") -- Player info functions -wea_c.setting_handler = dofile(wea_c.modpath.."/utils/setting_handler.lua") -- AFTER parser +wea_c.settings = dofile(wea_c.modpath.."/utils/setting_handler.lua") -- AFTER parser wea_c.pos = dofile(modpath.."/core/pos.lua") -- AFTER EventEmitter wea_c.safe_function = dofile(modpath.."/core/safe_function.lua") diff --git a/worldeditadditions_core/utils/path.lua b/worldeditadditions_core/utils/path.lua new file mode 100644 index 0000000..407f7c8 --- /dev/null +++ b/worldeditadditions_core/utils/path.lua @@ -0,0 +1,54 @@ +--- A path manipulation class. +-- @class worldeditadditions_core.Path +local path = {} + +-- Helper functions +local check = function( ... ) + for _, v in ipairs( ... ) do + if type(v) ~= "string" then + return false, v .. " is not a string." + end + end + return true +end + +--- The directory separator on the current host system +-- @value string +path.sep = "/" +if package and package.config then + path.sep = package.config:sub(1,1) +end + +--- Format string into path +-- @param str string The string to process +-- @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 ok, err = check(str) + if not ok then return false, err end + return ({str:gsub("[/\\]+", path.sep)})[1] +end + +--- Joins the given path segments into a single path with dirsep. +-- @param ... string The path fragments to process and join. +-- @return string|false, string? The joined path or +-- false and an error message. +-- @example Basic usage +-- local path = file_path("C:\\Users", "me", "/Documents/code.lua") +path.join = function( ... ) + local pathlets = { ... } + local ok, err = check(pathlets) + if not ok then return false, err end + return path.new(table.concat(pathlets, path.sep)) +end + +local Path = {} +Path.__name = "Path" -- A hack to allow easier identification +Path.__index = Path +Path.__call = function(self, ...) + return self.join(...) +end + +return setmetatable(path, Path) \ No newline at end of file diff --git a/worldeditadditions_core/utils/path/init.lua b/worldeditadditions_core/utils/path/init.lua deleted file mode 100644 index a72cade..0000000 --- a/worldeditadditions_core/utils/path/init.lua +++ /dev/null @@ -1,5 +0,0 @@ -local wea_c = worldeditadditions_core - -wea_c.path = { - join = dofile(wea_c.modpath.."/utils/format/join.lua"), -} diff --git a/worldeditadditions_core/utils/path/join.lua b/worldeditadditions_core/utils/path/join.lua deleted file mode 100644 index 6ea8148..0000000 --- a/worldeditadditions_core/utils/path/join.lua +++ /dev/null @@ -1,17 +0,0 @@ ---- --- @module worldeditadditions_core -local wea_c = worldeditadditions_core - ---- Joins the given path segments into a single path with dirsep. --- @param ... string The path fragments to process and join. --- @return string The joined path. --- @example Basic usage --- local path = file_path("C:\\Users", "me", "/Documents/code.lua") -local join = function( ... ) - local path = { ... } - for i, v in ipairs(path) do path[i] = tostring(v) end - return ({table.concat(path, wea_c.dirsep) - :gsub("[/\\]+", wea_c.dirsep)})[1] -end - -return join \ No newline at end of file