From 7244492b3635667d8e854f58ca3a80aeec653ef4 Mon Sep 17 00:00:00 2001 From: Akianonymus Date: Thu, 19 Aug 2021 16:40:19 +0530 Subject: [PATCH] utils/merge_table: Handle another edgecase --- lua/utils.lua | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lua/utils.lua b/lua/utils.lua index 58cf816..483db39 100644 --- a/lua/utils.lua +++ b/lua/utils.lua @@ -212,7 +212,7 @@ end M.merge_table = function(into, from) -- make sure both are table if type(into) ~= "table" or type(from) ~= "table" then - return 1 + return into end local stack, seen = {}, {} local table1, table2 = into, from @@ -223,20 +223,21 @@ M.merge_table = function(into, from) else local present = seen[v] or false if not present then - -- add the value to seen table until value is found - -- todo: maybe improve this - for _, value in pairs(table1) do - if value == v then - present = true - break - end - end - end - seen[v] = true - 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 + -- 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 + + for _, value in pairs(table1) do + if value == v then + present = true + break + end + end + seen[v] = true + if not present then + table1[#table1 + 1] = v + end else table1[k] = v end