Skip to content

Commit

Permalink
Fix types.is_empty returning true on a table with false as a key
Browse files Browse the repository at this point in the history
Ref #267.
  • Loading branch information
mpeterv committed Sep 21, 2018
1 parent 5ca96a0 commit 5096665
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lua/pl/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,15 @@ end
-- considered empty is it only contains spaces.
-- @return true if the object is empty, otherwise false.
function types.is_empty(o, ignore_spaces)
if o == nil or (type(o) == "table" and not next(o)) or (type(o) == "string" and (o == "" or (ignore_spaces and o:match("^%s+$")))) then
if o == nil then
return true
elseif type(o) == "table" then
return next(o) == nil
elseif type(o) == "string" then
return o == "" or (ignore_spaces and not not o:find("^%s+$"))
else
return true
end
return false
end

local function check_meta (val)
Expand Down
1 change: 1 addition & 0 deletions tests/test-types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ asserteq(types.is_indexable(10),nil)

asserteq(types.is_empty(nil),true)
asserteq(types.is_empty({}),true)
asserteq(types.is_empty({[false] = false}),false)
asserteq(types.is_empty(""),true)
asserteq(types.is_empty(" ",true),true)

Expand Down

0 comments on commit 5096665

Please sign in to comment.