mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
Fix float keys being masked in tables with array elements
This commit is contained in:
parent
99deb522f8
commit
182e6f28c1
21
inspect.lua
21
inspect.lua
@ -56,12 +56,11 @@ local function isIdentifier(str)
|
||||
return type(str) == 'string' and str:match( "^[_%a][_%a%d]*$" )
|
||||
end
|
||||
|
||||
local function isArrayKey(k, length)
|
||||
return type(k) == 'number' and 1 <= k and k <= length
|
||||
end
|
||||
|
||||
local function isDictionaryKey(k, length)
|
||||
return not isArrayKey(k, length)
|
||||
local function isSequenceKey(k, length)
|
||||
return type(k) == 'number'
|
||||
and 1 <= k
|
||||
and k <= length
|
||||
and math.floor(k) == k
|
||||
end
|
||||
|
||||
local defaultTypeOrders = {
|
||||
@ -86,10 +85,10 @@ local function sortKeys(a, b)
|
||||
return ta < tb
|
||||
end
|
||||
|
||||
local function getDictionaryKeys(t)
|
||||
local function getNonSequentialKeys(t)
|
||||
local keys, length = {}, #t
|
||||
for k,_ in pairs(t) do
|
||||
if isDictionaryKey(k, length) then table.insert(keys, k) end
|
||||
if not isSequenceKey(k, length) then table.insert(keys, k) end
|
||||
end
|
||||
table.sort(keys, sortKeys)
|
||||
return keys
|
||||
@ -234,7 +233,7 @@ function Inspector:putTable(t)
|
||||
else
|
||||
if self.tableAppearances[t] > 1 then self:puts('<', self:getId(t), '>') end
|
||||
|
||||
local dictKeys = getDictionaryKeys(t)
|
||||
local nonSequentialKeys = getNonSequentialKeys(t)
|
||||
local length = #t
|
||||
local mt = getmetatable(t)
|
||||
local to_string_result = getToStringResultSafely(t, mt)
|
||||
@ -254,7 +253,7 @@ function Inspector:putTable(t)
|
||||
count = count + 1
|
||||
end
|
||||
|
||||
for _,k in ipairs(dictKeys) do
|
||||
for _,k in ipairs(nonSequentialKeys) do
|
||||
if count > 0 then self:puts(',') end
|
||||
self:tabify()
|
||||
self:putKey(k)
|
||||
@ -272,7 +271,7 @@ function Inspector:putTable(t)
|
||||
end
|
||||
end)
|
||||
|
||||
if #dictKeys > 0 or mt then -- dictionary table. Justify closing }
|
||||
if #nonSequentialKeys > 0 or mt then -- result is multi-lined. Justify closing }
|
||||
self:tabify()
|
||||
elseif length > 0 then -- array tables have one extra space before closing }
|
||||
self:puts(' ')
|
||||
|
@ -80,6 +80,11 @@ describe( 'inspect', function()
|
||||
assert.equals(inspect({a = 1, b = 2}), "{\n a = 1,\n b = 2\n}")
|
||||
end)
|
||||
|
||||
it('identifies numeric non-array keys as dictionary keys', function()
|
||||
assert.equals(inspect({1, 2, [-1] = true}), "{ 1, 2,\n [-1] = true\n}")
|
||||
assert.equals(inspect({1, 2, [1.5] = true}), "{ 1, 2,\n [1.5] = true\n}")
|
||||
end)
|
||||
|
||||
it('sorts keys in dictionary tables', function()
|
||||
local t = { 1,2,3,
|
||||
[print] = 1, ["buy more"] = 1, a = 1,
|
||||
|
Loading…
Reference in New Issue
Block a user