mirror of
https://github.com/TangentFoxy/inspect.lua.git
synced 2025-07-28 02:52:18 +00:00
unindent in specs
This commit is contained in:
39
spec/unindent.lua
Normal file
39
spec/unindent.lua
Normal file
@@ -0,0 +1,39 @@
|
||||
-- Unindenting transforms a string like this:
|
||||
-- [[
|
||||
-- {
|
||||
-- foo = 1,
|
||||
-- bar = 2
|
||||
-- }
|
||||
-- ]]
|
||||
--
|
||||
-- Into the same one without indentation, nor start/end newlines
|
||||
--
|
||||
-- [[{
|
||||
-- foo = 1,
|
||||
-- bar = 2
|
||||
-- }]]
|
||||
--
|
||||
-- This makes the strings look and read better in the tests
|
||||
--
|
||||
|
||||
local getIndentPreffix = function(str)
|
||||
local level = math.huge
|
||||
local minPreffix = ""
|
||||
local len
|
||||
for preffix in str:gmatch("\n( +)") do
|
||||
len = #preffix
|
||||
if len < level then
|
||||
level = len
|
||||
minPreffix = preffix
|
||||
end
|
||||
end
|
||||
return minPreffix
|
||||
end
|
||||
|
||||
local unindent = function(str)
|
||||
str = str:gsub(" +$", ""):gsub("^ +", "") -- remove spaces at start and end
|
||||
local preffix = getIndentPreffix(str)
|
||||
return (str:gsub("\n" .. preffix, "\n"):gsub("\n$", ""))
|
||||
end
|
||||
|
||||
return unindent
|
Reference in New Issue
Block a user