Skip to content

Commit

Permalink
Add pattern matching to Table.includes (#3448)
Browse files Browse the repository at this point in the history
* Add optional `isPattern` to `Table.includes`

* Add testcases for `isPattern`

* fix shadowing upvalue, as with `deepEquals()`

* drop `Logic.isNumeric()`
  • Loading branch information
iMarbot authored Nov 6, 2023
1 parent dd56053 commit d070d5c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
8 changes: 5 additions & 3 deletions standard/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions standard/test/table_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit d070d5c

Please sign in to comment.