diff --git a/lua/pl/utils.lua b/lua/pl/utils.lua index fed880c7..39ca4464 100644 --- a/lua/pl/utils.lua +++ b/lua/pl/utils.lua @@ -360,7 +360,7 @@ function utils.enum(...) if type(first) ~= "table" then -- vararg with strings lst = utils.pack(...) - for i, value in ipairs(lst) do + for i, value in utils.npairs(lst) do utils.assert_arg(i, value, "string") enum[value] = value end @@ -378,17 +378,15 @@ function utils.enum(...) enum[value] = value end -- add key-ed part - for key, value in pairs(first) do - if not lst[key] then - if type(key) ~= "string" then - error(("expected key to be 'string' but got '%s'"):format(type(key)), 2) - end - if enum[key] then - error(("duplicate entry in array and hash part: '%s'"):format(key), 2) - end - enum[key] = value - lst[#lst+1] = key + for key, value in utils.kpairs(first) do + if type(key) ~= "string" then + error(("expected key to be 'string' but got '%s'"):format(type(key)), 2) + end + if enum[key] then + error(("duplicate entry in array and hash part: '%s'"):format(key), 2) end + enum[key] = value + lst[#lst+1] = key end end diff --git a/spec/utils-enum_spec.lua b/spec/utils-enum_spec.lua index 21fde627..dbe69541 100644 --- a/spec/utils-enum_spec.lua +++ b/spec/utils-enum_spec.lua @@ -24,6 +24,10 @@ describe("pl.utils", function () assert.has.error(function() t = enum("hello", true, "world") end, "argument 2 expected a 'string', got a 'boolean'") + -- no holes + assert.has.error(function() + t = enum("hello", nil, "world") + end, "argument 2 expected a 'string', got a 'nil'") end)