Skip to content

Commit

Permalink
Add ability to check multiple items to table.contains
Browse files Browse the repository at this point in the history
  • Loading branch information
demonnic committed Mar 29, 2020
1 parent 333fd70 commit 6a3579c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/mudlet-lua/lua/TableUtils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ end


--- Determines if a table contains a value as a key or as a value (recursive).
function table.contains(t, value)
function table._contains(t, value)
if type(t) ~= "table" then
return nil, "first parameter passed isn't a table"
end
Expand All @@ -142,6 +142,12 @@ function table.contains(t, value)
return false
end

function table.contains(tbl, ...)
for _,item in ipairs({...}) do
if table._contains(tbl, item) then return true end
end
return false
end


--- Table Union.
Expand Down
20 changes: 20 additions & 0 deletions src/mudlet-lua/tests/TableUtils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ describe("Tests TableUtils.lua functions", function()
assert.is_true(table.contains(tbl, "levels"))
end)

it("should return true if any of the passed arguments is in the table", function()
local tbl = {
one = 1,
two = 2,
three = {
{
ludicrous = {
levels = {
of = {
buried = "beeblebrox"
}
}
}
}
}
}
assert.is_true(table.contains(tbl, "transparent", "things", "buried"))
end)

it("should return false if item is not in the table at all", function()
local tbl = {
one = 1,
Expand All @@ -151,6 +170,7 @@ describe("Tests TableUtils.lua functions", function()
}
assert.is_false(table.contains(tbl, "five"))
end)

end)

describe("table.index_of(tbl,item)", function()
Expand Down

0 comments on commit 6a3579c

Please sign in to comment.