table.filter bug fix

This commit is contained in:
Xertis 2025-03-06 15:54:40 +03:00 committed by GitHub
parent cbf07a6351
commit 0d9cd6fefc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -151,9 +151,27 @@ function table.map(t, func)
end
function table.filter(t, func)
for i = #t, 1, -1 do
if not func(i, t[i]) then
table.remove(t, i)
end
end
local size = #t
for i, v in pairs(t) do
if not func(i, v) then
t[i] = nil
local i_type = type(i)
if i_type == "number" then
if i < 1 or i > size then
if not func(i, v) then
t[i] = nil
end
end
else
if not func(i, v) then
t[i] = nil
end
end
end