refactor(utils): update `in_table` function

main
olimorris 2024-08-09 08:59:48 +01:00
parent 98cd3de512
commit 12de2ac0cf
1 changed files with 9 additions and 25 deletions

View File

@ -57,33 +57,17 @@ function M.dirs_match(dir, dirs)
return false return false
end end
---Check if a string matches and entry in a given table ---Check if a value exists in a table
---@param val string ---@param val any The value to search for
---@param tbl table ---@param tbl table The table to search in
---@param callback function
---@return boolean ---@return boolean
function M.in_table(val, tbl, callback) function M.in_table(val, tbl)
if val == nil then for _, v in pairs(tbl) do
if v == val then
return true
end
end
return false return false
end
return tbl
and next(vim.tbl_filter(function(pattern)
if pattern.exact then
pattern = pattern[1]
-- Stripping off the trailing backslash that a user might put here,
-- but only if we aren't looking at the root directory
if pattern:sub(-1) == M.dir_pattern() and pattern:len() > 1 then
pattern = pattern:sub(1, -2)
end
return val == pattern
else
if callback and type(callback) == "function" then
pattern = callback(pattern)
end
return val:match(pattern)
end
end, tbl))
end end
return M return M