From ed5d728c0a5a1339e9133709417b03ec6914579a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Rahir=20=28rar=29?= Date: Wed, 22 May 2024 16:46:41 +0200 Subject: [PATCH] [FIX] misc: Fix `deepEquals` behaviour MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since PR 4066, we can compare objects while ignoring functions inside them as the function references might differ. However, the implementation was faulty and would mistakenly consider two object to be equals as soon as they both contained a function for the same key. closes odoo/o-spreadsheet#4246 Task: 3942782 X-original-commit: 1bf375c14fc8a5aa2ad84aa38440786fddbc9fec Signed-off-by: Adrien Minne (adrm) Signed-off-by: RĂ©mi Rahir (rar) --- src/helpers/misc.ts | 4 +++- tests/helpers/misc_helpers.test.ts | 10 ++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/helpers/misc.ts b/src/helpers/misc.ts index 1c2dfab1b..394ed26ea 100644 --- a/src/helpers/misc.ts +++ b/src/helpers/misc.ts @@ -369,7 +369,9 @@ export function deepEquals(o1: any, o2: any, ignoreFunctions?: "ignoreFunctions" if (typeOfO1Key === "object") { if (!deepEquals(o1[key], o2[key], ignoreFunctions)) return false; } else { - if (ignoreFunctions && typeOfO1Key === "function") return true; + if (ignoreFunctions && typeOfO1Key === "function") { + continue; + } if (o1[key] !== o2[key]) return false; } } diff --git a/tests/helpers/misc_helpers.test.ts b/tests/helpers/misc_helpers.test.ts index 30e8b5fec..2031c0e81 100644 --- a/tests/helpers/misc_helpers.test.ts +++ b/tests/helpers/misc_helpers.test.ts @@ -234,10 +234,16 @@ test.each([ }); test("deepEquals with argument ignoring functions", () => { - const o1 = { a: 1, b: () => 2 }; - const o2 = { a: 1, b: () => 2 }; + const o1 = { a: 1, b: () => 2, c: 2 }; + const o2 = { a: 1, b: () => 2, c: 2 }; + const o3 = { a: 1, b: () => 2, c: 3 }; + const o4 = { a: 2, b: () => 2, c: 2 }; expect(deepEquals(o1, o2)).toEqual(false); expect(deepEquals(o1, o2, "ignoreFunctions")).toEqual(true); + expect(deepEquals(o1, o3)).toEqual(false); + expect(deepEquals(o1, o3, "ignoreFunctions")).toEqual(false); + expect(deepEquals(o1, o4)).toEqual(false); + expect(deepEquals(o1, o4, "ignoreFunctions")).toEqual(false); }); describe("isConsecutive", () => {