Table: add table.join from Voxrame

This commit is contained in:
Van
2026-04-03 19:18:54 +03:00
parent 362d626f9b
commit 89de48b5a8

View File

@@ -170,6 +170,36 @@ function table.merge(t1, t2, recursively)
return result return result
end end
---Adds key-value from `table2` into `table1` only if that key does not exists in `table1`.
---Values that are tables are copied by `table.copy()`.
---If `recursively` is true, the function will be applied recursively for values
---that are tables in both `table1` and `table2`.
---
---
---@overload fun(t1:table, t2:table):table
---
---@generic T: table, U: table
---@param t1 T
---@param t2 U
---@param recursively boolean Default: false. Go recursively if both values are tables.
---
---Original source: [Voxrame](https://github.com/Voxrame/Voxrame/blob/d5255d466ca502267023c146696a655cf09595b3/helpers/src/lua_ext/table.lua#L161)
---@return T|U
function table.join(t1, t2, recursively)
recursively = recursively or false
for key, value in pairs(t2) do
if not t1[key] then
t1[key] = type(value) == 'table' and tableCopy(value) or value
else
if recursively and type(t1[key]) == 'table' and type(value) == 'table' then
table.join(t1[key], value, recursively)
end
end
end
return t1
end
--- Returns an array containing only unique values of the table `t`. --- Returns an array containing only unique values of the table `t`.
--- @param t any[] Array to check --- @param t any[] Array to check
--- @return any[] --- @return any[]