Skip to content

Commit

Permalink
feat(pretty) force ordered output in pretty.write (#293)
Browse files Browse the repository at this point in the history
Having consistent output makes it easier to use it for test purposes,
such as when dumping tables and comparing it to some expected output.
  • Loading branch information
hth313 authored and Tieske committed May 14, 2019
1 parent 42f18f0 commit 9aecd3f
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lua/pl/pretty.lua
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,14 @@ function pretty.write (tbl,space,not_clever)
used[i] = true
end
end
for key,val in pairs(t) do
local ordered_keys = {}
for k,v in pairs(t) do
if type(k) ~= 'number' then
ordered_keys[#ordered_keys + 1] = k
end
end
table.sort(ordered_keys)
local function write_entry (key, val)
local tkey = type(key)
local numkey = tkey == 'number'
if not_clever then
Expand All @@ -268,6 +275,16 @@ function pretty.write (tbl,space,not_clever)
end
end
end
for i = 1, #ordered_keys do
local key = ordered_keys[i]
local val = t[key]
write_entry(key, val)
end
for key,val in pairs(t) do
if type(key) == 'number' then
write_entry(key, val)
end
end
tables[t] = nil
eat_last_comma()
putln(oldindent..'},')
Expand Down

0 comments on commit 9aecd3f

Please sign in to comment.