Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the Vue/Vue3 wrappers' simpleEqual helper correctly recognize the same objects. #10896

Merged
merged 4 commits into from Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changelogs/10896.json
@@ -0,0 +1,8 @@
{
"issuesOrigin": "private",
"title": "Fixed a problem where the Vue/Vue3 wrappers' `simpleEqual` helper return incorrect result when comparing identical objects.",
"type": "fixed",
"issueOrPR": 10896,
"breaking": false,
"framework": "vue"
}
26 changes: 15 additions & 11 deletions wrappers/vue/src/helpers.ts
Expand Up @@ -253,18 +253,22 @@ export function createVueComponent(vNode: VNode, parent: Vue, props: object, dat
* @returns {boolean} `true` if they're the same, `false` otherwise.
*/
function simpleEqual(objectA, objectB) {
const circularReplacer = (function() {
const seen = new WeakSet();
const stringifyToJSON = (val) => {
const circularReplacer = (function() {
const seen = new WeakSet();

return function(key, value) {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) return;
seen.add(value);
}
return function(key, value) {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) return;
seen.add(value);
}

return value;
};
}());
return value;
};
}());

return JSON.stringify(val, circularReplacer);
};

if (typeof objectA === 'function' && typeof objectB === 'function') {
return objectA.toString() === objectB.toString();
Expand All @@ -273,6 +277,6 @@ function simpleEqual(objectA, objectB) {
return false;

} else {
return JSON.stringify(objectA, circularReplacer) === JSON.stringify(objectB, circularReplacer);
return stringifyToJSON(objectA) === stringifyToJSON(objectB);
}
}
12 changes: 12 additions & 0 deletions wrappers/vue/test/hotTableHelpers.spec.ts
@@ -1,3 +1,4 @@
import { GridSettings } from 'handsontable/settings';
import {
rewriteSettings,
prepareSettings,
Expand Down Expand Up @@ -87,4 +88,15 @@ describe('prepareSettings', () => {
expect(preparedSettings.whatever.foo).toBe('bar');
expect(preparedSettings.whatever.myself.foo).toBe('bar');
});

it('should not recognize passing of the same array twice as a changed object', () => {
const settings1: GridSettings = {
mergeCells: [{ row: 1, col: 1, colspan: 1, rowspan: 1 }],
};

const preparedSettings = prepareSettings({ settings: settings1 }, settings1);

expect(preparedSettings.mergeCells).toBe(undefined);
expect(Object.keys(preparedSettings).length).toBe(0);
});
});
26 changes: 15 additions & 11 deletions wrappers/vue3/src/helpers.ts
Expand Up @@ -139,18 +139,22 @@ export function prepareSettings(props: HotTableProps, currentSettings?: Handsont
* @returns {boolean} `true` if they're the same, `false` otherwise.
*/
function simpleEqual(objectA, objectB) {
const circularReplacer = (function() {
const seen = new WeakSet();
const stringifyToJSON = (val) => {
const circularReplacer = (function() {
const seen = new WeakSet();

return function(key, value) {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) return;
seen.add(value);
}
return function(key, value) {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) return;
seen.add(value);
}

return value;
};
}());
return value;
};
}());

return JSON.stringify(val, circularReplacer);
};

if (typeof objectA === 'function' && typeof objectB === 'function') {
return objectA.toString() === objectB.toString();
Expand All @@ -159,6 +163,6 @@ function simpleEqual(objectA, objectB) {
return false;

} else {
return JSON.stringify(objectA, circularReplacer) === JSON.stringify(objectB, circularReplacer);
return stringifyToJSON(objectA) === stringifyToJSON(objectB);
}
}
12 changes: 12 additions & 0 deletions wrappers/vue3/test/hotTableHelpers.spec.ts
@@ -1,3 +1,4 @@
import { GridSettings } from 'handsontable/settings';
import {
prepareSettings,
propFactory
Expand Down Expand Up @@ -60,4 +61,15 @@ describe('prepareSettings', () => {
expect(preparedSettings.whatever.foo).toBe('bar');
expect(preparedSettings.whatever.myself.foo).toBe('bar');
});

it('should not recognize passing of the same array twice as a changed object', () => {
const settings1: GridSettings = {
mergeCells: [{ row: 1, col: 1, colspan: 1, rowspan: 1 }],
};

const preparedSettings = prepareSettings({ settings: settings1 }, settings1);

expect(preparedSettings.mergeCells).toBe(undefined);
expect(Object.keys(preparedSettings).length).toBe(0);
});
});