cc-automation/util.lua
2025-05-15 23:41:47 +02:00

87 lines
1.6 KiB
Lua

local util = {}
function util.deepEqual(a, b)
local varType = type(a)
if varType ~= type(b) then
return false
end
if varType == "table" then
for key,_ in pairs(b) do
if a[key] == nil then
return false
end
end
for key,value in pairs(a) do
if not util.deepEqual(value, b[key]) then
return false
end
end
return true
end
return a == b
end
function util.find(array, f)
for k,v in pairs(array) do
if f(v, k) then
return v
end
end
return nil
end
function util.findAll(array, f)
local matches = {}
for k,v in pairs(array) do
if f(v, k) then
table.insert(matches, v)
end
end
return matches
end
function util.isEmpty(tbl)
local next, t = pairs(tbl)
return next(t) == nil
end
function util.fuzzyEquals(value, pattern)
local valueType = type(value)
local patternType = type(pattern)
if patternType == "nil" then
return true
end
if patternType == "function" then
return pattern(value)
end
if valueType ~= patternType then
return false
end
if valueType == "table" then
for k,_ in pairs(pattern) do
if value[k] == nil then
return false
end
end
for k,v in pairs(value) do
if not util.fuzzyEquals(v, pattern[k]) then
return false
end
end
return true
end
return value == pattern
end
return util