Skip to content

Commit

Permalink
Merge pull request #1428 from ryuapp/deep-equal-adapt-vnode
Browse files Browse the repository at this point in the history
fix: deepEqual exceed maximum call stack size
  • Loading branch information
afshinm authored Mar 3, 2024
2 parents 2d337e6 + 68724f4 commit 79124b2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/util/deepEqual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ export function deepEqual<A, B>(obj1: A, obj2: B) {
}
return true;
}
// If objects are VNodes, compare their props only
if (
// eslint-disable-next-line no-prototype-builtins
obj1.hasOwnProperty('constructor') &&
// eslint-disable-next-line no-prototype-builtins
obj2.hasOwnProperty('constructor') &&
// eslint-disable-next-line no-prototype-builtins
obj1.hasOwnProperty('props') &&
// eslint-disable-next-line no-prototype-builtins
obj2.hasOwnProperty('props') &&
// eslint-disable-next-line no-prototype-builtins
obj1.hasOwnProperty('key') &&
// eslint-disable-next-line no-prototype-builtins
obj2.hasOwnProperty('key') &&
// eslint-disable-next-line no-prototype-builtins
obj1.hasOwnProperty('ref') &&
// eslint-disable-next-line no-prototype-builtins
obj2.hasOwnProperty('ref') &&
// eslint-disable-next-line no-prototype-builtins
obj1.hasOwnProperty('type') &&
// eslint-disable-next-line no-prototype-builtins
obj2.hasOwnProperty('type')
) {
return deepEqual(obj1['props'], obj2['props']);
}
// If objects are both objects, compare their properties recursively
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
Expand Down
9 changes: 9 additions & 0 deletions tests/jest/util/deepEqual.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { deepEqual } from '../../../src/util/deepEqual';
import { html } from '../../../src/util/html';

describe('deepEqual', () => {
it('should return true when objects are the same', () => {
Expand Down Expand Up @@ -31,4 +32,12 @@ describe('deepEqual', () => {
const result = deepEqual({ a: 42, c: fn }, { a: 42, c: fn });
expect(result).toBeTrue();
});

it('should return true when objects are VNodes', () => {
const result = deepEqual(
html('<span>Grid.js</span>'),
html('<span>Grid.js</span>'),
);
expect(result).toBeTrue();
});
});

0 comments on commit 79124b2

Please sign in to comment.