fix(util): Util.merge now skips nil args

This commit is contained in:
Folke Lemaitre 2023-10-13 11:37:52 +02:00
parent 3769461194
commit 70f764bf73
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 25 additions and 8 deletions

View File

@ -396,22 +396,19 @@ end
---@param ... T
---@return T
function M.merge(...)
local values = { ... }
local ret = values[1]
local ret = select(1, ...)
if ret == vim.NIL then
ret = nil
end
for i = 2, #values, 1 do
local value = values[i]
for i = 2, select("#", ...) do
local value = select(i, ...)
if can_merge(ret) and can_merge(value) then
for k, v in pairs(value) do
ret[k] = M.merge(ret[k], v)
end
elseif value == vim.NIL then
ret = nil
else
elseif value ~= nil then
ret = value
end
end

View File

@ -101,6 +101,22 @@ describe("util", function()
input = { { a = 1 }, { b = 2 } },
output = { a = 1, b = 2 },
},
{
input = { nil, { a = 1 }, { b = 2 } },
output = { a = 1, b = 2 },
},
{
input = { { a = 1 }, { b = 2 }, nil },
output = { a = 1, b = 2 },
},
{
input = { { a = 1 }, nil, { b = 2 } },
output = { a = 1, b = 2 },
},
{
input = { nil, { a = 1 }, nil, { b = 2 }, nil },
output = { a = 1, b = 2 },
},
{
input = { { a = 1 }, { a = 2 } },
output = { a = 2 },
@ -120,7 +136,11 @@ describe("util", function()
}
for _, test in ipairs(tests) do
assert.same(test.output, Util.merge(unpack(test.input)))
local n = 0
for i in pairs(test.input) do
n = math.max(n, i)
end
assert.same(test.output, Util.merge(unpack(test.input, 1, n)))
end
end)
end)