feat: remove diff
This commit is contained in:
@@ -112,57 +112,3 @@ export const sortComparator = (a, b, sortKey) => {
|
||||
SORT_OPTIONS[sortKey] || SORT_OPTIONS.last_activity_at_desc;
|
||||
return sortConfig[sortMethod](a, b, sortDirection);
|
||||
};
|
||||
|
||||
export const deepObjectDiff = (original, updated, maxDepth = 3) => {
|
||||
const changes = {
|
||||
added: {},
|
||||
removed: {},
|
||||
modified: {},
|
||||
};
|
||||
|
||||
function isObject(item) {
|
||||
return item && typeof item === 'object' && !Array.isArray(item);
|
||||
}
|
||||
|
||||
function compareObjects(orig, upd, path = '', depth = 1) {
|
||||
if (depth > maxDepth) {
|
||||
if (JSON.stringify(orig) !== JSON.stringify(upd)) {
|
||||
changes.modified[path] = { from: orig, to: upd };
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Get all keys from both objects
|
||||
const allKeys = [...new Set([...Object.keys(orig), ...Object.keys(upd)])];
|
||||
|
||||
allKeys.forEach(key => {
|
||||
const currentPath = path ? `${path}.${key}` : key;
|
||||
|
||||
// Handle removed properties
|
||||
if (!Object.keys(upd).includes(key)) {
|
||||
changes.removed[currentPath] = orig[key];
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle added properties
|
||||
if (!Object.keys(orig).includes(key)) {
|
||||
changes.added[currentPath] = upd[key];
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle nested objects
|
||||
if (isObject(orig[key]) && isObject(upd[key])) {
|
||||
compareObjects(orig[key], upd[key], currentPath, depth + 1);
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle modified values
|
||||
if (JSON.stringify(orig[key]) !== JSON.stringify(upd[key])) {
|
||||
changes.modified[currentPath] = { from: orig[key], to: upd[key] };
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
compareObjects(original, updated);
|
||||
return changes;
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import types from '../../mutation-types';
|
||||
import getters, { getSelectedChatConversation } from './getters';
|
||||
import actions from './actions';
|
||||
import { findPendingMessageIndex, deepObjectDiff } from './helpers';
|
||||
import { findPendingMessageIndex } from './helpers';
|
||||
import { MESSAGE_STATUS } from 'shared/constants/messages';
|
||||
import wootConstants from 'dashboard/constants/globals';
|
||||
import { BUS_EVENTS } from '../../../../shared/constants/busEvents';
|
||||
@@ -223,11 +223,6 @@ export const mutations = {
|
||||
scope.setContext('incoming_meta', conversation.meta);
|
||||
scope.setContext('stored_meta', selectedConversation.meta);
|
||||
|
||||
const diff = deepObjectDiff(conversation, selectedConversation);
|
||||
scope.setContext('added', diff.added);
|
||||
scope.setContext('removed', diff.removed);
|
||||
scope.setContext('modified', diff.modified);
|
||||
|
||||
Sentry.captureMessage('Conversation update mismatch');
|
||||
});
|
||||
|
||||
@@ -241,11 +236,6 @@ export const mutations = {
|
||||
scope.setContext('incoming_meta', conversation.meta);
|
||||
scope.setContext('stored_meta', selectedConversation.meta);
|
||||
|
||||
const diff = deepObjectDiff(conversation, selectedConversation);
|
||||
scope.setContext('added', diff.added);
|
||||
scope.setContext('removed', diff.removed);
|
||||
scope.setContext('modified', diff.modified);
|
||||
|
||||
Sentry.captureMessage('Conversation update overlap');
|
||||
});
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import {
|
||||
filterByTeam,
|
||||
filterByLabel,
|
||||
filterByUnattended,
|
||||
deepObjectDiff,
|
||||
} from '../../conversations/helpers';
|
||||
|
||||
const conversationList = [
|
||||
@@ -173,129 +172,3 @@ describe('#filterByUnattended', () => {
|
||||
expect(filterByUnattended(true, 'mentions', 123)).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#deepObjectDiff', () => {
|
||||
it('should detect added properties', () => {
|
||||
const original = { name: 'John', age: 30 };
|
||||
const updated = { name: 'John', age: 30, email: 'john@example.com' };
|
||||
|
||||
const result = deepObjectDiff(original, updated);
|
||||
|
||||
expect(result.added).toEqual({ email: 'john@example.com' });
|
||||
expect(result.removed).toEqual({});
|
||||
expect(result.modified).toEqual({});
|
||||
});
|
||||
|
||||
it('should detect removed properties', () => {
|
||||
const original = { name: 'John', age: 30, email: 'john@example.com' };
|
||||
const updated = { name: 'John', age: 30 };
|
||||
|
||||
const result = deepObjectDiff(original, updated);
|
||||
|
||||
expect(result.added).toEqual({});
|
||||
expect(result.removed).toEqual({ email: 'john@example.com' });
|
||||
expect(result.modified).toEqual({});
|
||||
});
|
||||
|
||||
it('should detect modified properties', () => {
|
||||
const original = { name: 'John', age: 30 };
|
||||
const updated = { name: 'John', age: 31 };
|
||||
|
||||
const result = deepObjectDiff(original, updated);
|
||||
|
||||
expect(result.added).toEqual({});
|
||||
expect(result.removed).toEqual({});
|
||||
expect(result.modified).toEqual({ age: { from: 30, to: 31 } });
|
||||
});
|
||||
|
||||
it('should handle nested objects', () => {
|
||||
const original = {
|
||||
name: 'John',
|
||||
address: { city: 'New York', country: 'USA' },
|
||||
};
|
||||
const updated = {
|
||||
name: 'John',
|
||||
address: { city: 'Boston', country: 'USA' },
|
||||
};
|
||||
|
||||
const result = deepObjectDiff(original, updated);
|
||||
|
||||
expect(result.added).toEqual({});
|
||||
expect(result.removed).toEqual({});
|
||||
expect(result.modified).toEqual({
|
||||
'address.city': { from: 'New York', to: 'Boston' },
|
||||
});
|
||||
});
|
||||
|
||||
it('should respect maxDepth parameter', () => {
|
||||
const original = {
|
||||
user: {
|
||||
details: {
|
||||
address: { city: 'New York', country: 'USA' },
|
||||
},
|
||||
},
|
||||
};
|
||||
const updated = {
|
||||
user: {
|
||||
details: {
|
||||
address: { city: 'Boston', country: 'USA' },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
// With maxDepth of 2
|
||||
const result2 = deepObjectDiff(original, updated, 2);
|
||||
expect(result2.modified).toEqual({
|
||||
'user.details': {
|
||||
from: {
|
||||
address: { city: 'New York', country: 'USA' },
|
||||
},
|
||||
to: {
|
||||
address: { city: 'Boston', country: 'USA' },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// With maxDepth of 4 (enough to reach the deepest level)
|
||||
const result4 = deepObjectDiff(original, updated, 4);
|
||||
expect(result4.modified).toEqual({
|
||||
'user.details.address.city': { from: 'New York', to: 'Boston' },
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle arrays correctly', () => {
|
||||
const original = { tags: ['important', 'urgent'] };
|
||||
const updated = { tags: ['important', 'normal'] };
|
||||
|
||||
const result = deepObjectDiff(original, updated);
|
||||
|
||||
expect(result.modified).toEqual({
|
||||
tags: {
|
||||
from: ['important', 'urgent'],
|
||||
to: ['important', 'normal'],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle empty objects', () => {
|
||||
const original = {};
|
||||
const updated = {};
|
||||
|
||||
const result = deepObjectDiff(original, updated);
|
||||
|
||||
expect(result.added).toEqual({});
|
||||
expect(result.removed).toEqual({});
|
||||
expect(result.modified).toEqual({});
|
||||
});
|
||||
|
||||
it('should handle completely different objects', () => {
|
||||
const original = { a: 1, b: 2 };
|
||||
const updated = { c: 3, d: 4 };
|
||||
|
||||
const result = deepObjectDiff(original, updated);
|
||||
|
||||
expect(result.added).toEqual({ c: 3, d: 4 });
|
||||
expect(result.removed).toEqual({ a: 1, b: 2 });
|
||||
expect(result.modified).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user