Skip to content

Commit

Permalink
update(bin/scripting): switch, table.map, table.filter, and table.con…
Browse files Browse the repository at this point in the history
…tains
  • Loading branch information
m3ntorsky committed Oct 14, 2024
1 parent 5059eee commit 6f77806
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions plugin_files/bin/scripting/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,62 @@ end
--- @return string
string.trim = function(str)
return (str:gsub("^%s*(.-)%s*$", "%1"))
end


--- @param value any The value to be matched against. Can be a number or a string.
--- @param cases table
--- @return any Returns
switch = function(value, cases)
local case = cases[value]
if case then
if type(case) == "function" then
return case()
else
return case
end
elseif cases.default then
if type(cases.default) == "function" then
return cases.default()
else
return cases.default
end
end
end

--- @param tbl table The table to be mapped.
--- @param func function A function that takes a value and key as parameters and returns a new value.
--- @return table A new table with the transformed values.
table.map = function(tbl, func)
local result = {}
for key, value in next, tbl do
result[key] = func(value, key)
end
return result
end

--- @param tbl table The table to be filtered.
--- @param predicate function A function that takes a value and key as parameters and returns a boolean.
--- @return table A new table containing only the elements for which the predicate returned true.
table.filter = function(tbl, predicate)
local result = {}
local index = 1
for key, value in next, tbl do
if predicate(value, key) then
result[index] = value
index = index + 1
end
end
return result
end
--- @param tbl table The table to search.
--- @param value any The value to search for.
--- @return boolean True if the value exists in the table, false otherwise.
table.contains = function(tbl, value)
for _, val in next, tbl do
if val == value then
return true
end
end
return false
end

0 comments on commit 6f77806

Please sign in to comment.