Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pretty) error in pl.pretty table sort function #430

Merged
merged 2 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ see [CONTRIBUTING.md](CONTRIBUTING.md#release-instructions-for-a-new-version) fo
[#431](https://github.com/lunarmodules/Penlight/pull/431)
- feat: `app.require_here` now follows symlink'd main modules to their directory
[#423](https://github.com/lunarmodules/Penlight/pull/423)
- fix: `pretty.write` invalid order function for sorting
[#430](https://github.com/lunarmodules/Penlight/pull/430)
- fix: `compat.warn` raised write guard warning in OpenResty
[#414](https://github.com/lunarmodules/Penlight/pull/414)
- feat: `utils.enum` now accepts hash tables, to enable better error handling
Expand Down
7 changes: 4 additions & 3 deletions lua/pl/pretty.lua
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,11 @@ function pretty.write (tbl,space,not_clever)
end
end
table.sort(ordered_keys, function (a, b)
if type(a) == type(b) and type(a) == 'string' then
return a < b
if type(a) == type(b) then
return tostring(a) < tostring(b)
sveyret marked this conversation as resolved.
Show resolved Hide resolved
else
return type(a) < type(b)
end
return type(a) == 'boolean' or (type(b) ~= 'boolean' and type(a) == 'table')
end)
local function write_entry (key, val)
local tkey = type(key)
Expand Down
4 changes: 2 additions & 2 deletions tests/test-pretty.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ asserteq( pretty.write(t1,""), [[{{},{}}]] )

-- Check that write correctly print table with non number or string as keys

t1 = { [true] = "boolean", a = "a", b = "b", [1] = 1, [0] = 0 }
asserteq( pretty.write(t1,""), [[{1,["true"]="boolean",a="a",b="b",[0]=0}]] )
t1 = { [true] = "boolean", [false] = "untrue", a = "a", b = "b", [1] = 1, [0] = 0 }
asserteq( pretty.write(t1,""), [[{1,["false"]="untrue",["true"]="boolean",a="a",b="b",[0]=0}]] )


-- Check number formatting
Expand Down