diff --git a/standard/table.lua b/standard/table.lua index b321e76252a..3a18527fc99 100644 --- a/standard/table.lua +++ b/standard/table.lua @@ -27,11 +27,13 @@ end ---@param tbl table ---@param value any +---@param isPattern boolean? ---@return boolean -function Table.includes(tbl, value) +function Table.includes(tbl, value, isPattern) for _, entry in pairs(tbl) do - if entry == value then - return true + if isPattern and string.find(entry, value) + or not isPattern and entry == value then + return true end end return false diff --git a/standard/test/table_test.lua b/standard/test/table_test.lua index caf84b5d55e..aad7d8d00dc 100644 --- a/standard/test/table_test.lua +++ b/standard/test/table_test.lua @@ -135,6 +135,20 @@ function suite:testIncludes() self:assertTrue(Table.includes(b, 'testValue3')) self:assertFalse(Table.includes(a, 'testValue4')) self:assertFalse(Table.includes(b, 'testValue4')) + + self:assertTrue(Table.includes(a, 'testValue', false)) + self:assertTrue(Table.includes(b, 'testValue', false)) + self:assertTrue(Table.includes({'^estValue3$'}, '^estValue3$', false)) + self:assertFalse(Table.includes(b, 'estValue', false)) + + self:assertTrue(Table.includes(a, 'testValue', true)) + self:assertTrue(Table.includes(b, 'testValue', true)) + self:assertTrue(Table.includes(b, 'testValue3', true)) + self:assertTrue(Table.includes(b, 'estValue3', true)) + self:assertTrue(Table.includes(b, 'testValue%d', true)) + self:assertTrue(Table.includes(b, '^testValue%d$', true)) + self:assertFalse(Table.includes(b, '^estValue3$', true)) + self:assertFalse(Table.includes({'^estValue3$'}, '^estValue3$', true)) end function suite:testFilterByKey()