mirror of
https://github.com/neovim/neovim.git
synced 2025-12-15 19:05:40 +00:00
feat(lua): vim.tbl_contains supports general tables and predicates (#23040)
* feat(lua): vim.tbl_contains supports general tables and predicates Problem: `vim.tbl_contains` only works for list-like tables (integer keys without gaps) and primitive values (in particular, not for nested tables). Solution: Rename `vim.tbl_contains` to `vim.list_contains` and add new `vim.tbl_contains` that works for general tables and optionally allows `value` to be a predicate function that is checked for every key.
This commit is contained in:
@@ -461,6 +461,22 @@ describe('lua stdlib', function()
|
||||
pcall_err(exec_lua, [[return vim.pesc(2)]]))
|
||||
end)
|
||||
|
||||
it('vim.list_contains', function()
|
||||
eq(true, exec_lua("return vim.list_contains({'a','b','c'}, 'c')"))
|
||||
eq(false, exec_lua("return vim.list_contains({'a','b','c'}, 'd')"))
|
||||
end)
|
||||
|
||||
it('vim.tbl_contains', function()
|
||||
eq(true, exec_lua("return vim.tbl_contains({'a','b','c'}, 'c')"))
|
||||
eq(false, exec_lua("return vim.tbl_contains({'a','b','c'}, 'd')"))
|
||||
eq(true, exec_lua("return vim.tbl_contains({[2]='a',foo='b',[5] = 'c'}, 'c')"))
|
||||
eq(true, exec_lua([[
|
||||
return vim.tbl_contains({ 'a', { 'b', 'c' } }, function(v)
|
||||
return vim.deep_equal(v, { 'b', 'c' })
|
||||
end, { predicate = true })
|
||||
]]))
|
||||
end)
|
||||
|
||||
it('vim.tbl_keys', function()
|
||||
eq({}, exec_lua("return vim.tbl_keys({})"))
|
||||
for _, v in pairs(exec_lua("return vim.tbl_keys({'a', 'b', 'c'})")) do
|
||||
|
||||
Reference in New Issue
Block a user