Table: Replacing the Voxrames version of table.join with ours

This commit is contained in:
Van
2026-04-03 21:43:43 +03:00
parent f5fba0b779
commit 91603e21f8

View File

@@ -170,34 +170,28 @@ function table.merge(t1, t2, recursively)
return result
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
--- Appends a second list to the end of the first list,
--- thereby concatenating the two lists together.
--- @generic t
--- @param t1 t[]
--- @param t2 t[]
--- @return t[]
function table.join(t1, t2)
ensureArgType(t1, 'table', 1, 'table.join')
ensureArgType(t2, 'table', 2, 'table.join')
local result = {}
for i, v in ipairs(t1) do
result[i] = v
end
return t1
local offset = #t1
for i, v in ipairs(t2) do
result[offset + i] = v
end
return result
end
--- Returns an array containing only unique values of the table `t`.