Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/stevedonovan/Penlight.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Melnichenko <mpeterval@gmail.com>2018-09-21 13:29:50 +0300
committerPeter Melnichenko <mpeterval@gmail.com>2018-09-21 13:29:50 +0300
commit5096665f97ad47949ce79ade080f0198caa2ddb7 (patch)
tree71edb4a712f7dc1b022e89b5cfa841258080b144
parent5ca96a01124f04906ccde2b00f03ae6596c268b1 (diff)
Fix types.is_empty returning true on a table with false as a key
Ref #267.
-rw-r--r--lua/pl/types.lua9
-rw-r--r--tests/test-types.lua1
2 files changed, 8 insertions, 2 deletions
diff --git a/lua/pl/types.lua b/lua/pl/types.lua
index f4ab236..a62e4e3 100644
--- a/lua/pl/types.lua
+++ b/lua/pl/types.lua
@@ -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)
diff --git a/tests/test-types.lua b/tests/test-types.lua
index 075b970..42a280c 100644
--- a/tests/test-types.lua
+++ b/tests/test-types.lua
@@ -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)