From 4ed5fddc914bf502d1729c8f184670763df577fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 10 Oct 2023 18:04:26 +0800 Subject: update utility --- script/utility.lua | 62 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/script/utility.lua b/script/utility.lua index b383a171..936726f9 100644 --- a/script/utility.lua +++ b/script/utility.lua @@ -190,38 +190,48 @@ function m.dump(tbl, option) end --- 递归判断A与B是否相等 ----@param a any ----@param b any +---@param valueA any +---@param valueB any ---@return boolean -function m.equal(a, b) - local tp1 = type(a) - local tp2 = type(b) - if tp1 ~= tp2 then - return false - end - if tp1 == 'table' then - local mark = {} - for k, v in pairs(a) do - mark[k] = true - local res = m.equal(v, b[k]) - if not res then - return false - end +function m.equal(valueA, valueB) + local hasChecked = {} + + local function equal(a, b) + local tp1 = type(a) + local tp2 = type(b) + if tp1 ~= tp2 then + return false end - for k in pairs(b) do - if not mark[k] then - return false + if tp1 == 'table' then + if hasChecked[a] then + return true + end + hasChecked[a] = true + local mark = {} + for k, v in pairs(a) do + mark[k] = true + local res = equal(v, b[k]) + if not res then + return false + end + end + for k in pairs(b) do + if not mark[k] then + return false + end end - end - return true - elseif tp1 == 'number' then - if mathAbs(a - b) <= 1e-10 then return true + elseif tp1 == 'number' then + if mathAbs(a - b) <= 1e-10 then + return true + end + return tostring(a) == tostring(b) + else + return a == b end - return tostring(a) == tostring(b) - else - return a == b end + + return equal(valueA, valueB) end local function sortTable(tbl) -- cgit v1.2.3