Table: Replacing the Voxrames version of table.join with ours
This commit is contained in:
@@ -170,34 +170,28 @@ 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()`.
|
--- Appends a second list to the end of the first list,
|
||||||
---If `recursively` is true, the function will be applied recursively for values
|
--- thereby concatenating the two lists together.
|
||||||
---that are tables in both `table1` and `table2`.
|
--- @generic t
|
||||||
---
|
--- @param t1 t[]
|
||||||
---
|
--- @param t2 t[]
|
||||||
---@overload fun(t1:table, t2:table):table
|
--- @return t[]
|
||||||
---
|
function table.join(t1, t2)
|
||||||
---@generic T: table, U: table
|
ensureArgType(t1, 'table', 1, 'table.join')
|
||||||
---@param t1 T
|
ensureArgType(t2, 'table', 2, 'table.join')
|
||||||
---@param t2 U
|
|
||||||
---@param recursively boolean Default: false. Go recursively if both values are tables.
|
local result = {}
|
||||||
---
|
for i, v in ipairs(t1) do
|
||||||
---Original source: [Voxrame](https://github.com/Voxrame/Voxrame/blob/d5255d466ca502267023c146696a655cf09595b3/helpers/src/lua_ext/table.lua#L161)
|
result[i] = v
|
||||||
---@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
|
end
|
||||||
|
|
||||||
return t1
|
local offset = #t1
|
||||||
|
for i, v in ipairs(t2) do
|
||||||
|
result[offset + i] = v
|
||||||
|
end
|
||||||
|
|
||||||
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Returns an array containing only unique values of the table `t`.
|
--- Returns an array containing only unique values of the table `t`.
|
||||||
|
|||||||
Reference in New Issue
Block a user