refactor(iter): use metatable as packed table tag (#23254)

This is a more robust method for tagging a packed table as it completely
eliminates the possibility of mistaking an actual table key as the
packed table tag.
This commit is contained in:
Gregory Anders
2023-04-21 16:13:39 -06:00
committed by GitHub
parent ef92b5a994
commit f68af3c3bc
2 changed files with 55 additions and 15 deletions

View File

@@ -3416,6 +3416,41 @@ describe('lua stdlib', function()
{ item_3 = 'test' },
}, output)
end)
it('handles nil values', function()
local t = {1, 2, 3, 4, 5}
do
local it = vim.iter(t):enumerate():map(function(i, v)
if i % 2 == 0 then
return nil, v*v
end
return v, nil
end)
eq({
{ [1] = 1 },
{ [2] = 4 },
{ [1] = 3 },
{ [2] = 16 },
{ [1] = 5 },
}, it:totable())
end
do
local it = vim.iter(ipairs(t)):map(function(i, v)
if i % 2 == 0 then
return nil, v*v
end
return v, nil
end)
eq({
{ [1] = 1 },
{ [2] = 4 },
{ [1] = 3 },
{ [2] = 16 },
{ [1] = 5 },
}, it:totable())
end
end)
end)
end)