utils/merge_table: Handle another edgecase

This commit is contained in:
Akianonymus 2021-08-19 16:40:19 +05:30 committed by siduck76
parent 224873b937
commit 7244492b36
1 changed files with 15 additions and 14 deletions

View File

@ -212,7 +212,7 @@ end
M.merge_table = function(into, from) M.merge_table = function(into, from)
-- make sure both are table -- make sure both are table
if type(into) ~= "table" or type(from) ~= "table" then if type(into) ~= "table" or type(from) ~= "table" then
return 1 return into
end end
local stack, seen = {}, {} local stack, seen = {}, {}
local table1, table2 = into, from local table1, table2 = into, from
@ -223,20 +223,21 @@ M.merge_table = function(into, from)
else else
local present = seen[v] or false local present = seen[v] or false
if not present then if not present then
if type(k) == "number" then
-- add the value to seen table until value is found -- add the value to seen table until value is found
-- only do when key is number we just want to append to subtables
-- todo: maybe improve this -- todo: maybe improve this
for _, value in pairs(table1) do for _, value in pairs(table1) do
if value == v then if value == v then
present = true present = true
break break
end end
end end
end
seen[v] = true seen[v] = true
if not present then if not present then
-- if type is number, then it is a sub table value, so append
if type(k) == "number" then
table1[#table1 + 1] = v table1[#table1 + 1] = v
end
else else
table1[k] = v table1[k] = v
end end