mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
small refactor
This commit is contained in:
parent
54ef1b0711
commit
282800b478
27
inspect.lua
27
inspect.lua
@ -27,7 +27,7 @@ local function unescape(str)
|
||||
end
|
||||
|
||||
local function isIdentifier(str)
|
||||
return string.match( str, "^[_%a][_%a%d]*$" )
|
||||
return type(str) == 'string' and str:match( "^[_%a][_%a%d]*$" )
|
||||
end
|
||||
|
||||
local function isArrayKey(k, length)
|
||||
@ -91,21 +91,13 @@ function Inspector:new(v, depth)
|
||||
}
|
||||
}
|
||||
|
||||
setmetatable( inspector, {
|
||||
setmetatable( inspector, {
|
||||
__index = Inspector,
|
||||
__tostring = function(instance) return table.concat(instance.buffer) end
|
||||
} )
|
||||
return inspector:putValue(v)
|
||||
end
|
||||
|
||||
function Inspector:puts(...)
|
||||
local args = {...}
|
||||
for i=1, #args do
|
||||
table.insert(self.buffer, tostring(args[i]))
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function Inspector:tabify()
|
||||
self:puts("\n", string.rep(" ", self.level))
|
||||
return self
|
||||
@ -119,6 +111,14 @@ function Inspector:down()
|
||||
self.level = self.level + 1
|
||||
end
|
||||
|
||||
function Inspector:puts(...)
|
||||
local args = {...}
|
||||
for i=1, #args do
|
||||
table.insert(self.buffer, tostring(args[i]))
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function Inspector:putComma(comma)
|
||||
if comma then self:puts(',') end
|
||||
return true
|
||||
@ -160,7 +160,7 @@ function Inspector:putTable(t)
|
||||
self:tabify():puts('<metatable> = '):putValue(mt)
|
||||
end
|
||||
self:up()
|
||||
|
||||
|
||||
if #dictKeys > 0 or mt then -- dictionary table. Justify closing }
|
||||
self:tabify()
|
||||
elseif length > 0 then -- array tables have one extra space before closing }
|
||||
@ -202,10 +202,9 @@ function Inspector:putValue(v)
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
function Inspector:putKey(k)
|
||||
if type(k) == "string" and isIdentifier(k) then
|
||||
return self:puts(k)
|
||||
end
|
||||
if isIdentifier(k) then return self:puts(k) end
|
||||
return self:puts( "[" ):putValue(k):puts("]")
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user