diff --git a/packages/grafana-data/src/dataframe/frameComparisons.test.ts b/packages/grafana-data/src/dataframe/frameComparisons.test.ts index e88a9d5707d..b9e6fa98424 100644 --- a/packages/grafana-data/src/dataframe/frameComparisons.test.ts +++ b/packages/grafana-data/src/dataframe/frameComparisons.test.ts @@ -93,39 +93,6 @@ describe('test comparisons', () => { ).toBeFalsy(); }); - it('should skip provided properties', () => { - expect( - compareDataFrameStructures( - { - ...frameB, - fields: [ - field0, - { - ...field1, - config: { - ...field1.config, - }, - }, - ], - }, - { - ...frameB, - fields: [ - field0, - { - ...field1, - config: { - ...field1.config, - unit: 'rpm', - }, - }, - ], - }, - ['unit'] - ) - ).toBeTruthy(); - }); - describe('custom config comparison', () => { it('handles custom config shallow equality', () => { const a = { diff --git a/packages/grafana-data/src/dataframe/frameComparisons.ts b/packages/grafana-data/src/dataframe/frameComparisons.ts index c21ad4eb8b8..c9f7dd502b9 100644 --- a/packages/grafana-data/src/dataframe/frameComparisons.ts +++ b/packages/grafana-data/src/dataframe/frameComparisons.ts @@ -15,10 +15,11 @@ import { DataFrame } from '../types/dataFrame'; * * @beta */ -export function compareDataFrameStructures(a: DataFrame, b: DataFrame, skipProperties?: string[]): boolean { +export function compareDataFrameStructures(a: DataFrame, b: DataFrame): boolean { if (a === b) { return true; } + if (a?.fields?.length !== b?.fields?.length) { return false; } @@ -26,45 +27,36 @@ export function compareDataFrameStructures(a: DataFrame, b: DataFrame, skipPrope for (let i = 0; i < a.fields.length; i++) { const fA = a.fields[i]; const fB = b.fields[i]; + if (fA.type !== fB.type) { return false; } + const cfgA = fA.config as any; const cfgB = fB.config as any; let aKeys = Object.keys(cfgA); let bKeys = Object.keys(cfgB); - if (skipProperties) { - aKeys = aKeys.filter((k) => skipProperties.indexOf(k) < 0); - bKeys = aKeys.filter((k) => skipProperties.indexOf(k) < 0); - } if (aKeys.length !== bKeys.length) { return false; } for (const key of aKeys) { - if (skipProperties && skipProperties.indexOf(key) > -1) { - continue; - } - - if (!cfgB.hasOwnProperty(key)) { + if (!(key in cfgB)) { return false; } if (key === 'custom') { if (!shallowCompare(cfgA[key], cfgB[key])) { return false; - } else { - continue; } - } - - if (cfgA[key] !== cfgB[key]) { + } else if (cfgA[key] !== cfgB[key]) { return false; } } } + return true; } @@ -88,29 +80,30 @@ export function compareArrayValues(a: T[], b: T[], cmp: (a: T, b: T) => boole return true; } +type Cmp = (valA: any, valB: any) => boolean; + +const defaultCmp: Cmp = (a, b) => a === b; + /** * Checks if two objects are equal shallowly * * @beta */ -export function shallowCompare(a: T, b: T, cmp?: (valA: any, valB: any) => boolean) { - const aKeys = Object.keys(a); - const bKeys = Object.keys(b); +export function shallowCompare(a: T, b: T, cmp: Cmp = defaultCmp) { if (a === b) { return true; } + const aKeys = Object.keys(a); + const bKeys = Object.keys(b); + if (aKeys.length !== bKeys.length) { return false; } for (let key of aKeys) { - if (cmp) { - //@ts-ignore - return cmp(a[key], b[key]); - } //@ts-ignore - if (a[key] !== b[key]) { + if (!cmp(a[key], b[key])) { return false; } }