Skip to content

Commit

Permalink
fix(util): Util.merge now skips nil args
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 13, 2023
1 parent 3769461 commit 70f764b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
11 changes: 4 additions & 7 deletions lua/lazy/core/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion tests/core/util_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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)

0 comments on commit 70f764b

Please sign in to comment.