diff --git a/inspect.lua b/inspect.lua index 1e1c0b0..f656755 100644 --- a/inspect.lua +++ b/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(' ') diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index f5ee147..744631e 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -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,