Skip to content

Commit

Permalink
[FIX] misc: Fix deepEquals behaviour
Browse files Browse the repository at this point in the history
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 #4246

Task: 3942782
X-original-commit: 1bf375c
Signed-off-by: Adrien Minne (adrm) <adrm@odoo.com>
Signed-off-by: Rémi Rahir (rar) <rar@odoo.com>
  • Loading branch information
rrahir committed May 23, 2024
1 parent faadb90 commit ed5d728
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/helpers/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
10 changes: 8 additions & 2 deletions tests/helpers/misc_helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit ed5d728

Please sign in to comment.