add utils.normalizePath()

develop
myk002 2021-06-29 12:22:05 -07:00
parent 2a54b101bd
commit eafa6e86f7
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
2 changed files with 21 additions and 0 deletions

@ -544,6 +544,13 @@ function check_number(text)
return nv ~= nil, nv
end
-- Normalize directory separator slashes across platforms to '/' and collapse
-- adjacent slashes into a single slash.
local platformSlash = package.config:sub(1,1)
function normalizePath(path)
return path:gsub(platformSlash, '/'):gsub('/+', '/')
end
function invert(tab)
local result = {}
for k,v in pairs(tab) do

@ -14,6 +14,20 @@ function test.OrderedTable()
end
end
function test.normalizePath()
expect.eq('imapath/file.csv', utils.normalizePath('imapath/file.csv'))
expect.eq('/ima/path', utils.normalizePath('/ima/path'))
expect.eq('ima/path', utils.normalizePath('ima//path'))
expect.eq('imapath', utils.normalizePath('imapath'))
expect.eq('/imapath', utils.normalizePath('/imapath'))
expect.eq('/imapath', utils.normalizePath('//imapath'))
expect.eq('/imapath', utils.normalizePath('///imapath'))
expect.eq('imapath/', utils.normalizePath('imapath/'))
expect.eq('imapath/', utils.normalizePath('imapath//'))
end
function test.invert()
local t = {}
local i = utils.invert{'a', 4.4, false, true, 5, t}