From aa85f75d6d1c2a799a167cded435173025424af7 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Sun, 10 Jul 2022 13:07:11 +0200 Subject: [PATCH] Add table.merge --- table.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/table.lua b/table.lua index bd04527..e60bcc8 100644 --- a/table.lua +++ b/table.lua @@ -538,6 +538,25 @@ function deepcomplete(table, completions) return table end +function merge(table, other_table, merge_func) + merge_func = merge_func or merge + local res = {} + for key, value in pairs(table) do + local other_value = other_table[key] + if other_value == nil then + res[key] = value + else + res[key] = merge_func(value, other_value) + end + end + for key, value in pairs(other_table) do + if table[key] == nil then + res[key] = value + end + end + return res +end + function merge_tables(table, other_table) return add_all(shallowcopy(table), other_table) end