Lua: table.select API

This commit is contained in:
Zef Hemel
2026-01-27 09:00:37 +01:00
parent fe800417bc
commit 2e469ba3ea
3 changed files with 99 additions and 24 deletions
+37 -2
View File
@@ -1,9 +1,37 @@
function deepCompare(t1, t2)
if t1 == t2 then return true end
-- If they are the same object, return true
if type(t1) ~= "table" or type(t2) ~= "table" then return false end
-- If not both tables, return false
-- Check if both tables have the same number of keys
local t1_keys = 0
local t2_keys = 0
for k in pairs(t1) do
t1_keys = t1_keys + 1
end
for k in pairs(t2) do
t2_keys = t2_keys + 1
end
if t1_keys ~= t2_keys then return false end
-- Recursively compare each key-value pair
for k, v in pairs(t1) do
if not deepCompare(v, t2[k]) then
return false
end
end
return true
end
local function assertEqual(a, b)
if a ~= b then
error("Assertion failed: " .. a .. " is not equal to " .. b)
if not deepCompare(a, b) then
error("Assertion failed: " .. tostring(a) .. " is not equal to " .. tostring(b))
end
end
-- Basic table operations
local t = { 1, 2, 3 }
table.insert(t, 4)
@@ -155,3 +183,10 @@ local idx, two = table.find(t, function(v) return v == 2 end)
assertEqual(idx, 3)
assertEqual(two, 2)
assertEqual(table.find(t, function(v) return v == 4 end), nil)
-- Test table.select
local tbl = {name = "Pete", age = 100, parents = {"John", "Jane"}}
assertEqual(table.select(tbl, "name"), {name = "Pete"})
assertEqual(table.select(tbl, {"name"}), {name = "Pete"})
assertEqual(table.select(tbl, "name", "age"), { name = "Pete", age = 100})
assertEqual(table.select(tbl, "name", "age", "non-existing"), { name = "Pete", age = 100})